Skip site navigation (1) Skip section navigation (2)

pgsql: Fix PARAM_EXEC assignment mechanism to be safe in the presenceo

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix PARAM_EXEC assignment mechanism to be safe in the presenceo
Date: 2012-09-08 00:39:31
Message-ID: E1TA959-00039A-9U@gemulon.postgresql.org (view raw or flat)
Thread:
Lists: 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
------
REL8_4_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/6e54298bb418dd9dfb983cfe9b06034754f83a8e

Modified Files
--------------
src/backend/nodes/outfuncs.c              |    5 +-
src/backend/optimizer/path/allpaths.c     |   10 ++
src/backend/optimizer/plan/planner.c      |    5 +-
src/backend/optimizer/plan/subselect.c    |  250 ++++++++++++++---------------
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, 218 insertions(+), 149 deletions(-)

pgsql-committers by date

Next:From: Tom LaneDate: 2012-09-08 21:39:23
Subject: pgsql: Adjust PL/Python regression tests some more for Python 3.3.
Previous:From: pgsqlDate: 2012-09-08 00:33:57
Subject: pgsql: Tag refs/tags/REL9_2_0 was created

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group