Fix error message for ALTER CONSTRAINT ... NOT VALID · postgres/postgres@c989aff · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Commit c989aff

Browse files
Álvaro Herrerajianhe-funFujii Masaoamulsul
committed
Fix error message for ALTER CONSTRAINT ... NOT VALID
Trying to alter a constraint so that it becomes NOT VALID results in an error that assumes the constraint is a foreign key. This is potentially wrong, so give a more generic error message. While at it, give CREATE CONSTRAINT TRIGGER a better error message as well. Co-authored-by: jian he <jian.universality@gmail.com> Co-authored-by: Fujii Masao <masao.fujii@oss.nttdata.com> Co-authored-by: Álvaro Herrera <alvherre@kurilemu.de> Co-authored-by: Amul Sul <sulamul@gmail.com> Discussion: https://postgr.es/m/CACJufxHSp2puxP=q8ZtUGL1F+heapnzqFBZy5ZNGUjUgwjBqTQ@mail.gmail.com
1 parent bd3f59f commit c989aff

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

src/backend/parser/gram.y

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,12 @@ alter_table_cmd:
26682668
c->alterDeferrability = true;
26692669
if ($4 & CAS_NO_INHERIT)
26702670
c->alterInheritability = true;
2671+
/* handle unsupported case with specific error message */
2672+
if ($4 & CAS_NOT_VALID)
2673+
ereport(ERROR,
2674+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2675+
errmsg("constraints cannot be altered to be NOT VALID"),
2676+
parser_errposition(@4));
26712677
processCASbits($4, @4, "FOREIGN KEY",
26722678
&c->deferrable,
26732679
&c->initdeferred,

src/test/regress/expected/constraints.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,11 @@ ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
748748
ERROR: cannot alter enforceability of constraint "unique_tbl_i_key" of relation "unique_tbl"
749749
ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORCED;
750750
ERROR: cannot alter enforceability of constraint "unique_tbl_i_key" of relation "unique_tbl"
751+
-- can't make an existing constraint NOT VALID
752+
ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT VALID;
753+
ERROR: constraints cannot be altered to be NOT VALID
754+
LINE 1: ...ABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT VALID;
755+
^
751756
DROP TABLE unique_tbl;
752757
--
753758
-- EXCLUDE constraints

src/test/regress/expected/foreign_key.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ LINE 1: ...e ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY ...
13591359
ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NO INHERIT;
13601360
ERROR: constraint "fktable_fk_fkey" of relation "fktable" is not a not-null constraint
13611361
ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
1362-
ERROR: FOREIGN KEY constraints cannot be marked NOT VALID
1362+
ERROR: constraints cannot be altered to be NOT VALID
13631363
LINE 1: ...ER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
13641364
^
13651365
ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey ENFORCED NOT ENFORCED;

src/test/regress/sql/constraints.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ CREATE TABLE UNIQUE_NOTEN_TBL(i int UNIQUE NOT ENFORCED);
537537
ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
538538
ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORCED;
539539

540+
-- can't make an existing constraint NOT VALID
541+
ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT VALID;
542+
540543
DROP TABLE unique_tbl;
541544

542545
--

0 commit comments

Comments
 (0)

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.