Hi,
While looking at alternatives.out in the regression test of pg_plan_advice,
I was a bit confused because there are exists_1 and exists_2 in the generated
plan advice even though the query contains only one EXISTS.
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM alt_t1
WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
QUERY PLAN
-------------------------------------------------------------------
Seq Scan on alt_t1
Filter: ((ANY (a = (hashed SubPlan exists_2).col1)) OR (a < 0))
SubPlan exists_2
-> Seq Scan on alt_t2
Generated Plan Advice:
SEQ_SCAN(alt_t1 alt_t2@exists_2)
NO_GATHER(alt_t1 alt_t2@exists_2)
DO_NOT_SCAN(alt_t2@exists_1)
(8 rows)
I realized that when an EXISTS is simple, it is converted into an additional
ANY subplan. However, it seems slightly confusing that the prefix "exists_"
is the same as the original EXISTS subplan, especially when using
pg_plan_advice to control the plan.
I am wondering whether it might be worth renaming the subplan to something
like "exists_to_any", to make it clearer that it is an ANY subplan converted
from EXISTS rather than the original EXISTS. I’ve attached a patch in this
direction.
What do you think?
--
Yugo Nagata <[email protected]>
>From 38185ed69e710b189c1eafcf295fba210f29dfd6 Mon Sep 17 00:00:00 2001
From: Yugo Nagata <[email protected]>
Date: Mon, 25 May 2026 10:16:23 +0900
Subject: [PATCH] Rename EXISTS-to-ANY converted subplan to exists_to_any
---
.../pg_plan_advice/expected/alternatives.out | 30 +++++++++----------
contrib/pg_plan_advice/sql/alternatives.sql | 2 +-
src/backend/optimizer/plan/subselect.c | 2 +-
src/test/regress/expected/partition_prune.out | 10 +++----
src/test/regress/expected/subselect.out | 16 +++++-----
5 files changed, 30 insertions(+), 30 deletions(-)
diff --git a/contrib/pg_plan_advice/expected/alternatives.out b/contrib/pg_plan_advice/expected/alternatives.out
index a6fb296d4b4..94bfcf3d0a2 100644
--- a/contrib/pg_plan_advice/expected/alternatives.out
+++ b/contrib/pg_plan_advice/expected/alternatives.out
@@ -12,15 +12,15 @@ VACUUM ANALYZE alt_t2;
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM alt_t1
WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
- QUERY PLAN
--------------------------------------------------------------------
+ QUERY PLAN
+--------------------------------------------------------------------------
Seq Scan on alt_t1
- Filter: ((ANY (a = (hashed SubPlan exists_2).col1)) OR (a < 0))
- SubPlan exists_2
+ Filter: ((ANY (a = (hashed SubPlan exists_to_any_1).col1)) OR (a < 0))
+ SubPlan exists_to_any_1
-> Seq Scan on alt_t2
Generated Plan Advice:
- SEQ_SCAN(alt_t1 alt_t2@exists_2)
- NO_GATHER(alt_t1 alt_t2@exists_2)
+ SEQ_SCAN(alt_t1 alt_t2@exists_to_any_1)
+ NO_GATHER(alt_t1 alt_t2@exists_to_any_1)
DO_NOT_SCAN(alt_t2@exists_1)
(8 rows)
@@ -31,21 +31,21 @@ SET LOCAL pg_plan_advice.advice = 'DO_NOT_SCAN(alt_t2@exists_1)';
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM alt_t1
WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
- QUERY PLAN
--------------------------------------------------------------------
+ QUERY PLAN
+--------------------------------------------------------------------------
Seq Scan on alt_t1
- Filter: ((ANY (a = (hashed SubPlan exists_2).col1)) OR (a < 0))
- SubPlan exists_2
+ Filter: ((ANY (a = (hashed SubPlan exists_to_any_1).col1)) OR (a < 0))
+ SubPlan exists_to_any_1
-> Seq Scan on alt_t2
Supplied Plan Advice:
DO_NOT_SCAN(alt_t2@exists_1) /* matched */
Generated Plan Advice:
- SEQ_SCAN(alt_t1 alt_t2@exists_2)
- NO_GATHER(alt_t1 alt_t2@exists_2)
+ SEQ_SCAN(alt_t1 alt_t2@exists_to_any_1)
+ NO_GATHER(alt_t1 alt_t2@exists_to_any_1)
DO_NOT_SCAN(alt_t2@exists_1)
(10 rows)
-SET LOCAL pg_plan_advice.advice = 'DO_NOT_SCAN(alt_t2@exists_2)';
+SET LOCAL pg_plan_advice.advice = 'DO_NOT_SCAN(alt_t2@exists_to_any_1)';
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM alt_t1
WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
@@ -57,12 +57,12 @@ WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
-> Index Only Scan using alt_t2_a_idx on alt_t2
Index Cond: (a = alt_t1.a)
Supplied Plan Advice:
- DO_NOT_SCAN(alt_t2@exists_2) /* matched */
+ DO_NOT_SCAN(alt_t2@exists_to_any_1) /* matched */
Generated Plan Advice:
SEQ_SCAN(alt_t1)
INDEX_ONLY_SCAN(alt_t2@exists_1 public.alt_t2_a_idx)
NO_GATHER(alt_t1 alt_t2@exists_1)
- DO_NOT_SCAN(alt_t2@exists_2)
+ DO_NOT_SCAN(alt_t2@exists_to_any_1)
(12 rows)
COMMIT;
diff --git a/contrib/pg_plan_advice/sql/alternatives.sql b/contrib/pg_plan_advice/sql/alternatives.sql
index 16299edd196..f45f7083882 100644
--- a/contrib/pg_plan_advice/sql/alternatives.sql
+++ b/contrib/pg_plan_advice/sql/alternatives.sql
@@ -22,7 +22,7 @@ SET LOCAL pg_plan_advice.advice = 'DO_NOT_SCAN(alt_t2@exists_1)';
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM alt_t1
WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
-SET LOCAL pg_plan_advice.advice = 'DO_NOT_SCAN(alt_t2@exists_2)';
+SET LOCAL pg_plan_advice.advice = 'DO_NOT_SCAN(alt_t2@exists_to_any_1)';
EXPLAIN (COSTS OFF, PLAN_ADVICE)
SELECT * FROM alt_t1
WHERE EXISTS (SELECT 1 FROM alt_t2 WHERE alt_t2.a = alt_t1.a) OR alt_t1.a < 0;
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index ccec1eaa7fe..cf5bfa04ad5 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -272,7 +272,7 @@ make_subplan(PlannerInfo *root, Query *orig_subquery,
char *plan_name;
/* Generate Paths for the ANY subquery; we'll need all rows */
- plan_name = choose_plan_name(root->glob, sublinkstr, true);
+ plan_name = choose_plan_name(root->glob, "exists_to_any", true);
subroot = subquery_planner(root->glob, subquery, plan_name,
root, subroot, false, 0.0, NULL);
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out
index 849049f9c51..aac89fd9d9b 100644
--- a/src/test/regress/expected/partition_prune.out
+++ b/src/test/regress/expected/partition_prune.out
@@ -1921,15 +1921,15 @@ where asptab.id > ss.b::int;
-> Seq Scan on int4_tbl touter
-> Append
-> Index Only Scan using asptab0_pkey on asptab0 asptab_1
- Index Cond: (id > (EXISTS(SubPlan exists_3))::integer)
- SubPlan exists_4
+ Index Cond: (id > (EXISTS(SubPlan exists_2))::integer)
+ SubPlan exists_to_any_2
-> Seq Scan on int4_tbl tinner_2
-> Index Only Scan using asptab1_pkey on asptab1 asptab_2
- Index Cond: (id > (EXISTS(SubPlan exists_3))::integer)
- SubPlan exists_3
+ Index Cond: (id > (EXISTS(SubPlan exists_2))::integer)
+ SubPlan exists_2
-> Seq Scan on int4_tbl tinner_1
Filter: (f1 = touter.f1)
- SubPlan exists_2
+ SubPlan exists_to_any_1
-> Seq Scan on int4_tbl tinner
(14 rows)
diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out
index a3778c23c34..af4aa78a406 100644
--- a/src/test/regress/expected/subselect.out
+++ b/src/test/regress/expected/subselect.out
@@ -1253,12 +1253,12 @@ rollback; -- to get rid of the bogus operator
explain (costs off)
select count(*) from tenk1 t
where (exists(select 1 from tenk1 k where k.unique1 = t.unique2) or ten < 0);
- QUERY PLAN
----------------------------------------------------------------------------------
+ QUERY PLAN
+----------------------------------------------------------------------------------------
Aggregate
-> Seq Scan on tenk1 t
- Filter: ((ANY (unique2 = (hashed SubPlan exists_2).col1)) OR (ten < 0))
- SubPlan exists_2
+ Filter: ((ANY (unique2 = (hashed SubPlan exists_to_any_1).col1)) OR (ten < 0))
+ SubPlan exists_to_any_1
-> Index Only Scan using tenk1_unique1 on tenk1 k
(5 rows)
@@ -1303,8 +1303,8 @@ analyze exists_tbl;
explain (costs off)
select * from exists_tbl t1
where (exists(select 1 from exists_tbl t2 where t1.c1 = t2.c2) or c3 < 0);
- QUERY PLAN
----------------------------------------------------------------------------
+ QUERY PLAN
+----------------------------------------------------------------------------------
Append
-> Seq Scan on exists_tbl_null t1_1
Filter: (EXISTS(SubPlan exists_1) OR (c3 < 0))
@@ -1315,8 +1315,8 @@ select * from exists_tbl t1
-> Seq Scan on exists_tbl_def t2_2
Filter: (t1_1.c1 = c2)
-> Seq Scan on exists_tbl_def t1_2
- Filter: ((ANY (c1 = (hashed SubPlan exists_2).col1)) OR (c3 < 0))
- SubPlan exists_2
+ Filter: ((ANY (c1 = (hashed SubPlan exists_to_any_1).col1)) OR (c3 < 0))
+ SubPlan exists_to_any_1
-> Append
-> Seq Scan on exists_tbl_null t2_4
-> Seq Scan on exists_tbl_def t2_5
--
2.43.0