diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2010-07-28 04:51:27 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2010-07-28 04:51:27 +0000 |
commit | 7c294bf761c3329e42d4ede04e976f7c0145f7e8 (patch) | |
tree | 84d288905962447d915d48909bd31412d2d31ec5 | |
parent | 003c5981c32a984d8b9e7518551b9df9bb9fa1a3 (diff) | |
download | postgresql-7c294bf761c3329e42d4ede04e976f7c0145f7e8.tar.gz postgresql-7c294bf761c3329e42d4ede04e976f7c0145f7e8.zip |
Fix potential failure when hashing the output of a subplan that produces
a pass-by-reference datatype with a nontrivial projection step.
We were using the same memory context for the projection operation as for
the temporary context used by the hashtable routines in execGrouping.c.
However, the hashtable routines feel free to reset their temp context at
any time, which'd lead to destroying input data that was still needed.
Report and diagnosis by Tao Ma.
Back-patch to 8.1, where the problem was introduced by the changes that
allowed us to work with "virtual" tuples instead of materializing intermediate
tuple values everywhere. The earlier code looks quite similar, but it doesn't
suffer the problem because the data gets copied into another context as a
result of having to materialize ExecProject's output tuple.
-rw-r--r-- | src/backend/executor/nodeSubplan.c | 34 | ||||
-rw-r--r-- | src/include/nodes/execnodes.h | 7 | ||||
-rw-r--r-- | src/test/regress/expected/subselect.out | 9 | ||||
-rw-r--r-- | src/test/regress/sql/subselect.sql | 6 |
4 files changed, 36 insertions, 20 deletions
diff --git a/src/backend/executor/nodeSubplan.c b/src/backend/executor/nodeSubplan.c index 19e92e8f343..56054dedeb9 100644 --- a/src/backend/executor/nodeSubplan.c +++ b/src/backend/executor/nodeSubplan.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/executor/nodeSubplan.c,v 1.70.2.3 2007/04/26 23:25:08 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/executor/nodeSubplan.c,v 1.70.2.4 2010/07/28 04:51:27 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -78,7 +78,6 @@ ExecHashSubPlan(SubPlanState *node, { SubPlan *subplan = (SubPlan *) node->xprstate.expr; PlanState *planstate = node->planstate; - ExprContext *innerecontext = node->innerecontext; TupleTableSlot *slot; /* Shouldn't have any direct correlation Vars */ @@ -116,12 +115,6 @@ ExecHashSubPlan(SubPlanState *node, */ /* - * Since the hashtable routines will use innerecontext's per-tuple memory - * as working memory, be sure to reset it for each tuple. - */ - ResetExprContext(innerecontext); - - /* * If the LHS is all non-null, probe for an exact match in the main hash * table. If we find one, the result is TRUE. Otherwise, scan the * partly-null table to see if there are any rows that aren't provably @@ -465,7 +458,6 @@ buildSubPlanHash(SubPlanState *node) PlanState *planstate = node->planstate; int ncols = list_length(node->exprs); ExprContext *innerecontext = node->innerecontext; - MemoryContext tempcxt = innerecontext->ecxt_per_tuple_memory; MemoryContext oldcontext; int nbuckets; TupleTableSlot *slot; @@ -488,7 +480,7 @@ buildSubPlanHash(SubPlanState *node) * If it's not necessary to distinguish FALSE and UNKNOWN, then we don't * need to store subplan output rows that contain NULL. */ - MemoryContextReset(node->tablecxt); + MemoryContextReset(node->hashtablecxt); node->hashtable = NULL; node->hashnulls = NULL; node->havehashrows = false; @@ -504,8 +496,8 @@ buildSubPlanHash(SubPlanState *node) node->hashfunctions, nbuckets, sizeof(TupleHashEntryData), - node->tablecxt, - tempcxt); + node->hashtablecxt, + node->hashtempcxt); if (!subplan->unknownEqFalse) { @@ -523,8 +515,8 @@ buildSubPlanHash(SubPlanState *node) node->hashfunctions, nbuckets, sizeof(TupleHashEntryData), - node->tablecxt, - tempcxt); + node->hashtablecxt, + node->hashtempcxt); } /* @@ -583,7 +575,7 @@ buildSubPlanHash(SubPlanState *node) /* * Reset innerecontext after each inner tuple to free any memory used - * in hash computation or comparison routines. + * during ExecProject. */ ResetExprContext(innerecontext); } @@ -699,7 +691,8 @@ ExecInitSubPlan(SubPlanState *node, EState *estate) node->projRight = NULL; node->hashtable = NULL; node->hashnulls = NULL; - node->tablecxt = NULL; + node->hashtablecxt = NULL; + node->hashtempcxt = NULL; node->innerecontext = NULL; node->keyColIdx = NULL; node->eqfunctions = NULL; @@ -775,12 +768,19 @@ ExecInitSubPlan(SubPlanState *node, EState *estate) ListCell *lexpr; /* We need a memory context to hold the hash table(s) */ - node->tablecxt = + node->hashtablecxt = AllocSetContextCreate(CurrentMemoryContext, "Subplan HashTable Context", ALLOCSET_DEFAULT_MINSIZE, ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE); + /* and a small one for the hash tables to use as temp storage */ + node->hashtempcxt = + AllocSetContextCreate(CurrentMemoryContext, + "Subplan HashTable Temp Context", + ALLOCSET_SMALL_MINSIZE, + ALLOCSET_SMALL_INITSIZE, + ALLOCSET_SMALL_MAXSIZE); /* and a short-lived exprcontext for function evaluation */ node->innerecontext = CreateExprContext(estate); /* Silly little array of column numbers 1..n */ diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index f873f93cc52..7a7ef655bdd 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.139.2.4 2007/04/26 23:25:09 tgl Exp $ + * $PostgreSQL: pgsql/src/include/nodes/execnodes.h,v 1.139.2.5 2010/07/28 04:51:27 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -581,8 +581,9 @@ typedef struct SubPlanState TupleHashTable hashnulls; /* hash table for rows with null(s) */ bool havehashrows; /* TRUE if hashtable is not empty */ bool havenullrows; /* TRUE if hashnulls is not empty */ - MemoryContext tablecxt; /* memory context containing tables */ - ExprContext *innerecontext; /* working context for comparisons */ + MemoryContext hashtablecxt; /* memory context containing hash tables */ + MemoryContext hashtempcxt; /* temp memory context for hash tables */ + ExprContext *innerecontext; /* econtext for computing inner tuples */ AttrNumber *keyColIdx; /* control data for hash tables */ FmgrInfo *eqfunctions; /* comparison functions for hash tables */ FmgrInfo *hashfunctions; /* lookup data for hash functions */ diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out index 339b46f2972..7dd2db728bd 100644 --- a/src/test/regress/expected/subselect.out +++ b/src/test/regress/expected/subselect.out @@ -449,3 +449,12 @@ from ----- (0 rows) +-- +-- Test case for premature memory release during hashing of subplan output +-- +select '1'::text in (select '1'::name union all select '1'::name); + ?column? +---------- + t +(1 row) + diff --git a/src/test/regress/sql/subselect.sql b/src/test/regress/sql/subselect.sql index 46b46b4c45e..257ff9a6a59 100644 --- a/src/test/regress/sql/subselect.sql +++ b/src/test/regress/sql/subselect.sql @@ -289,3 +289,9 @@ from from int8_tbl) sq0 join int4_tbl i4 on dummy = i4.f1; + +-- +-- Test case for premature memory release during hashing of subplan output +-- + +select '1'::text in (select '1'::name union all select '1'::name); |