pgsql: Reject ADD CONSTRAINT NOT NULL if name mismatches existing const

From: Álvaro Herrera <alvherre(at)kurilemu(dot)de>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Reject ADD CONSTRAINT NOT NULL if name mismatches existing const
Date: 2026-02-03 11:39:38
Message-ID: E1vnEks-000u3v-1B@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Reject ADD CONSTRAINT NOT NULL if name mismatches existing constraint

When using ALTER TABLE ... ADD CONSTRAINT to add a not-null constraint
with an explicit name, we have to ensure that if the column is already
marked NOT NULL, the provided name matches the existing constraint name.
Failing to do so could lead to confusion regarding which constraint
object actually enforces the rule.

This patch adds a check to throw an error if the user tries to add a
named not-null constraint to a column that already has one with a
different name.

Reported-by: yanliang lei <msdnchina(at)163(dot)com>
Co-authored-by: Álvaro Herrera <alvherre(at)kurilemu(dot)de>
Co-authored-bu: Srinath Reddy Sadipiralla <srinath2133(at)gmail(dot)com>
Backpatch-through: 18
Discussion: https://postgr.es/m/19351-8f1c523ead498545%40postgresql.org

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/492a69e1407029f8c673484f44aa719a63323d77

Modified Files
--------------
src/backend/catalog/heap.c | 1 +
src/backend/catalog/pg_constraint.c | 21 +++++++++++++++++++--
src/include/catalog/pg_constraint.h | 2 +-
src/test/regress/expected/constraints.out | 6 +++++-
src/test/regress/sql/constraints.sql | 4 +++-
5 files changed, 29 insertions(+), 5 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2026-02-03 13:09:51 pgsql: Don't hint that you can reconnect when the database is dropped
Previous Message Peter Eisentraut 2026-02-03 08:07:51 pgsql: Change StaticAssertVariableIsOfType to be a declaration