Fix ALTER TABLE error message
authorÁlvaro Herrera <alvherre@alvh.no-ip.org>
Tue, 4 Mar 2025 19:07:30 +0000 (20:07 +0100)
committerÁlvaro Herrera <alvherre@alvh.no-ip.org>
Tue, 4 Mar 2025 19:07:30 +0000 (20:07 +0100)
This bogus error message was introduced in 2013 by commit f177cbfe676d,
because of misunderstanding the processCASbits() API; at the time, no
test cases were added that would be affected by this change.  Only in
ca87c415e2fc was one added (along with a couple of typos), with an XXX
note that the error message was bogus.  Fix the whole, add some test
cases.

Backpatch all the way back.

Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://postgr.es/m/202503041822.aobpqke3igvb@alvherre.pgsql

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

index 7d99c9355c6aab54c0e67907f759b8dea11a76a4..c11a3beff06d129b4dfeaa1b932308616d9c1548 100644 (file)
@@ -2663,7 +2663,7 @@ alter_table_cmd:
                    n->def = (Node *) c;
                    c->conname = $3;
                    c->alterDeferrability = true;
-                   processCASbits($4, @4, "ALTER CONSTRAINT statement",
+                   processCASbits($4, @4, "FOREIGN KEY",
                                    &c->deferrable,
                                    &c->initdeferred,
                                    NULL, NULL, NULL, yyscanner);
index 692a69fe4573cbf84eebbdce42070aaf84898453..4f39100fcdf9ea1e6af990f3c04d014c7ba51082 100644 (file)
@@ -735,7 +735,7 @@ SELECT * FROM unique_tbl;
  3 | threex
 (5 rows)
 
--- enforcibility cannot be specified or set for unique constrain
+-- enforceability cannot be specified or set for unique constraint
 CREATE TABLE UNIQUE_EN_TBL(i int UNIQUE ENFORCED);
 ERROR:  misplaced ENFORCED clause
 LINE 1: CREATE TABLE UNIQUE_EN_TBL(i int UNIQUE ENFORCED);
@@ -744,13 +744,12 @@ CREATE TABLE UNIQUE_NOTEN_TBL(i int UNIQUE NOT ENFORCED);
 ERROR:  misplaced NOT ENFORCED clause
 LINE 1: CREATE TABLE UNIQUE_NOTEN_TBL(i int UNIQUE NOT ENFORCED);
                                                    ^
--- XXX: error message is misleading here
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
-ERROR:  ALTER CONSTRAINT statement constraints cannot be marked ENFORCED
+ERROR:  FOREIGN KEY constraints cannot be marked ENFORCED
 LINE 1: ...TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
                                                               ^
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORCED;
-ERROR:  ALTER CONSTRAINT statement constraints cannot be marked NOT ENFORCED
+ERROR:  FOREIGN KEY constraints cannot be marked NOT ENFORCED
 LINE 1: ...ABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORC...
                                                              ^
 DROP TABLE unique_tbl;
index 374dcb266e74aec4f8b37d161cb738504ea0f892..6a3374d5152db31da01243ca08bde22c0ea3ec53 100644 (file)
@@ -1278,11 +1278,19 @@ DETAIL:  Key (fk)=(20) is not present in table "pktable".
 COMMIT;
 -- try additional syntax
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE;
--- illegal option
+-- illegal options
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY DEFERRED;
 ERROR:  constraint declared INITIALLY DEFERRED must be DEFERRABLE
 LINE 1: ...e ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY ...
                                                              ^
+ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NO INHERIT;
+ERROR:  FOREIGN KEY constraints cannot be marked NO INHERIT
+LINE 1: ...ER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NO INHERIT...
+                                                             ^
+ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
+ERROR:  FOREIGN KEY constraints cannot be marked NOT VALID
+LINE 1: ...ER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
+                                                             ^
 -- test order of firing of FK triggers when several RI-induced changes need to
 -- be made to the same row.  This was broken by subtransaction-related
 -- changes in 8.0.
index d6742f83fb93b14940e818b7f5ee55c2001579c8..21ce4177de43f7632e28dae789756a6f23ddbcee 100644 (file)
@@ -531,10 +531,9 @@ COMMIT;
 
 SELECT * FROM unique_tbl;
 
--- enforcibility cannot be specified or set for unique constrain
+-- enforceability cannot be specified or set for unique constraint
 CREATE TABLE UNIQUE_EN_TBL(i int UNIQUE ENFORCED);
 CREATE TABLE UNIQUE_NOTEN_TBL(i int UNIQUE NOT ENFORCED);
--- XXX: error message is misleading here
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED;
 ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key NOT ENFORCED;
 
index bc0adb8cfe9f306a852755efe1a673350eaffbee..44945b0453afe050ac5ffc326386502249290633 100644 (file)
@@ -970,8 +970,10 @@ COMMIT;
 
 -- try additional syntax
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE;
--- illegal option
+-- illegal options
 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY DEFERRED;
+ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NO INHERIT;
+ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID;
 
 -- test order of firing of FK triggers when several RI-induced changes need to
 -- be made to the same row.  This was broken by subtransaction-related