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 E1TA959-000399-8z@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.

Back-patch of commit 46c508fbcf98ac334f1e831d21021d731c882fbb into all
branches that support WITH.

Branch
------
REL9_0_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/25b6df1e35190270b2cd04fd3beec7fed8441829

Modified Files
--------------
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:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix PARAM_EXEC assignment mechanism to be safe in the presence o
Next
From: Tom Lane
Date:
Subject: pgsql: Fix PARAM_EXEC assignment mechanism to be safe in the presence o