Check collation when creating partitioned index
authorPeter Eisentraut <[email protected]>
Fri, 1 Dec 2023 14:48:06 +0000 (15:48 +0100)
committerPeter Eisentraut <[email protected]>
Fri, 1 Dec 2023 15:05:41 +0000 (16:05 +0100)
When creating a partitioned index, the partition key must be a subset
of the index's columns.  But this currently doesn't check that the
collations between the partition key and the index definition match.
So you can construct a unique index that fails to enforce uniqueness.
(This would most likely involve a nondeterministic collation, so it
would have to be crafted explicitly and is not something that would
just happen by accident.)

This patch adds the required collation check.  As a result, any
previously allowed unique index that has a collation mismatch would no
longer be allowed to be created.

Reviewed-by: Tom Lane <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/3327cb54-f7f1-413b-8fdb-7a9dceebb938%40eisentraut.org

src/backend/commands/indexcmds.c

index 0b3b8e98b80396e26a2572d8a4f002f914d31ad7..c7ecedbe3bc00e5ba7c0dc63484db01c8cf15ba4 100644 (file)
@@ -1011,10 +1011,13 @@ DefineIndex(Oid tableId,
            {
                if (key->partattrs[i] == indexInfo->ii_IndexAttrNumbers[j])
                {
-                   /* Matched the column, now what about the equality op? */
+                   /* Matched the column, now what about the collation and equality op? */
                    Oid         idx_opfamily;
                    Oid         idx_opcintype;
 
+                   if (key->partcollation[i] != collationIds[j])
+                       continue;
+
                    if (get_opclass_opfamily_and_input_type(opclassIds[j],
                                                            &idx_opfamily,
                                                            &idx_opcintype))