Fix PARAM_EXEC assignment mechanism to be safe in the presence of WITH.
The planner previously assumed that parameter Vars having the same absolute
query level, varno, and varattno could safely be assigned the same runtime
PARAM_EXEC slot, even though they might be different Vars appearing in
different subqueries. This was (probably) safe before the introduction of
CTEs, but the lazy-evalution mechanism used for CTEs means that a CTE can
be executed during execution of some other subquery, causing the lifespan
of Params at the same syntactic nesting level as the CTE to overlap with
use of the same slots inside the CTE. In 9.1 we created additional hazards
by using the same parameter-assignment technology for nestloop inner scan
parameters, but it was broken before that, as illustrated by the added
To fix, restructure the planner's management of PlannerParamItems so that
items having different semantic lifespans are kept rigorously separated.
This will probably result in complex queries using more runtime PARAM_EXEC
slots than before, but the slots are cheap enough that this hardly matters.
Also, stop generating PlannerParamItems containing Params for subquery
outputs: all we really need to do is reserve the PARAM_EXEC slot number,
and that now only takes incrementing a counter. The planning code is
simpler and probably faster than before, as well as being more correct.
Per report from Vik Reykja.
Back-patch of commit 46c508fbcf98ac334f1e831d21021d731c882fbb into all
branches that support WITH.
src/backend/nodes/outfuncs.c | 5 +-
src/backend/optimizer/path/allpaths.c | 10 ++
src/backend/optimizer/plan/planner.c | 7 +-
src/backend/optimizer/plan/subselect.c | 251 ++++++++++++++---------------
src/backend/optimizer/prep/prepjointree.c | 1 +
src/backend/optimizer/prep/prepunion.c | 10 ++
src/include/nodes/relation.h | 51 ++++--
src/test/regress/expected/with.out | 21 +++
src/test/regress/sql/with.sql | 14 ++
9 files changed, 219 insertions(+), 151 deletions(-)
pgsql-committers by date
|Next:||From: Tom Lane||Date: 2012-09-08 21:39:23|
|Subject: pgsql: Adjust PL/Python regression tests some more for Python 3.3.|
|Previous:||From: pgsql||Date: 2012-09-08 00:33:57|
|Subject: pgsql: Tag refs/tags/REL9_2_0 was created|