pgsql: Disallow non-default collation in ADD PRIMARY KEY/UNIQUE USING I - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Disallow non-default collation in ADD PRIMARY KEY/UNIQUE USING I
Date
Msg-id E1idGPs-0000Rs-OA@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Disallow non-default collation in ADD PRIMARY KEY/UNIQUE USING INDEX.

When creating a uniqueness constraint using a pre-existing index,
we have always required that the index have the same properties you'd
get if you just let a new index get built.  However, when collations
were added, we forgot to add the index's collation to that check.

It's hard to trip over this without intentionally trying to break it:
you'd have to explicitly specify a different collation in CREATE
INDEX, then convert it to a pkey or unique constraint.  Still, if you
did that, pg_dump would emit a script that fails to reproduce the
index's collation.  The main practical problem is that after a
pg_upgrade the index would be corrupt, because its actual physical
order wouldn't match what pg_index says.  A more theoretical issue,
which is new as of v12, is that if you create the index with a
nondeterministic collation then it wouldn't be enforcing the normal
notion of uniqueness, causing the constraint to mean something
different from a normally-created constraint.

To fix, just add collation to the conditions checked for index
acceptability in ADD PRIMARY KEY/UNIQUE USING INDEX.  We won't try
to clean up after anybody who's already created such a situation;
it seems improbable enough to not be worth the effort involved.
(If you do get into trouble, a REINDEX should be enough to fix it.)

In principle this is a long-standing bug, but I chose not to
back-patch --- the odds of causing trouble seem about as great
as the odds of preventing it, and both risks are very low anyway.

Per report from Alexey Bashtanov, though this is not his preferred
fix.

Discussion: https://postgr.es/m/b05ce36a-cefb-ca5e-b386-a400535b1c0b@imap.cc

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/fbbf68094c5ff3d513969d072126c92932e484da

Modified Files
--------------
src/backend/parser/parse_utilcmd.c         | 12 +++++++-----
src/test/regress/expected/create_index.out | 13 +++++++++++++
src/test/regress/sql/create_index.sql      |  6 ++++++
3 files changed, 26 insertions(+), 5 deletions(-)


pgsql-committers by date:

Previous
From: Michael Paquier
Date:
Subject: pgsql: Fix handling of OpenSSL's SSL_clear_options
Next
From: Jeff Davis
Date:
Subject: pgsql: Fix comments in execGrouping.c