MergeJoin fails on incomplete btree opfamily definition

From: Andrei Lepikhov <lepihov(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: MergeJoin fails on incomplete btree opfamily definition
Date: 2026-04-15 08:20:54
Message-ID: f6b5dcbb-2512-41ac-8c3b-c17088a4063c@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

While experimenting with chaotic cost assignment [1], I found that
equivclass.sql contains a query that sometimes fails with an error:

-- let's try that as a mergejoin
set enable_mergejoin = on;
set enable_nestloop = off;
explain (costs off)
select * from ec1,
(select ff + 1 as x from
...

The underlying problem is simple: an incomplete btree operator family.
That would be acceptable if the behaviour were consistent, but as the
attached self-contained SQL script shows, it is unstable and depends on
which sort direction the planner happens to choose for the merge join's
internal sort — the same catalogue state can yield either a valid plan
or an elog(ERROR) depending on cost-driven decisions.

The main reason to fix this: when the opfamily lacks the ordering
operator required to sort one side of a merge, the planner should skip
the MergeJoin strategy and fall back to another join method, rather than
constructing an unusable plan and crashing in
prepare_sort_from_pathkeys(). I have verified the issue reproduces on
current master. This code is unchanged for several releases, so it seems
worth fixing and back-patching.

The issue actually has two layers. The "missing operator" error is the
first one. There is a related "missing support function" error on the
same path — I'll discover it later if there is interest in this topic.
See the proposed fix for the current problem.

[1] https://github.com/danolivo/pg-chaos-test

--
regards, Andrei Lepikhov,
pgEdge

Attachment Content-Type Size
repro_layer_1.sql text/plain 1.9 KB
v0-0001-Skip-merge-join-paths-when-opfamily-lacks-orderin.patch text/plain 9.3 KB

Browse pgsql-bugs by date

  From Date Subject
Next Message Julien Tachoires 2026-04-15 10:57:18 Bug in CREATE TABLE .. LIKE .. INCLUDING STATISTICS?
Previous Message Thomas Munro 2026-04-15 06:59:16 Re: BUG #19354: JOHAB rejects valid byte sequences