Re: BUG #19873: planner silently drops the second ORDER BY sort key when both keys reference the same expression und

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: shihao zhong <zhong950419(at)gmail(dot)com>
Cc: 303677365(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19873: planner silently drops the second ORDER BY sort key when both keys reference the same expression und
Date: 2026-11-24 19:31:01
Message-ID: 1024707.1795548661@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

shihao zhong <zhong950419(at)gmail(dot)com> writes:
> Thanks for the report, BUG #19869 is the same one. I think it is a
> real bug.

I agree it's a bug, but your patch is solving a different problem.
Specifically, this patch prevents reaching the bug for the specific
case of the btree_pattern_ops operators, but it does nothing for
other operators, for which this type of query might be more sensible.

I concur with the submitter's opinion that what we really ought to
do is not merge pathkeys that have the same expression and different
collations when the earlier one has a nondeterministic collation.
Not sure how hard it is to make that happen, but that seems like the
Right Thing. The idea that we could merge such cases is founded
precisely on the assumption that the earlier sort column will only
report equality for bitwise-equal strings, so it's one of the
assumptions that nondeterministic collations break.

I'm also not convinced that we need to or should block use of ~<~ in
ORDER BY etc with a nondeterministic collation. AFAICS, specifying
that results in a C-locale sort regardless of the specified collation
for the target expression, and that's fine. The inconsistency in the
btree_pattern_ops opclass doesn't affect using it for sorting, because
at no point will a sort operation invoke the opclass's "=" member.
We blocked this for indexes because you'd get the wrong answers from
an "=" indexscan, but I don't see that there's a hazard for ORDER BY.

(Even if I thought we needed to block this, I'm not very happy
about the number of usually-useless catalog lookups that this
patch adds to parsing ORDER BY etc. But I question the need
to block it at all.)

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Daniel Gustafsson 2026-11-24 19:29:34 Re: BUG #19354: JOHAB rejects valid byte sequences