]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix error message for ALTER CONSTRAINT ... NOT VALID
authorÁlvaro Herrera <alvherre@kurilemu.de>
Wed, 2 Jul 2025 15:02:27 +0000 (17:02 +0200)
committerÁlvaro Herrera <alvherre@kurilemu.de>
Wed, 2 Jul 2025 15:02:27 +0000 (17:02 +0200)
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

src/backend/parser/gram.y
src/test/regress/expected/constraints.out
src/test/regress/expected/foreign_key.out
src/test/regress/sql/constraints.sql

index 1c11b235aa609d6010473d9805a52cc3d61ea2f3..a2e084b8f64310e7e145f5acae191efd30f874d4 100644 (file)
@@ -2668,6 +2668,12 @@ alter_table_cmd:
                                                c->alterDeferrability = true;
                                        if ($4 & CAS_NO_INHERIT)
                                                c->alterInheritability = true;
+                                       /* handle unsupported case with specific error message */
+                                       if ($4 & CAS_NOT_VALID)
+                                               ereport(ERROR,
+                                                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                                               errmsg("constraints cannot be altered to be NOT VALID"),
+                                                               parser_errposition(@4));
                                        processCASbits($4, @4, "FOREIGN KEY",
                                                                        &c->deferrable,
                                                                        &c->initdeferred,
index b5592617d97550d2f66161d5d783fafdb1b2cc23..ccea883cffd65458e03e01e09ac934ca7675330a 100644 (file)
@@ -748,6 +748,11 @@ ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
 ERROR:  cannot alter enforceability of constraint "unique_tbl_i_key" of relation "unique_tbl"
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORCED;
 ERROR:  cannot alter enforceability of constraint "unique_tbl_i_key" of relation "unique_tbl"
+-- can't make an existing constraint NOT VALID
+ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT VALID;
+ERROR:  constraints cannot be altered to be NOT VALID
+LINE 1: ...ABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT VALID;
+                                                             ^
 DROP TABLE unique_tbl;
 --
 -- EXCLUDE constraints
index 6a8f395934520bfabe1c859958269234b951a852..f9bd252444f5373eedec4ef897045773b8c60f59 100644 (file)
@@ -1359,7 +1359,7 @@ LINE 1: ...e ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY ...
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NO INHERIT;
 ERROR:  constraint "fktable_fk_fkey" of relation "fktable" is not a not-null constraint
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
-ERROR:  FOREIGN KEY constraints cannot be marked NOT VALID
+ERROR:  constraints cannot be altered to be NOT VALID
 LINE 1: ...ER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
                                                              ^
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey ENFORCED NOT ENFORCED;
index 12668f0e0ce0f4867a2077de1d4f409c5bc350fa..7487723ab843786c27b43a43ccfa56e5c98c7079 100644 (file)
@@ -537,6 +537,9 @@ CREATE TABLE UNIQUE_NOTEN_TBL(i int UNIQUE NOT ENFORCED);
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORCED;
 
+-- can't make an existing constraint NOT VALID
+ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT VALID;
+
 DROP TABLE unique_tbl;
 
 --