]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Prevent creation of duplicate not-null constraints for domains
authorÁlvaro Herrera <alvherre@kurilemu.de>
Thu, 3 Jul 2025 09:46:12 +0000 (11:46 +0200)
committerÁlvaro Herrera <alvherre@kurilemu.de>
Thu, 3 Jul 2025 09:46:12 +0000 (11:46 +0200)
This was previously harmless, but now that we create pg_constraint rows
for those, duplicates are not welcome anymore.

Backpatch to 18.

Co-authored-by: jian he <jian.universality@gmail.com>
Co-authored-by: Álvaro Herrera <alvherre@kurilemu.de>
Discussion: https://postgr.es/m/CACJufxFSC0mcQ82bSk58sO-WJY4P-o4N6RD2M0D=DD_u_6EzdQ@mail.gmail.com

src/backend/commands/typecmds.c
src/test/regress/expected/domain.out
src/test/regress/sql/domain.sql

index 45ae7472ab5ad08393285268bf3c5d2d7178041d..26d985193aea42888c250b977cdc2f0d5e91fd75 100644 (file)
@@ -939,11 +939,19 @@ DefineDomain(ParseState *pstate, CreateDomainStmt *stmt)
                                break;
 
                        case CONSTR_NOTNULL:
-                               if (nullDefined && !typNotNull)
+                               if (nullDefined)
+                               {
+                                       if (!typNotNull)
+                                               ereport(ERROR,
+                                                               errcode(ERRCODE_SYNTAX_ERROR),
+                                                               errmsg("conflicting NULL/NOT NULL constraints"),
+                                                               parser_errposition(pstate, constr->location));
+
                                        ereport(ERROR,
-                                                       errcode(ERRCODE_SYNTAX_ERROR),
-                                                       errmsg("conflicting NULL/NOT NULL constraints"),
+                                                       errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+                                                       errmsg("redundant NOT NULL constraint definition"),
                                                        parser_errposition(pstate, constr->location));
+                               }
                                if (constr->is_no_inherit)
                                        ereport(ERROR,
                                                        errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
index ba6f05eeb7df6087b15e6e905d0b996bb3e9ce21..b5ea707df31038d617b2f950419516c1e9a39e47 100644 (file)
@@ -1019,6 +1019,11 @@ insert into domain_test values (1, 2);
 -- should fail
 alter table domain_test add column c str_domain;
 ERROR:  domain str_domain does not allow null values
+-- disallow duplicated not-null constraints
+create domain int_domain1 as int constraint nn1 not null constraint nn2 not null;
+ERROR:  redundant NOT NULL constraint definition
+LINE 1: ...domain int_domain1 as int constraint nn1 not null constraint...
+                                                             ^
 create domain str_domain2 as text check (value <> 'foo') default 'foo';
 -- should fail
 alter table domain_test add column d str_domain2;
index b752a63ab5f698e2c92bfcacb8c1dfbee8946e73..b8f5a6397121a81d274a56c87e7f748c210103ee 100644 (file)
@@ -602,6 +602,9 @@ insert into domain_test values (1, 2);
 -- should fail
 alter table domain_test add column c str_domain;
 
+-- disallow duplicated not-null constraints
+create domain int_domain1 as int constraint nn1 not null constraint nn2 not null;
+
 create domain str_domain2 as text check (value <> 'foo') default 'foo';
 
 -- should fail