Re: BUG #19645: Partition key opclass bypasses nondeterministic collation check, wrong results

From: Zexin Li <lizi(dot)openmind(at)gmail(dot)com>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: jj-zhang25(at)mails(dot)tsinghua(dot)edu(dot)cn
Subject: Re: BUG #19645: Partition key opclass bypasses nondeterministic collation check, wrong results
Date: 2026-09-02 06:28:46
Message-ID: CAAP6ZkR6fRtDqW=c=+bPEHPzQOtFioXjqgGtT6r6Ycc7WKs1zQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Sat, Aug 29, 2026 at 2:39 PM PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
> Apply the existing check to partition key expressions: when a partition key
> specifies an operator class or a collation that differs from the column's,
> and either collation is nondeterministic, refuse it with the message already
> used for indexes and exclusion constraints.

I can reproduce this on HEAD and on 16.13. As far as I can tell, the
check that index_create() applies to the pattern_ops opclasses (commit
2810396312) was never added to ComputePartitionAttrs(), so tuple
routing and partition pruning both use the opclass's bytewise
comparison while texteq follows the column's nondeterministic
collation.

I'm not sure the check needs to be as broad as suggested, though. In
my testing, PARTITION BY (c COLLATE "C") over a column with a
nondeterministic collation gives correct results: the qual's collation
no longer matches the partition key's, so PartCollMatchesExprColl()
rejects it and nothing gets pruned. HASH with text_pattern_ops also
seems fine, since that family hashes with the collation-aware
hashtext. So the attached only moves the existing check into a
helper, CheckOpclassCollation(), and calls it from
ComputePartitionAttrs() as well; it checks the same three btree
opclasses as before and nothing else. I may well be missing a case.
A regression test is included; it fails without the fix.

One consequence is that an existing table with such a partition key
would no longer restore from a dump. Those tables were returning
wrong answers anyway.

About the December 2023 thread you mention: as far as I can see it
became commit a11c9c42ea, which compares a unique/PK/exclusion index's
collation against the partition key inside DefineIndex(). It doesn't
look at the partition key's own opclass; on 16.13 the table above is
created without complaint, and a unique index over the key is
accepted as well, since both sides carry the same collation. So I
don't think it reaches this case, but I'd be glad to be corrected.

Regards,
Zexin Li

Attachment Content-Type Size
0001-Check-partition-key-opclass-against-nondeterministic.patch application/x-patch 9.4 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Fujii Masao 2026-09-02 11:01:46 Re: BUG #19441: Backend waits for serializable snapshot indefinitely on removing temp relations
Previous Message Tom Lane 2026-09-02 03:15:59 Re: BUG #19644: byteaout, float8out and float4out are marked IMMUTABLE but depend on GUCs