pgsql: Fix mishandling of FieldSelect-on-whole-row-Var in nested latera - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix mishandling of FieldSelect-on-whole-row-Var in nested latera
Date
Msg-id E1XgFkZ-000577-FS@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix mishandling of FieldSelect-on-whole-row-Var in nested lateral queries.

If an inline-able SQL function taking a composite argument is used in a
LATERAL subselect, and the composite argument is a lateral reference,
the planner could fail with "variable not found in subplan target list",
as seen in bug #11703 from Karl Bartel.  (The outer function call used in
the bug report and in the committed regression test is not really necessary
to provoke the bug --- you can get it if you manually expand the outer
function into "LATERAL (SELECT inner_function(outer_relation))", too.)

The cause of this is that we generate the reltargetlist for the referenced
relation before doing eval_const_expressions() on the lateral sub-select's
expressions (cf find_lateral_references()), so what's scheduled to be
emitted by the referenced relation is a whole-row Var, not the simplified
single-column Var produced by optimizing the function's FieldSelect on the
whole-row Var.  Then setrefs.c fails to match up that lateral reference to
what's available from the outer scan.

Preserving the FieldSelect optimization in such cases would require either
major planner restructuring (to recursively do expression simplification
on sub-selects much earlier) or some amazingly ugly kluge to change the
reltargetlist of a possibly-already-planned relation.  It seems better
just to skip the optimization when the Var is from an upper query level;
the case is not so common that it's likely anyone will notice a few
wasted cycles.

AFAICT this problem only occurs for uplevel LATERAL references, so
back-patch to 9.3 where LATERAL was added.

Branch
------
REL9_3_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/4e54685d0bcc74b840fdeff54c79c343941d3681

Modified Files
--------------
src/backend/optimizer/util/clauses.c     |   20 +++++++++---
src/test/regress/expected/rangefuncs.out |   52 ++++++++++++++++++++++++++++++
src/test/regress/sql/rangefuncs.sql      |   20 ++++++++++++
3 files changed, 87 insertions(+), 5 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix mishandling of FieldSelect-on-whole-row-Var in nested latera
Next
From: Tom Lane
Date:
Subject: pgsql: Fix mishandling of FieldSelect-on-whole-row-Var in nested latera