]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Avoid creating a RESULT RTE that's marked LATERAL.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 9 Jul 2021 17:38:24 +0000 (13:38 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 9 Jul 2021 17:38:24 +0000 (13:38 -0400)
Commit 7266d0997 added code to pull up simple constant function
results, converting the RTE_FUNCTION RTE to a dummy RTE_RESULT
RTE since it no longer need be scanned.  But I forgot to clear
the LATERAL flag if the RTE has it set.  If the function reduced
to a constant, it surely contains no lateral references so this
simplification is logically OK.  It's needed because various other
places will Assert that RESULT RTEs aren't LATERAL.

Per bug #17097 from Yaoguang Chen.  Back-patch to v13 where the
faulty code came in.

Discussion: https://postgr.es/m/17097-3372ef9f798fc94f@postgresql.org

src/backend/optimizer/prep/prepjointree.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 9ff2b595a8650ad9e9d27f221967bd9ad0602d6e..08a73fb9d8699e2252a956edbd958f935ad3993c 100644 (file)
@@ -1802,10 +1802,13 @@ pull_up_constant_function(PlannerInfo *root, Node *jtnode,
 
        /*
         * Convert the RTE to be RTE_RESULT type, signifying that we don't need to
-        * scan it anymore, and zero out RTE_FUNCTION-specific fields.
+        * scan it anymore, and zero out RTE_FUNCTION-specific fields.  Also make
+        * sure the RTE is not marked LATERAL, since elsewhere we don't expect
+        * RTE_RESULTs to be LATERAL.
         */
        rte->rtekind = RTE_RESULT;
        rte->functions = NIL;
+       rte->lateral = false;
 
        /*
         * We can reuse the RangeTblRef node.
index 37ef22f3d5c64c47ae54f6a9b2ee8a75389c5ab5..9b9fe11f278db0b63a30fa75a53f42f95068c130 100644 (file)
@@ -3414,6 +3414,14 @@ select unique1 from tenk1, lateral f_immutable_int4(1) x where x = unique1;
    Index Cond: (unique1 = 1)
 (2 rows)
 
+explain (costs off)
+select unique1 from tenk1, lateral f_immutable_int4(1) x where x in (select 17);
+        QUERY PLAN        
+--------------------------
+ Result
+   One-Time Filter: false
+(2 rows)
+
 explain (costs off)
 select unique1, x from tenk1 join f_immutable_int4(1) x on unique1 = x;
                   QUERY PLAN                  
index 88181848603e0bf650a4b83ac09062ba50fa546c..2fc4f030f7a0982e26dda81e67cb86f295fb6577 100644 (file)
@@ -1099,6 +1099,9 @@ select unique1 from tenk1, f_immutable_int4(1) x where x = unique1;
 explain (costs off)
 select unique1 from tenk1, lateral f_immutable_int4(1) x where x = unique1;
 
+explain (costs off)
+select unique1 from tenk1, lateral f_immutable_int4(1) x where x in (select 17);
+
 explain (costs off)
 select unique1, x from tenk1 join f_immutable_int4(1) x on unique1 = x;