| From: | Daniel Migowski <dmigowski(at)ikoffice(dot)de> |
|---|---|
| To: | "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org> |
| Cc: | "alpkeles99(at)gmail(dot)com" <alpkeles99(at)gmail(dot)com>, Christoph Mühleisen <cmuehleisen(at)ikoffice(dot)de> |
| Subject: | Re: BUG #19787: "could not find pathkey item to sort" for Gather Merge over a merge join below reordered LEFT JOINs |
| Date: | 2026-11-23 16:44:57 |
| Message-ID: | FR2PPF74E3D276D87F90D1DDB5C2562DD7DB9452@FR2PPF74E3D276D.DEUP281.PROD.OUTLOOK.COM |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
Hi,
We hit the same error in production on 18.6: an application view that
starts with a RIGHT JOIN followed by a long chain of LEFT JOINs fails
with "could not find pathkey item to sort" as soon as all of its
columns are selected; even EXPLAIN fails. The reporter's analysis
matches what I see, so here is a patch.
For the reported query (RT indexes: a=1, b=2, a/b join=3, c=4, (a/b)/c
join=5, d=6), the {b,c,d} join rel is formed as (b LJ c) LJ d below the
a/b join. Since 9df8f903e its relid set is {2,4,6}, without 5, so its
joininfo still holds the clone "c.x = b.x{3}" (required_relids {2,3,4},
incompatible_relids {5}), although the other clone "c.x = b.x" has
already been applied inside the rel. The dead clone can never be
applied above, and c.x is not in the rel's reltarget (which is empty),
but pathkeys_useful_for_merging() matches the clone's left_ec with the
merge join's c.x pathkey. So truncate_useless_pathkeys() keeps that
pathkey, generate_gather_paths() puts a Gather Merge on the partial
path, and create_gather_merge_plan() can't find c.x in the subplan's
tlist.
The attached patch makes pathkeys_useful_for_merging() ignore a
joininfo clause whose other side references any of the rel's relids.
Such a clause can never become a mergeclause at a join above the rel,
because that side would have to come from the join's other input,
which is disjoint from the rel. That's the rule
eclass_useful_for_merging() already applies to EC members ("To join,
we need a member not in the given rel"), so the joininfo branch just
becomes consistent with it. For the dead clone above, the other side
b.x{3} has relids {2,3}, which overlap {2,4,6}.
Alternatives I considered:
* Keeping such dead clones out of the joinrel's joininfo in
subbuild_joinrel_joinlist(). That would need to know that another
clone has already been applied inside the rel, which joininfo
construction doesn't know, and joininfo has more consumers, so it
seemed riskier.
* Making generate_gather_paths() drop or truncate pathkeys that can't
be computed from the rel's reltarget. That would fix the symptom,
but the root cause is keeping the useless pathkey in the first place.
The patch adds the reporter's case to join.sql.
The bug came in with 9df8f903e, so the fix needs to go back to 16. The
pathkeys.c hunk is the same in all branches; only the test hunk's
context differs, so I'm also attaching versions for REL_16_STABLE to
REL_19_STABLE (as .txt, so cfbot doesn't try to apply them to master).
Testing (Windows, MSVC, meson, cassert except where noted):
* make check passes on all branches with the patch: master 243,
REL_19 243, REL_18 235 (release build), REL_17 229, REL_16 224 tests.
No existing expected output changes.
* With only the pathkeys.c change reverted, the new test fails on every
branch with "could not find pathkey item to sort", and join is the
only failing test.
* postgres_fdw, file_fdw and isolation tests pass on master.
* Our original view: I loaded its schema, the data of the 18 tables it
uses and the production planner statistics into a local 18.7 cluster.
Unpatched 18.7 fails on it with the same error for random_page_cost
between 7 and 10; with the patch it plans and executes fine across
random_page_cost 1.1 to 30, and Gather Merge is still used elsewhere
in the plan.
Regards,
Daniel Migowski
PS: This fix is fully made with Claude Opus 5.5 Extra High, so please check it manually, but at least it actually fixes the bug. Also Claude just seems to have duplicated a check that was done elsewhere before and forgotten to do here also, so the patch doesn't look too bad.
| Attachment | Content-Type | Size |
|---|---|---|
| REL_18-v1-0001-Fix-pathkeys_useful_for_merging-to-ignore-unusabl.patch.txt | text/plain | 7.7 KB |
| REL_19-v1-0001-Fix-pathkeys_useful_for_merging-to-ignore-unusabl.patch.txt | text/plain | 7.8 KB |
| v1-0001-Fix-pathkeys_useful_for_merging-to-ignore-unusabl.patch | application/octet-stream | 7.8 KB |
| REL_16-v1-0001-Fix-pathkeys_useful_for_merging-to-ignore-unusabl.patch.txt | text/plain | 7.7 KB |
| REL_17-v1-0001-Fix-pathkeys_useful_for_merging-to-ignore-unusabl.patch.txt | text/plain | 7.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Jan Nidzwetzki | 2026-11-23 15:31:39 | Re: pg_rewind and hint bits leads to visibility corruption |