summaryrefslogtreecommitdiff
path: root/src/test/regress
diff options
context:
space:
mode:
authorTom Lane2022-04-21 21:58:52 +0000
committerTom Lane2022-04-21 21:58:52 +0000
commite6440d5007aaa33f946ab63f2c1dfd1ee99e2050 (patch)
tree743d7ca7aa57af4db697e4d41fd5d6f8f1021afc /src/test/regress
parent1c60a99dd9a2064125d503227d8f8b17d33dc4ed (diff)
Remove inadequate assertion check in CTE inlining.
inline_cte() expected to find exactly as many references to the target CTE as its cterefcount indicates. While that should be accurate for the tree as emitted by the parser, there are some optimizations that occur upstream of here that could falsify it, notably removal of unused subquery output expressions. Trying to make the accounting 100% accurate seems expensive and doomed to future breakage. It's not really worth it, because all this code is protecting is downstream assumptions that every referenced CTE has a plan. Let's convert those assertions to regular test-and-elog just in case there's some actual problem, and then drop the failing assertion. Per report from Tomas Vondra (thanks also to Richard Guo for analysis). Back-patch to v12 where the faulty code came in. Discussion: https://postgr.es/m/29196a1e-ed47-c7ca-9be2-b1c636816183@enterprisedb.com
Diffstat (limited to 'src/test/regress')
-rw-r--r--src/test/regress/expected/with.out64
-rw-r--r--src/test/regress/sql/with.sql31
2 files changed, 95 insertions, 0 deletions
diff --git a/src/test/regress/expected/with.out b/src/test/regress/expected/with.out
index 04a942993d6..63bbef0677e 100644
--- a/src/test/regress/expected/with.out
+++ b/src/test/regress/expected/with.out
@@ -1766,6 +1766,70 @@ SELECT * FROM bug6051_3;
---
(0 rows)
+-- check case where CTE reference is removed due to optimization
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT q1 FROM
+(
+ WITH t_cte AS (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+ QUERY PLAN
+--------------------------------------
+ Subquery Scan on ss
+ Output: ss.q1
+ -> Seq Scan on public.int8_tbl i8
+ Output: i8.q1, NULL::bigint
+(4 rows)
+
+SELECT q1 FROM
+(
+ WITH t_cte AS (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+ q1
+------------------
+ 123
+ 123
+ 4567890123456789
+ 4567890123456789
+ 4567890123456789
+(5 rows)
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT q1 FROM
+(
+ WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+ QUERY PLAN
+---------------------------------------------
+ Subquery Scan on ss
+ Output: ss.q1
+ -> Seq Scan on public.int8_tbl i8
+ Output: i8.q1, NULL::bigint
+ CTE t_cte
+ -> Seq Scan on public.int8_tbl t
+ Output: t.q1, t.q2
+(7 rows)
+
+SELECT q1 FROM
+(
+ WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+ q1
+------------------
+ 123
+ 123
+ 4567890123456789
+ 4567890123456789
+ 4567890123456789
+(5 rows)
+
-- a truly recursive CTE in the same list
WITH RECURSIVE t(a) AS (
SELECT 0
diff --git a/src/test/regress/sql/with.sql b/src/test/regress/sql/with.sql
index 75b310cac5d..d2c4c174a45 100644
--- a/src/test/regress/sql/with.sql
+++ b/src/test/regress/sql/with.sql
@@ -842,6 +842,37 @@ COMMIT;
SELECT * FROM bug6051_3;
+-- check case where CTE reference is removed due to optimization
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT q1 FROM
+(
+ WITH t_cte AS (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+
+SELECT q1 FROM
+(
+ WITH t_cte AS (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+
+EXPLAIN (VERBOSE, COSTS OFF)
+SELECT q1 FROM
+(
+ WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+
+SELECT q1 FROM
+(
+ WITH t_cte AS MATERIALIZED (SELECT * FROM int8_tbl t)
+ SELECT q1, (SELECT q2 FROM t_cte WHERE t_cte.q1 = i8.q1) AS t_sub
+ FROM int8_tbl i8
+) ss;
+
-- a truly recursive CTE in the same list
WITH RECURSIVE t(a) AS (
SELECT 0