From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fully enforce uniqueness of constraint names.
Date: Tue, 04 Sep 2018 18:04:35 +0000
Message-ID: <E1fxFgp-0006Ni-1o@gemulon.postgresql.org> (raw)
Fully enforce uniqueness of constraint names.
It's been true for a long time that we expect names of table and domain
constraints to be unique among the constraints of that table or domain.
However, the enforcement of that has been pretty haphazard, and it missed
some corner cases such as creating a CHECK constraint and then an index
constraint of the same name (as per recent report from André Hänsel).
Also, due to the lack of an actual unique index enforcing this, duplicates
could be created through race conditions.
Moreover, the code that searches pg_constraint has been quite inconsistent
about how to handle duplicate names if one did occur: some places checked
and threw errors if there was more than one match, while others just
processed the first match they came to.
To fix, create a unique index on (conrelid, contypid, conname). Since
either conrelid or contypid is zero, this will separately enforce
uniqueness of constraint names among constraints of any one table and any
one domain. (If we ever implement SQL assertions, and put them into this
catalog, more thought might be needed. But it'd be at least as reasonable
to put them into a new catalog; having overloaded this one catalog with
two kinds of constraints was a mistake already IMO.) This index can replace
the existing non-unique index on conrelid, though we need to keep the one
on contypid for query performance reasons.
Having done that, we can simplify the logic in various places that either
coped with duplicates or neglected to, as well as potentially improve
lookup performance when searching for a constraint by name.
Also, as per our usual practice, install a preliminary check so that you
get something more friendly than a unique-index violation report in the
case complained of by André. And teach ChooseIndexName to avoid choosing
autogenerated names that would draw such a failure.
While it's not possible to make such a change in the back branches,
it doesn't seem quite too late to put this into v11, so do so.
Discussion: https://postgr.es/m/0c1001d4428f$0942b430$1bc81c90$@webkr.de
Branch
------
REL_11_STABLE
Details
-------
https://git.postgresql.org/pg/commitdiff/fb466d7b5dbe73f318324cada80203522f46401f
Modified Files
--------------
doc/src/sgml/ref/alter_index.sgml | 3 +
doc/src/sgml/ref/alter_table.sgml | 37 +++--
doc/src/sgml/ref/create_table.sgml | 24 +++
src/backend/catalog/heap.c | 190 +++++++++++-----------
src/backend/catalog/index.c | 20 +++
src/backend/catalog/pg_constraint.c | 254 +++++++++++++++---------------
src/backend/commands/indexcmds.c | 27 +++-
src/backend/commands/tablecmds.c | 138 ++++++++--------
src/backend/commands/typecmds.c | 85 +++++-----
src/backend/parser/parse_utilcmd.c | 3 +-
src/backend/utils/cache/relcache.c | 10 +-
src/include/catalog/catversion.h | 2 +-
src/include/catalog/indexing.h | 4 +-
src/include/catalog/pg_constraint.h | 7 +-
src/include/commands/defrem.h | 3 +-
src/test/regress/expected/alter_table.out | 35 ++++
src/test/regress/sql/alter_table.sql | 18 +++
17 files changed, 494 insertions(+), 366 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: Fully enforce uniqueness of constraint names.
In-Reply-To: <E1fxFgp-0006Ni-1o@gemulon.postgresql.org>
References: <E1fxFgp-0006Ni-1o@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox