Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 147fbf9

Browse files
committed
Repair bug reported by Laurent Perez: bad plan generated when UPDATE or
DELETE of an inheritance tree references another inherited relation. This bug has been latent since 7.1; I'm still not quite sure why 7.1 and 7.2 don't manifest it (at least, they don't crash on a simple test case).
1 parent 391eb5e commit 147fbf9

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.148 2003/02/15 21:39:58 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.149 2003/03/05 18:38:14 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -443,6 +443,7 @@ inheritance_planner(Query *parse, List *inheritlist)
443443
{
444444
int parentRTindex = parse->resultRelation;
445445
Oid parentOID = getrelid(parentRTindex, parse->rtable);
446+
int mainrtlength = length(parse->rtable);
446447
List *subplans = NIL;
447448
List *tlist = NIL;
448449
List *l;
@@ -451,6 +452,7 @@ inheritance_planner(Query *parse, List *inheritlist)
451452
{
452453
int childRTindex = lfirsti(l);
453454
Oid childOID = getrelid(childRTindex, parse->rtable);
455+
int subrtlength;
454456
Query *subquery;
455457
Plan *subplan;
456458

@@ -461,6 +463,24 @@ inheritance_planner(Query *parse, List *inheritlist)
461463
/* Generate plan */
462464
subplan = grouping_planner(subquery, 0.0 /* retrieve all tuples */ );
463465
subplans = lappend(subplans, subplan);
466+
/*
467+
* It's possible that additional RTEs got added to the rangetable
468+
* due to expansion of inherited source tables (see allpaths.c).
469+
* If so, we must copy 'em back to the main parse tree's rtable.
470+
*
471+
* XXX my goodness this is ugly. Really need to think about ways
472+
* to rein in planner's habit of scribbling on its input.
473+
*/
474+
subrtlength = length(subquery->rtable);
475+
if (subrtlength > mainrtlength)
476+
{
477+
List *subrt = subquery->rtable;
478+
479+
while (mainrtlength-- > 0) /* wish we had nthcdr() */
480+
subrt = lnext(subrt);
481+
parse->rtable = nconc(parse->rtable, subrt);
482+
mainrtlength = subrtlength;
483+
}
464484
/* Save preprocessed tlist from first rel for use in Append */
465485
if (tlist == NIL)
466486
tlist = subplan->targetlist;

0 commit comments

Comments
 (0)