pgsql: Fix PARAM_EXEC assignment mechanism to be safe in the presence o - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix PARAM_EXEC assignment mechanism to be safe in the presence o
Date
Msg-id E1T9Iss-0004gk-W5@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
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
regression test.

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.

These changes will mostly also need to be made in the back branches, but
I'm going to hold off on that until after 9.2.0 wraps.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/46c508fbcf98ac334f1e831d21021d731c882fbb

Modified Files
--------------
src/backend/nodes/outfuncs.c              |    6 +-
src/backend/optimizer/path/allpaths.c     |    7 +
src/backend/optimizer/plan/createplan.c   |   63 ++++----
src/backend/optimizer/plan/planner.c      |    7 +-
src/backend/optimizer/plan/subselect.c    |  279 ++++++++++++++---------------
src/backend/optimizer/prep/prepjointree.c |    1 +
src/backend/optimizer/prep/prepunion.c    |   10 +
src/backend/optimizer/util/relnode.c      |    2 +
src/include/nodes/relation.h              |   71 ++++----
src/test/regress/expected/with.out        |   21 +++
src/test/regress/sql/with.sql             |   14 ++
11 files changed, 269 insertions(+), 212 deletions(-)


pgsql-committers by date:

Previous
From: Alvaro Herrera
Date:
Subject: pgsql: Trim spgist_private.h inclusion
Next
From: Robert Haas
Date:
Subject: pgsql: sepgsql cleanups.