Skip to content

Commit

Permalink
Fix buggy recursion in flatten_rtes_walker().
Browse files Browse the repository at this point in the history
Must save-and-restore the context we are modifying.
Oversight in commit a61b1f7.

Tender Wang

Discussion: https://postgr.es/m/CAHewXNnnNySD_YcKNuFpQDV2gxWA7_YLWqHmYVcyoOYxn8kY2A@mail.gmail.com
Discussion: https://postgr.es/m/20230212233711.GA1316@telsasoft.com
  • Loading branch information
tglsfdc committed Feb 13, 2023
1 parent f50f029 commit c7468c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/backend/optimizer/plan/setrefs.c
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/test/regress/expected/join.out
Expand Up @@ -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
--
Expand Down
6 changes: 6 additions & 0 deletions src/test/regress/sql/join.sql
Expand Up @@ -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
--
Expand Down

0 comments on commit c7468c7

Please sign in to comment.