From 5c64f656dd89633ac5f15d6ce182d0f8f175521b Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Mon, 12 Oct 2026 03:08:33 -0400 Subject: [PATCH v1 3/3] Add tests for planner restart after join removal. Cover aggpresorted flags left over from the first pass, and a removed rel that keeps its TABLESAMPLE arguments. --- src/test/regress/expected/join.out | 30 ++++++++++++++++++++++++++++++ src/test/regress/sql/join.sql | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index 4ee94d00ade..fed66d17a13 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -12137,3 +12137,33 @@ SELECT COUNT(*) FROM onek t1 LEFT JOIN tenk1 t2 19000 (1 row) +-- Check that join removal restarts do not keep stale planner state. +-- Self-join elimination changes which aggregates can use presorted input. +CREATE TABLE sje_agg (id int PRIMARY KEY, p int, q int); +INSERT INTO sje_agg SELECT i, i % 3, (i * 7) % 10 FROM generate_series(1, 30) i; +ANALYZE sje_agg; +SELECT count(DISTINCT a.p) AS dp, count(DISTINCT a.q) AS dq, + sum(DISTINCT a2.q) AS sq +FROM sje_agg a JOIN sje_agg a2 ON a.id = a2.id; + dp | dq | sq +----+----+---- + 3 | 10 | 45 +(1 row) + +DROP TABLE sje_agg; +-- A removed rel must not keep its TABLESAMPLE arguments. +CREATE TABLE jr_t (id int PRIMARY KEY, x int, y int); +CREATE TABLE jr_o (k int PRIMARY KEY, v int); +EXPLAIN (COSTS OFF) +SELECT a.id +FROM jr_t a + LEFT JOIN jr_o b2 ON b2.k = a.y + LEFT JOIN LATERAL (SELECT * FROM jr_o c + TABLESAMPLE BERNOULLI (coalesce(b2.v, 0) + 100)) s + ON s.k = a.x; + QUERY PLAN +-------------------- + Seq Scan on jr_t a +(1 row) + +DROP TABLE jr_t, jr_o; diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index bf8153afbcd..293c3003f44 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -4633,3 +4633,25 @@ SELECT COUNT(*) FROM onek t1 LEFT JOIN tenk1 t2 ON (t2.thousand = t1.tenthous OR t2.thousand = t1.thousand); SELECT COUNT(*) FROM onek t1 LEFT JOIN tenk1 t2 ON (t2.thousand = t1.tenthous OR t2.thousand = t1.thousand); + +-- Check that join removal restarts do not keep stale planner state. +-- Self-join elimination changes which aggregates can use presorted input. +CREATE TABLE sje_agg (id int PRIMARY KEY, p int, q int); +INSERT INTO sje_agg SELECT i, i % 3, (i * 7) % 10 FROM generate_series(1, 30) i; +ANALYZE sje_agg; +SELECT count(DISTINCT a.p) AS dp, count(DISTINCT a.q) AS dq, + sum(DISTINCT a2.q) AS sq +FROM sje_agg a JOIN sje_agg a2 ON a.id = a2.id; +DROP TABLE sje_agg; + +-- A removed rel must not keep its TABLESAMPLE arguments. +CREATE TABLE jr_t (id int PRIMARY KEY, x int, y int); +CREATE TABLE jr_o (k int PRIMARY KEY, v int); +EXPLAIN (COSTS OFF) +SELECT a.id +FROM jr_t a + LEFT JOIN jr_o b2 ON b2.k = a.y + LEFT JOIN LATERAL (SELECT * FROM jr_o c + TABLESAMPLE BERNOULLI (coalesce(b2.v, 0) + 100)) s + ON s.k = a.x; +DROP TABLE jr_t, jr_o; -- 2.37.1 (Apple Git-137.1)