Re: Reject ADD CONSTRAINT NOT NULL if name mismatches existing domain not-null constraint

From: Abraham Pérez <abraham1798(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Reject ADD CONSTRAINT NOT NULL if name mismatches existing domain not-null constraint
Date: 2026-11-27 13:13:13
Message-ID: CAF6dGh1x2d1wvUecmK-MxgSOH21L0SziUuaV6q9=bMTGc0d_9g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I spent some time reviewing this patch (v1-0001) and wanted to share my test
results and a couple of observations.

Testing
=======
I applied v1-0001 on top of current master (20ad1c5b610). It applies cleanly
with git am, no rebase needed. Built with --enable-debug --enable-cassert
--enable-tap-tests; no new compiler warnings. "make check" is green here (all
243 tests passed), including the updated src/test/regress domain test.

I also reproduced the behavior change on a live instance:

create domain dnn int;
alter domain dnn add constraint c1 not null;
alter domain dnn add constraint c2 not null; -- mismatched name,
already NOT NULL

On master this second statement silently succeeds (it is treated as redundant).
With the patch it is rejected:

ERROR: cannot create not-null constraint "c2" for domain "dnn"
DETAIL: A not-null constraint named "c1" already exists for domain "dnn".

The redundant cases that should still be accepted are accepted: repeating the
same name ("add constraint c1 not null" again) and the unnamed form ("add not
null"). This matches the behavior for table NOT NULL constraints introduced in
96e2af605043, and the new error reuses the same errcode and message wording,
which looks right to me.

Code
====
The change is small and localized to the already-NOT-NULL branch of
AlterDomainAddConstraint(). One minor note, already raised by Chao Li upthread:
the HeapTuple returned by findDomainNotNullConstraint() is not freed,
but that is
consistent with the sibling code in AlterDomainNotNull(), which also relies on
the surrounding memory context, so I don't think it's a problem.

Nit: the commit message still has empty "discussion:" / "commitfest entry:"
placeholder URLs; worth filling in before commit.

Open design question
====================
The main thing that seems to be holding this up is the question raised
downthread:
whether these mismatched-name cases should ERROR at all, versus Zizhuan Liu's
counter-proposal to emit a NOTICE and keep the previously-compatible "silently
succeed" behavior (and to extend the same treatment to AlterDomainNotNull()).

For what it's worth, v1 as written is self-consistent and matches the precedent
set for tables in 96e2af605043, where the project chose to ERROR in exactly this
situation. So if aligning domain behavior with table behavior is the goal, v1
does that correctly. Since that direction isn't settled yet, I'll leave the
error-vs-notice call to the author and a committer rather than take a side here.

Given that open point, I'm setting this to Waiting on Author.

Thanks for the patch!

Regards,

Browse pgsql-hackers by date

  From Date Subject
Next Message Osama Abdul Qader 2026-11-27 13:14:30 Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint
Previous Message vignesh C 2026-11-27 13:03:47 Re: Fix a wrong comment in UpdateDeadTupleRetentionStatus()