From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Daniel Migowski Date: Mon, 23 Nov 2026 16:58:51 +0100 Subject: [PATCH REL_17 v1] Fix pathkeys_useful_for_merging() to ignore unusable join clauses pathkeys_useful_for_merging() considered a pathkey useful for merging if it matched either side of any mergejoinable clause in the rel's joininfo list. However, after outer joins have been commuted per outer join identity 3, the joininfo list of a join rel that has performed the pushed-down join(s) can still contain clone versions of their join clauses, because the rel's relid set does not include those outer joins yet (see commit 9df8f903e). Such clones will never be applied above the rel, and their Vars need not be emitted by it. Counting them as useful could keep pathkeys that cannot be computed from the rel's reltarget, and if a Gather Merge was then put atop such a partial path, createplan.c failed with "could not find pathkey item to sort". A joininfo clause can serve as a mergeclause at some join above the rel only if its other side can be computed from the other input of that join, which is impossible if that side references any of the rel's relids. So ignore the clause in that case, just as eclass_useful_for_merging() ignores EquivalenceClass members that overlap the rel. Bug: #19787 Reported-by: Alperen Keles Discussion: https://postgr.es/m/19787-d14d7f0f320f4495@postgresql.org Backpatch-through: 16 Co-Authored-By: Claude Opus 5.5 --- src/backend/optimizer/path/pathkeys.c | 34 +++++++++++++++---- src/test/regress/expected/join.out | 49 +++++++++++++++++++++++++++ src/test/regress/sql/join.sql | 32 +++++++++++++++++ 3 files changed, 109 insertions(+), 6 deletions(-) diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c index 8b0244b765..5a96b20bfc 100644 --- a/src/backend/optimizer/path/pathkeys.c +++ b/src/backend/optimizer/path/pathkeys.c @@ -2025,10 +2025,11 @@ trim_mergeclauses_for_inner_pathkeys(PlannerInfo *root, * above the given relation. * * We consider a pathkey potentially useful if it corresponds to the merge - * ordering of either side of any joinclause for the rel. This might be - * overoptimistic, since joinclauses that require different other relations - * might never be usable at the same time, but trying to be exact is likely - * to be more trouble than it's worth. + * ordering of either side of any joinclause for the rel whose other side + * does not reference the rel. This might be overoptimistic, since + * joinclauses that require different other relations might never be usable + * at the same time, but trying to be exact is likely to be more trouble than + * it's worth. * * To avoid doubling the number of mergejoin paths considered, we would like * to consider only one of the two scan directions (ASC or DESC) as useful @@ -2076,8 +2077,29 @@ pathkeys_useful_for_merging(PlannerInfo *root, RelOptInfo *rel, List *pathkeys) continue; update_mergeclause_eclasses(root, restrictinfo); - if (pathkey->pk_eclass == restrictinfo->left_ec || - pathkey->pk_eclass == restrictinfo->right_ec) + /* + * To be usable as a mergeclause at some join above this rel, + * the clause must have the pathkey's EC on one side, and its + * other side must be computable from the other input of that + * join. The latter is impossible if the other side refers to + * any of this rel's relids, so ignore the clause then, just + * as eclass_useful_for_merging ignores EC members that + * overlap the rel. + * + * This is not just an optimization. After outer joins have + * been commuted per identity 3, the joininfo list of a join + * rel that has performed the pushed-down join(s) can still + * contain clone versions of their clauses that will never be + * applied (see subbuild_joinrel_restrictlist), and whose Vars + * need not be available from the rel's reltarget. Treating + * such a clause as useful could leave us with pathkeys that + * cannot be computed from the rel's output, which would + * cause failures if, say, a Gather Merge is put atop the path. + */ + if ((pathkey->pk_eclass == restrictinfo->left_ec && + !bms_overlap(restrictinfo->right_relids, rel->relids)) || + (pathkey->pk_eclass == restrictinfo->right_ec && + !bms_overlap(restrictinfo->left_relids, rel->relids))) { matched = true; break; diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index 59a2a16cc5..16db8f9714 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -6221,6 +6221,55 @@ where s.y is not null; -> Seq Scan on int4_tbl t5 (14 rows) +-- +-- check that clone variants of outer-join quals that remain in the joininfo +-- of a join rel formed by commuted outer joins, but can never be applied +-- above it, do not make its pathkeys look useful for merging (bug #19787) +-- +begin; +create table oj_t1 (x int); +create table oj_t2 (x int); +create table oj_t3 (x int); +create table oj_t4 (x int); +analyze oj_t2, oj_t4; +-- encourage use of a parallel merge join below the commuted outer joins +set local cpu_tuple_cost = 1; +set local parallel_tuple_cost = 0; +set local min_parallel_table_scan_size = 0; +explain (costs off) +select 1 from oj_t1 t1 + left join oj_t2 t2 on true + left join oj_t3 t3 on t3.x = t2.x + left join oj_t4 t4 on t4.x = t3.x; + QUERY PLAN +------------------------------------------------------------------- + Nested Loop Left Join + -> Gather + Workers Planned: 2 + -> Parallel Seq Scan on oj_t1 t1 + -> Materialize + -> Hash Left Join + Hash Cond: (t3.x = t4.x) + -> Gather + Workers Planned: 2 + -> Parallel Hash Right Join + Hash Cond: (t3.x = t2.x) + -> Parallel Seq Scan on oj_t3 t3 + -> Parallel Hash + -> Parallel Seq Scan on oj_t2 t2 + -> Hash + -> Seq Scan on oj_t4 t4 +(16 rows) + +select 1 from oj_t1 t1 + left join oj_t2 t2 on true + left join oj_t3 t3 on t3.x = t2.x + left join oj_t4 t4 on t4.x = t3.x; + ?column? +---------- +(0 rows) + +rollback; -- -- test successful handling of full join underneath left join (bug #14105) -- diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 8289e8da09..c2ee79a12f 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -2214,6 +2214,38 @@ select 1 from int4_tbl t1 join int4_tbl t5 on true where s.y is not null; +-- +-- check that clone variants of outer-join quals that remain in the joininfo +-- of a join rel formed by commuted outer joins, but can never be applied +-- above it, do not make its pathkeys look useful for merging (bug #19787) +-- + +begin; + +create table oj_t1 (x int); +create table oj_t2 (x int); +create table oj_t3 (x int); +create table oj_t4 (x int); +analyze oj_t2, oj_t4; + +-- encourage use of a parallel merge join below the commuted outer joins +set local cpu_tuple_cost = 1; +set local parallel_tuple_cost = 0; +set local min_parallel_table_scan_size = 0; + +explain (costs off) +select 1 from oj_t1 t1 + left join oj_t2 t2 on true + left join oj_t3 t3 on t3.x = t2.x + left join oj_t4 t4 on t4.x = t3.x; + +select 1 from oj_t1 t1 + left join oj_t2 t2 on true + left join oj_t3 t3 on t3.x = t2.x + left join oj_t4 t4 on t4.x = t3.x; + +rollback; + -- -- test successful handling of full join underneath left join (bug #14105) --