1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Prevent creation of duplicate not-null constraints for domains

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
This commit is contained in:
Álvaro Herrera
2025-07-03 11:46:12 +02:00
parent 87251e1149
commit 647cffd2f3
3 changed files with 19 additions and 3 deletions

View 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),

View 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;

View 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