Skip to content

Commit 6443cd2

Browse files
committed
Cope with lateral references in the quals of a subquery RTE.
The qual pushdown logic assumed that all Vars in a restriction clause must be Vars referencing subquery outputs; but since we introduced LATERAL, it's possible for such a Var to be a lateral reference instead. This led to an assertion failure in debug builds. In a non-debug build, there might be no ill effects (if qual_is_pushdown_safe decided the qual was unsafe anyway), or we could get failures later due to construction of an invalid plan. I've not gone to much length to characterize the possible failures, but at least segfaults in the executor have been observed. Given that this has been busted since 9.3 and it took this long for anybody to notice, I judge that the case isn't worth going to great lengths to optimize. Hence, fix by just teaching qual_is_pushdown_safe that such quals are unsafe to push down, matching the previous behavior when it accidentally didn't fail. Per report from Tom Ellis. Back-patch to all supported branches. Discussion: https://postgr.es/m/20200713175124.GQ8220@cloudinit-builder
1 parent 2d43c30 commit 6443cd2

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/backend/optimizer/path/allpaths.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,8 +2845,10 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
28452845
Assert(!contain_window_function(qual));
28462846

28472847
/*
2848-
* Examine all Vars used in clause; since it's a restriction clause, all
2849-
* such Vars must refer to subselect output columns.
2848+
* Examine all Vars used in clause. Since it's a restriction clause, all
2849+
* such Vars must refer to subselect output columns ... unless this is
2850+
* part of a LATERAL subquery, in which case there could be lateral
2851+
* references.
28502852
*/
28512853
vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
28522854
foreach(vl, vars)
@@ -2866,7 +2868,19 @@ qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
28662868
break;
28672869
}
28682870

2869-
Assert(var->varno == rti);
2871+
/*
2872+
* Punt if we find any lateral references. It would be safe to push
2873+
* these down, but we'd have to convert them into outer references,
2874+
* which subquery_push_qual lacks the infrastructure to do. The case
2875+
* arises so seldom that it doesn't seem worth working hard on.
2876+
*/
2877+
if (var->varno != rti)
2878+
{
2879+
safe = false;
2880+
break;
2881+
}
2882+
2883+
/* Subqueries have no system columns */
28702884
Assert(var->varattno >= 0);
28712885

28722886
/* Check point 4 */

src/test/regress/expected/subselect.out

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,59 @@ from int4_tbl;
980980
(4,5,6.0)
981981
(5 rows)
982982

983+
--
984+
-- Check for sane handling of a lateral reference in a subquery's quals
985+
-- (most of the complication here is to prevent the test case from being
986+
-- flattened too much)
987+
--
988+
explain (verbose, costs off)
989+
select * from
990+
int4_tbl i4,
991+
lateral (
992+
select i4.f1 > 1 as b, 1 as id
993+
from (select random() order by 1) as t1
994+
union all
995+
select true as b, 2 as id
996+
) as t2
997+
where b and f1 >= 0;
998+
QUERY PLAN
999+
--------------------------------------------
1000+
Nested Loop
1001+
Output: i4.f1, ((i4.f1 > 1)), (1)
1002+
-> Seq Scan on public.int4_tbl i4
1003+
Output: i4.f1
1004+
Filter: (i4.f1 >= 0)
1005+
-> Append
1006+
-> Subquery Scan on t1
1007+
Output: (i4.f1 > 1), 1
1008+
Filter: (i4.f1 > 1)
1009+
-> Sort
1010+
Output: (random())
1011+
Sort Key: (random())
1012+
-> Result
1013+
Output: random()
1014+
-> Result
1015+
Output: true, 2
1016+
(16 rows)
1017+
1018+
select * from
1019+
int4_tbl i4,
1020+
lateral (
1021+
select i4.f1 > 1 as b, 1 as id
1022+
from (select random() order by 1) as t1
1023+
union all
1024+
select true as b, 2 as id
1025+
) as t2
1026+
where b and f1 >= 0;
1027+
f1 | b | id
1028+
------------+---+----
1029+
0 | t | 2
1030+
123456 | t | 1
1031+
123456 | t | 2
1032+
2147483647 | t | 1
1033+
2147483647 | t | 2
1034+
(5 rows)
1035+
9831036
--
9841037
-- Check that volatile quals aren't pushed down past a DISTINCT:
9851038
-- nextval() should not be called more than the nominal number of times

src/test/regress/sql/subselect.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,32 @@ select (select q from
526526
) q )
527527
from int4_tbl;
528528

529+
--
530+
-- Check for sane handling of a lateral reference in a subquery's quals
531+
-- (most of the complication here is to prevent the test case from being
532+
-- flattened too much)
533+
--
534+
explain (verbose, costs off)
535+
select * from
536+
int4_tbl i4,
537+
lateral (
538+
select i4.f1 > 1 as b, 1 as id
539+
from (select random() order by 1) as t1
540+
union all
541+
select true as b, 2 as id
542+
) as t2
543+
where b and f1 >= 0;
544+
545+
select * from
546+
int4_tbl i4,
547+
lateral (
548+
select i4.f1 > 1 as b, 1 as id
549+
from (select random() order by 1) as t1
550+
union all
551+
select true as b, 2 as id
552+
) as t2
553+
where b and f1 >= 0;
554+
529555
--
530556
-- Check that volatile quals aren't pushed down past a DISTINCT:
531557
-- nextval() should not be called more than the nominal number of times

0 commit comments

Comments
 (0)