summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-08-09 11:21:39 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2024-08-09 11:21:39 -0400
commit3ad35d5022c9e5329906542150a19f9a38f9564d (patch)
treeda9e960810f4a3aaae630a54b1337b25ebd02211 /src/test
parent2ee02c98dd17f19ac5407e80c904d408931ab9ec (diff)
Fix "failed to find plan for subquery/CTE" errors in EXPLAIN.
To deparse a reference to a field of a RECORD-type output of a subquery, EXPLAIN normally digs down into the subquery's plan to try to discover exactly which anonymous RECORD type is meant. However, this can fail if the subquery has been optimized out of the plan altogether on the grounds that no rows could pass the WHERE quals, which has been possible at least since 3fc6e2d7f. There isn't anything remaining in the plan tree that would help us, so fall back to printing the field name as "fN" for the N'th column of the record. (This will actually be the right thing some of the time, since it matches the column names we assign to RowExprs.) In passing, fix a comment typo in create_projection_plan, which I noticed while experimenting with an alternative fix for this. Per bug #18576 from Vasya B. Back-patch to all supported branches. Richard Guo and Tom Lane Discussion: https://postgr.es/m/18576-9feac34e132fea9e@postgresql.org
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/rowtypes.out54
-rw-r--r--src/test/regress/sql/rowtypes.sql21
2 files changed, 75 insertions, 0 deletions
diff --git a/src/test/regress/expected/rowtypes.out b/src/test/regress/expected/rowtypes.out
index 17ce98c58cd..573bc7f00fa 100644
--- a/src/test/regress/expected/rowtypes.out
+++ b/src/test/regress/expected/rowtypes.out
@@ -1264,6 +1264,60 @@ select pg_get_viewdef('composite_v', true);
drop view composite_v;
--
+-- Check cases where the composite comes from a proven-dummy rel (bug #18576)
+--
+explain (verbose, costs off)
+select (ss.a).x, (ss.a).n from
+ (select information_schema._pg_expandarray(array[1,2]) AS a) ss;
+ QUERY PLAN
+------------------------------------------------------------------------
+ Subquery Scan on ss
+ Output: (ss.a).x, (ss.a).n
+ -> ProjectSet
+ Output: information_schema._pg_expandarray('{1,2}'::integer[])
+ -> Result
+(5 rows)
+
+explain (verbose, costs off)
+select (ss.a).x, (ss.a).n from
+ (select information_schema._pg_expandarray(array[1,2]) AS a) ss
+where false;
+ QUERY PLAN
+--------------------------
+ Result
+ Output: (a).f1, (a).f2
+ One-Time Filter: false
+(3 rows)
+
+explain (verbose, costs off)
+with cte(c) as materialized (select row(1, 2)),
+ cte2(c) as (select * from cte)
+select (c).f1 from cte2 as t;
+ QUERY PLAN
+-----------------------------------
+ CTE Scan on cte
+ Output: (cte.c).f1
+ CTE cte
+ -> Result
+ Output: '(1,2)'::record
+(5 rows)
+
+explain (verbose, costs off)
+with cte(c) as materialized (select row(1, 2)),
+ cte2(c) as (select * from cte)
+select (c).f1 from cte2 as t
+where false;
+ QUERY PLAN
+-----------------------------------
+ Result
+ Output: (cte.c).f1
+ One-Time Filter: false
+ CTE cte
+ -> Result
+ Output: '(1,2)'::record
+(6 rows)
+
+--
-- Tests for component access / FieldSelect
--
CREATE TABLE compositetable(a text, b text);
diff --git a/src/test/regress/sql/rowtypes.sql b/src/test/regress/sql/rowtypes.sql
index 72c9df0d7d8..9770681b80d 100644
--- a/src/test/regress/sql/rowtypes.sql
+++ b/src/test/regress/sql/rowtypes.sql
@@ -510,6 +510,27 @@ select pg_get_viewdef('composite_v', true);
drop view composite_v;
--
+-- Check cases where the composite comes from a proven-dummy rel (bug #18576)
+--
+explain (verbose, costs off)
+select (ss.a).x, (ss.a).n from
+ (select information_schema._pg_expandarray(array[1,2]) AS a) ss;
+explain (verbose, costs off)
+select (ss.a).x, (ss.a).n from
+ (select information_schema._pg_expandarray(array[1,2]) AS a) ss
+where false;
+
+explain (verbose, costs off)
+with cte(c) as materialized (select row(1, 2)),
+ cte2(c) as (select * from cte)
+select (c).f1 from cte2 as t;
+explain (verbose, costs off)
+with cte(c) as materialized (select row(1, 2)),
+ cte2(c) as (select * from cte)
+select (c).f1 from cte2 as t
+where false;
+
+--
-- Tests for component access / FieldSelect
--
CREATE TABLE compositetable(a text, b text);