]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Reject ADD CONSTRAINT NOT NULL if name mismatches existing constraint
authorÁlvaro Herrera <alvherre@kurilemu.de>
Tue, 3 Feb 2026 11:33:29 +0000 (12:33 +0100)
committerÁlvaro Herrera <alvherre@kurilemu.de>
Tue, 3 Feb 2026 11:33:29 +0000 (12:33 +0100)
commit96e2af605043974137d84edf5c0a24561956919e
tree4b543e6be7200bcea5db86da014542dc8a5801f2
parent955e50766869a5ccf862d7f8439f5d35b723c0f9
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@163.com>
Co-authored-by: Álvaro Herrera <alvherre@kurilemu.de>
Co-authored-bu: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Backpatch-through: 18
Discussion: https://postgr.es/m/19351-8f1c523ead498545%40postgresql.org
src/backend/catalog/heap.c
src/backend/catalog/pg_constraint.c
src/include/catalog/pg_constraint.h
src/test/regress/expected/constraints.out
src/test/regress/sql/constraints.sql