1
0
mirror of https://github.com/postgres/postgres.git synced 2026-10-23 13:29:09 +03:00

Rename EXISTS-to-ANY converted subplan to exists_to_any

Simple EXISTS subplans can be converted to hashed ANY subplans as
an alternative implementation. Previously, both the original EXISTS
subplan and the converted ANY subplan used names with the
exists_ prefix, making EXPLAIN output harder to read and
pg_plan_advice targets less clear.

Use the exists_to_any_ prefix for converted ANY subplans so that
their names distinguish them from the original EXISTS alternative.
This only changes the names shown by EXPLAIN and used by plan advice;
it does not affect planner behavior.

Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: solai v <solai.cdac@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/20260605165641.3950f99ace0aad8f807abe96@sraoss.co.jp
Backpatch-through: 19
This commit is contained in:
Fujii Masao
2026-08-15 18:12:14 +09:00
parent 7e6e294e4e
commit 7b7c4a8dcc
5 changed files with 35 additions and 31 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -271,8 +271,12 @@ 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);
/*
* Generate Paths for the ANY subquery; we'll need all rows.
* Use a distinct prefix for this user-visible name, since this is
* an ANY implementation of the original EXISTS subplan.
*/
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);

View File

@@ -1967,15 +1967,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)

View File

@@ -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