Skip to content

Commit fcba3b8

Browse files
committed
Tweak trivial_subqueryscan() to consider a SubqueryScan's targetlist
trivial if it contains either Vars referencing the corresponding subplan columns, or Consts equaling the corresponding subplan columns. This lets the planner eliminate the SubqueryScan in some cases generated by generate_setop_tlist().
1 parent 88b8110 commit fcba3b8

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/backend/optimizer/plan/setrefs.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.124 2006/08/12 02:52:05 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.125 2006/08/28 14:32:41 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -448,16 +448,32 @@ trivial_subqueryscan(SubqueryScan *plan)
448448
{
449449
TargetEntry *ptle = (TargetEntry *) lfirst(lp);
450450
TargetEntry *ctle = (TargetEntry *) lfirst(lc);
451-
Var *var = (Var *) ptle->expr;
452451

453452
if (ptle->resjunk != ctle->resjunk)
454453
return false; /* tlist doesn't match junk status */
455-
if (!var || !IsA(var, Var))
456-
return false; /* tlist item not a Var */
457-
Assert(var->varno == plan->scan.scanrelid);
458-
Assert(var->varlevelsup == 0);
459-
if (var->varattno != attrno)
460-
return false; /* out of order */
454+
455+
/*
456+
* We accept either a Var referencing the corresponding element of
457+
* the subplan tlist, or a Const equaling the subplan element.
458+
* See generate_setop_tlist() for motivation.
459+
*/
460+
if (ptle->expr && IsA(ptle->expr, Var))
461+
{
462+
Var *var = (Var *) ptle->expr;
463+
464+
Assert(var->varno == plan->scan.scanrelid);
465+
Assert(var->varlevelsup == 0);
466+
if (var->varattno != attrno)
467+
return false; /* out of order */
468+
}
469+
else if (ptle->expr && IsA(ptle->expr, Const))
470+
{
471+
if (!equal(ptle->expr, ctle->expr))
472+
return false;
473+
}
474+
else
475+
return false;
476+
461477
attrno++;
462478
}
463479

0 commit comments

Comments
 (0)