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

Commit 4299a92

Browse files
committed
Fix qual_is_pushdown_safe to not try to push down quals involving a whole-row
Var referencing the subselect output. While this case could possibly be made to work, it seems not worth expending effort on. Per report from Magnus Naeslund(f).
1 parent 562f63b commit 4299a92

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/backend/optimizer/path/allpaths.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.142 2006/02/04 23:03:20 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.143 2006/02/13 16:22:23 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -816,19 +816,22 @@ compare_tlist_datatypes(List *tlist, List *colTypes,
816816
* it will work correctly: sublinks will already have been transformed into
817817
* subplans in the qual, but not in the subquery).
818818
*
819-
* 2. The qual must not refer to any subquery output columns that were
819+
* 2. The qual must not refer to the whole-row output of the subquery
820+
* (since there is no easy way to name that within the subquery itself).
821+
*
822+
* 3. The qual must not refer to any subquery output columns that were
820823
* found to have inconsistent types across a set operation tree by
821824
* subquery_is_pushdown_safe().
822825
*
823-
* 3. If the subquery uses DISTINCT ON, we must not push down any quals that
826+
* 4. If the subquery uses DISTINCT ON, we must not push down any quals that
824827
* refer to non-DISTINCT output columns, because that could change the set
825828
* of rows returned. This condition is vacuous for DISTINCT, because then
826829
* there are no non-DISTINCT output columns, but unfortunately it's fairly
827830
* expensive to tell the difference between DISTINCT and DISTINCT ON in the
828831
* parsetree representation. It's cheaper to just make sure all the Vars
829832
* in the qual refer to DISTINCT columns.
830833
*
831-
* 4. We must not push down any quals that refer to subselect outputs that
834+
* 5. We must not push down any quals that refer to subselect outputs that
832835
* return sets, else we'd introduce functions-returning-sets into the
833836
* subquery's WHERE/HAVING quals.
834837
*/
@@ -857,6 +860,13 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
857860

858861
Assert(var->varno == rti);
859862

863+
/* Check point 2 */
864+
if (var->varattno == 0)
865+
{
866+
safe = false;
867+
break;
868+
}
869+
860870
/*
861871
* We use a bitmapset to avoid testing the same attno more than once.
862872
* (NB: this only works because subquery outputs can't have negative
@@ -866,7 +876,7 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
866876
continue;
867877
tested = bms_add_member(tested, var->varattno);
868878

869-
/* Check point 2 */
879+
/* Check point 3 */
870880
if (differentTypes[var->varattno])
871881
{
872882
safe = false;
@@ -878,7 +888,7 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
878888
Assert(tle != NULL);
879889
Assert(!tle->resjunk);
880890

881-
/* If subquery uses DISTINCT or DISTINCT ON, check point 3 */
891+
/* If subquery uses DISTINCT or DISTINCT ON, check point 4 */
882892
if (subquery->distinctClause != NIL &&
883893
!targetIsInSortList(tle, subquery->distinctClause))
884894
{
@@ -887,7 +897,7 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
887897
break;
888898
}
889899

890-
/* Refuse functions returning sets (point 4) */
900+
/* Refuse functions returning sets (point 5) */
891901
if (expression_returns_set((Node *) tle->expr))
892902
{
893903
safe = false;

0 commit comments

Comments
 (0)