Fix buggy recursion in flatten_rtes_walker().
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 13 Feb 2023 17:19:58 +0000 (12:19 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 13 Feb 2023 17:19:58 +0000 (12:19 -0500)
Must save-and-restore the context we are modifying.
Oversight in commit a61b1f748.

Tender Wang

Discussion: https://postgr.es/m/CAHewXNnnNySD_YcKNuFpQDV2gxWA7_YLWqHmYVcyoOYxn8kY2A@mail.gmail.com
Discussion: https://postgr.es/m/20230212233711.GA1316@telsasoft.com

src/backend/optimizer/plan/setrefs.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 186fc8014b6203e2d6263ff384b1f77607db0cb0..5cc8366af6685cf14fa148e5eab56c43e132ad6d 100644 (file)
@@ -536,11 +536,16 @@ flatten_rtes_walker(Node *node, flatten_rtes_walker_context *cxt)
                 * Recurse into subselects.  Must update cxt->query to this query so
                 * that the rtable and rteperminfos correspond with each other.
                 */
+               Query      *save_query = cxt->query;
+               bool            result;
+
                cxt->query = (Query *) node;
-               return query_tree_walker((Query *) node,
-                                                                flatten_rtes_walker,
-                                                                (void *) cxt,
-                                                                QTW_EXAMINE_RTES_BEFORE);
+               result = query_tree_walker((Query *) node,
+                                                                  flatten_rtes_walker,
+                                                                  (void *) cxt,
+                                                                  QTW_EXAMINE_RTES_BEFORE);
+               cxt->query = save_query;
+               return result;
        }
        return expression_tree_walker(node, flatten_rtes_walker,
                                                                  (void *) cxt);
index db9512969f37396ec83bcb7fd45d4d18554669c8..16318d9da22d7fa057ece5b93531f7ba0228c7b8 100644 (file)
@@ -5569,6 +5569,18 @@ select atts.relid::regclass, s.* from pg_stats s join
 ERROR:  column atts.relid does not exist
 LINE 1: select atts.relid::regclass, s.* from pg_stats s join
                ^
+-- Test bug in rangetable flattening
+explain (verbose, costs off)
+select 1 from
+  (select * from int8_tbl where q1 <> (select 42) offset 0) ss
+where false;
+        QUERY PLAN        
+--------------------------
+ Result
+   Output: 1
+   One-Time Filter: false
+(3 rows)
+
 --
 -- Test LATERAL
 --
index 2ff68879d228770f91f7ddfe07a941eb8d10c4dd..2b2e09495505f16e2e0948510a47ef5cfa4da3a1 100644 (file)
@@ -2086,6 +2086,12 @@ select atts.relid::regclass, s.* from pg_stats s join
     indexrelid from pg_index i) atts on atts.attnum = a.attnum where
     schemaname != 'pg_catalog';
 
+-- Test bug in rangetable flattening
+explain (verbose, costs off)
+select 1 from
+  (select * from int8_tbl where q1 <> (select 42) offset 0) ss
+where false;
+
 --
 -- Test LATERAL
 --