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

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: 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 03:21:36
Message-ID: CAGRkXqTYUfyKOwAcp5RAMgY=UQh7rcTS=pi2OKV=6TrezSgCWA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Thanks for the report, BUG #19869 is the same one. I think it is a
real bug.

A btree opfamily needs its "=" to agree with its ordering. The
pattern_ops opfamilies sort bytewise, but their "=" follows the
collation. With a nondeterministic collation, 'A' ~<~ 'a' and
'A' = 'a' are both true. Anything that sorts and then tests for
equality goes wrong. With your table t, this returns 4 and the right
answer is 8:

CREATE TABLE t2 (y text COLLATE ci);
INSERT INTO t2 VALUES ('a'), ('B');
SET enable_hashjoin = off; SET enable_nestloop = off;
SELECT count(*) FROM (SELECT x FROM t JOIN t2 ON x = y
ORDER BY x USING ~<~) s;

GROUP BY and DISTINCT break the same way.

Commit 2810396312 kept a check for this only in CREATE INDEX. The
attached patch adds the same check to ORDER BY. Its hint points to
COLLATE "C", which sorts the same way. Both checks now use one small
function for the list of opfamilies. The partition key patch in [1]
could use it too.

Such queries mostly work today and views using them would no longer
restore, so I'd suggest HEAD only.

[1]https://postgr.es/m/19645-60a963f2e91478e0@postgresql.org

Thanks,
Shihao

Attachment Content-Type Size
v1-0002-Add-tests-for-pattern_ops-operators-with-nondetermin.patch application/octet-stream 5.0 KB
nocfbot-v1-REL_15.patch application/octet-stream 12.9 KB
v1-0001-Reject-pattern_ops-ordering-operators-for-nondetermi.patch application/octet-stream 7.5 KB
nocfbot-v1-REL_18.patch application/octet-stream 12.5 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2026-11-24 03:32:10 Re: Segfaults in 18 and 19 when pgstats can't grow its DSA (back-patch of d9de60c5e4)
Previous Message Fujii Masao 2026-11-24 02:43:17 Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off