Skip to content

Commit d1a4958

Browse files
jianhe-funCommitfest Bot
authored andcommitted
better error message for ALTER DOMAIN ADD CONSTRAINT
DomainConstraintElem syntax in gram.y can specify constraint properties such as ([ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE | NO INHERIT ] [ENFORCED | NOT ENFORCED]) According to the ALTER DOMAIN synopsis section, none of these properties are allowed. In fact, these properties are wrapped up as a separate individual Constraints node (see DefineDomain). However, AlterDomainAddConstraint can only cope with a single Constraints node. therefore, error out at AlterDomainAddConstraint is not possible, so handle error cases in gram.y. discussion: https://postgr.es/m/CACJufxHitd5LGLBSSAPShhtDWxT0ViVKTHinkYW-skBX93TcpA@mail.gmail.com
1 parent 9ea3b6f commit d1a4958

File tree

4 files changed

+79
-8
lines changed

4 files changed

+79
-8
lines changed

src/backend/commands/tablecmds.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5463,8 +5463,7 @@ ATExecCmd(List **wqueue, AlteredTableInfo *tab,
54635463
ATExecAddConstraint(wqueue, tab, rel, (Constraint *) cmd->def,
54645464
true, true, lockmode);
54655465
break;
5466-
case AT_ReAddDomainConstraint: /* Re-add pre-existing domain check
5467-
* constraint */
5466+
case AT_ReAddDomainConstraint: /* add domain check or not-null constraint */
54685467
address =
54695468
AlterDomainAddConstraint(((AlterDomainStmt *) cmd->def)->typeName,
54705469
((AlterDomainStmt *) cmd->def)->def,

src/backend/parser/gram.y

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,8 +4418,27 @@ DomainConstraintElem:
44184418
n->raw_expr = $3;
44194419
n->cooked_expr = NULL;
44204420
processCASbits($5, @5, "CHECK",
4421-
NULL, NULL, NULL, &n->skip_validation,
4422-
&n->is_no_inherit, yyscanner);
4421+
&n->deferrable, &n->initdeferred, &n->is_enforced,
4422+
&n->skip_validation, &n->is_no_inherit, yyscanner);
4423+
4424+
if ($5 & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE | CAS_INITIALLY_IMMEDIATE |
4425+
CAS_INITIALLY_DEFERRED))
4426+
ereport(ERROR,
4427+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4428+
errmsg("specifying constraint deferrability not supported for domains"),
4429+
parser_errposition(@5));
4430+
4431+
if ($5 & (CAS_NOT_ENFORCED | CAS_ENFORCED))
4432+
ereport(ERROR,
4433+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4434+
errmsg("specifying constraint enforceability not supported for domains"),
4435+
parser_errposition(@5));
4436+
if (n->is_no_inherit)
4437+
ereport(ERROR,
4438+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4439+
errmsg("constraint specified as no-inherit is not supported for domains"),
4440+
parser_errposition(@5));
4441+
44234442
n->is_enforced = true;
44244443
n->initially_valid = !n->skip_validation;
44254444
$$ = (Node *) n;
@@ -4433,8 +4452,27 @@ DomainConstraintElem:
44334452
n->keys = list_make1(makeString("value"));
44344453
/* no NOT VALID, NO INHERIT support */
44354454
processCASbits($3, @3, "NOT NULL",
4436-
NULL, NULL, NULL,
4437-
NULL, NULL, yyscanner);
4455+
&n->deferrable, &n->initdeferred, &n->is_enforced,
4456+
NULL, &n->is_no_inherit, yyscanner);
4457+
4458+
if ($3 & (CAS_NOT_DEFERRABLE | CAS_DEFERRABLE | CAS_INITIALLY_IMMEDIATE |
4459+
CAS_INITIALLY_DEFERRED))
4460+
ereport(ERROR,
4461+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4462+
errmsg("specifying constraint deferrability not supported for domains"),
4463+
parser_errposition(@3));
4464+
4465+
if ($3 & (CAS_NOT_ENFORCED | CAS_ENFORCED))
4466+
ereport(ERROR,
4467+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4468+
errmsg("specifying constraint enforceability not supported for domains"),
4469+
parser_errposition(@3));
4470+
if (n->is_no_inherit)
4471+
ereport(ERROR,
4472+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4473+
errmsg("constraint specified as no-inherit is not supported for domains"),
4474+
parser_errposition(@3));
4475+
44384476
n->initially_valid = true;
44394477
$$ = (Node *) n;
44404478
}

src/test/regress/expected/domain.out

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ create domain d_fail as int4 constraint cc check (values > 1) deferrable;
6868
ERROR: specifying constraint deferrability not supported for domains
6969
LINE 1: ...n d_fail as int4 constraint cc check (values > 1) deferrable...
7070
^
71+
create domain d_int as int4;
72+
alter domain d_int add constraint nn not null no inherit;
73+
ERROR: constraint specified as no-inherit is not supported for domains
74+
LINE 1: alter domain d_int add constraint nn not null no inherit;
75+
^
76+
alter domain d_int add constraint nn not null not enforced;
77+
ERROR: specifying constraint enforceability not supported for domains
78+
LINE 1: alter domain d_int add constraint nn not null not enforced;
79+
^
80+
alter domain d_int add constraint nn not null not deferrable initially immediate;
81+
ERROR: specifying constraint deferrability not supported for domains
82+
LINE 1: alter domain d_int add constraint nn not null not deferrable...
83+
^
84+
alter domain d_int add constraint cc check(value > 1) no inherit;
85+
ERROR: constraint specified as no-inherit is not supported for domains
86+
LINE 1: ...r domain d_int add constraint cc check(value > 1) no inherit...
87+
^
88+
alter domain d_int add constraint cc check(value > 1) not enforced;
89+
ERROR: specifying constraint enforceability not supported for domains
90+
LINE 1: ...r domain d_int add constraint cc check(value > 1) not enforc...
91+
^
92+
alter domain d_int add constraint cc check(value > 1) not deferrable initially immediate;
93+
ERROR: specifying constraint deferrability not supported for domains
94+
LINE 1: ...r domain d_int add constraint cc check(value > 1) not deferr...
95+
^
96+
drop domain d_int;
7197
-- Test domain input.
7298
-- Note: the point of checking both INSERT and COPY FROM is that INSERT
7399
-- exercises CoerceToDomain while COPY exercises domain_in.
@@ -1368,11 +1394,11 @@ LINE 1: ...S int CONSTRAINT the_constraint CHECK (value > 0) NOT ENFORC...
13681394
CREATE DOMAIN constraint_enforced_dom AS int;
13691395
-- XXX misleading error messages
13701396
ALTER DOMAIN constraint_enforced_dom ADD CONSTRAINT the_constraint CHECK (value > 0) ENFORCED;
1371-
ERROR: CHECK constraints cannot be marked ENFORCED
1397+
ERROR: specifying constraint enforceability not supported for domains
13721398
LINE 1: ...om ADD CONSTRAINT the_constraint CHECK (value > 0) ENFORCED;
13731399
^
13741400
ALTER DOMAIN constraint_enforced_dom ADD CONSTRAINT the_constraint CHECK (value > 0) NOT ENFORCED;
1375-
ERROR: CHECK constraints cannot be marked NOT ENFORCED
1401+
ERROR: specifying constraint enforceability not supported for domains
13761402
LINE 1: ...m ADD CONSTRAINT the_constraint CHECK (value > 0) NOT ENFORC...
13771403
^
13781404
DROP DOMAIN constraint_enforced_dom;

src/test/regress/sql/domain.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ create domain d_fail as int4 constraint cc generated by default as identity;
3131
create domain d_fail as int4 constraint cc check (values > 1) no inherit;
3232
create domain d_fail as int4 constraint cc check (values > 1) deferrable;
3333

34+
create domain d_int as int4;
35+
alter domain d_int add constraint nn not null no inherit;
36+
alter domain d_int add constraint nn not null not enforced;
37+
alter domain d_int add constraint nn not null not deferrable initially immediate;
38+
alter domain d_int add constraint cc check(value > 1) no inherit;
39+
alter domain d_int add constraint cc check(value > 1) not enforced;
40+
alter domain d_int add constraint cc check(value > 1) not deferrable initially immediate;
41+
drop domain d_int;
3442
-- Test domain input.
3543

3644
-- Note: the point of checking both INSERT and COPY FROM is that INSERT

0 commit comments

Comments
 (0)