8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -816,19 +816,22 @@ compare_tlist_datatypes(List *tlist, List *colTypes,
816
816
* it will work correctly: sublinks will already have been transformed into
817
817
* subplans in the qual, but not in the subquery).
818
818
*
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
820
823
* found to have inconsistent types across a set operation tree by
821
824
* subquery_is_pushdown_safe().
822
825
*
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
824
827
* refer to non-DISTINCT output columns, because that could change the set
825
828
* of rows returned. This condition is vacuous for DISTINCT, because then
826
829
* there are no non-DISTINCT output columns, but unfortunately it's fairly
827
830
* expensive to tell the difference between DISTINCT and DISTINCT ON in the
828
831
* parsetree representation. It's cheaper to just make sure all the Vars
829
832
* in the qual refer to DISTINCT columns.
830
833
*
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
832
835
* return sets, else we'd introduce functions-returning-sets into the
833
836
* subquery's WHERE/HAVING quals.
834
837
*/
@@ -857,6 +860,13 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
857
860
858
861
Assert (var -> varno == rti );
859
862
863
+ /* Check point 2 */
864
+ if (var -> varattno == 0 )
865
+ {
866
+ safe = false;
867
+ break ;
868
+ }
869
+
860
870
/*
861
871
* We use a bitmapset to avoid testing the same attno more than once.
862
872
* (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,
866
876
continue ;
867
877
tested = bms_add_member (tested , var -> varattno );
868
878
869
- /* Check point 2 */
879
+ /* Check point 3 */
870
880
if (differentTypes [var -> varattno ])
871
881
{
872
882
safe = false;
@@ -878,7 +888,7 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
878
888
Assert (tle != NULL );
879
889
Assert (!tle -> resjunk );
880
890
881
- /* If subquery uses DISTINCT or DISTINCT ON, check point 3 */
891
+ /* If subquery uses DISTINCT or DISTINCT ON, check point 4 */
882
892
if (subquery -> distinctClause != NIL &&
883
893
!targetIsInSortList (tle , subquery -> distinctClause ))
884
894
{
@@ -887,7 +897,7 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
887
897
break ;
888
898
}
889
899
890
- /* Refuse functions returning sets (point 4 ) */
900
+ /* Refuse functions returning sets (point 5 ) */
891
901
if (expression_returns_set ((Node * ) tle -> expr ))
892
902
{
893
903
safe = false;
0 commit comments