Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tpWuU-007GME-AA for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Mar 2025 18:22:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tpWuT-004X4W-1F for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Mar 2025 18:22:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tpWuS-004Wy4-8y for pgsql-hackers@lists.postgresql.org; Tue, 04 Mar 2025 18:22:28 +0000 Received: from fhigh-a8-smtp.messagingengine.com ([103.168.172.159]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tpWuP-000xGZ-1n for pgsql-hackers@lists.postgresql.org; Tue, 04 Mar 2025 18:22:27 +0000 Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id 967651140135 for ; Tue, 4 Mar 2025 13:22:24 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Tue, 04 Mar 2025 13:22:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:message-id:mime-version:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1741112544; x=1741198944; bh=itge/yt2YFkiby1b02oBnkYB/9Yn4eEvpnG dKRBMXnU=; b=fgEZuC/9fGmrSYB9f6eDqMDY4hSIqXlxzCIsbZsw3/wjC3w/9Wy g3cG3tTbirnENAwZrtgpFk9Ulfu6YAjSFhBC0Dwfy7+pyc9oMZ/JbDGVxMbaes+E 4awpSCTQ31d2S+XCPNROz21SaaRYXdRqN78Kj/WvNzLU/RC4hRr739kYAhDMXgDf fWvfLIUyuXkiKMjYhoCvzwQZtijFJRLcijoAE08ZnbPEtDTebWI+NZRtPMD0cBwk LNXMAkgfPIDFNT5sACIy95FfUqVtvzbvhTV6l6IBAraRg2HZtuazLQyxhOSHSWwq 5/QGRB/r8xUhfA/mtVQLvoCsGIfuiVuizAQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddutddvjeehucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepfffhvffukfggtggugfesmhekreertddtjeen ucfhrhhomheplmhlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepieeifeekueegffevvedtjeeg jefgheegvdefhfevgfdvkedtvdehgedvffejjedtnecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprh gtphhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehpghhsqhhlqdhh rggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 4 Mar 2025 13:22:24 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1741112542; bh=JidFPh2xwIDoCMwJDpz0J3NY++NKXTJ8EE5m2+EvSdw=; h=Date:From:To:Subject:From; b=P46SQH5II0JX/g/pfGqJmj2wiyLEUQKHOEKkZvsz+Af3/8lD/HJuI3sdeD4xeig/u xBefi4HUC388OZ9tRYolyg3KcqE2wrEw8RnPHbu7FkkUEQwfINqZ6dsAF470k5QsyK NQS9xfA9UQ7bVATb8zxCMG2N1MJ35z1x4ImPQoUQPSg6LDZA5NRsY3NJ3t5wpuIA7h sAMjraih9+k3bBVHBIIq4Oyz2/yVDbbx82c6hZcxk7Rw0FffXnqriqCp2GiLOn9edf nQsNXqN4RnropxhPm56gRtzKUac62nwMRU6hGAdEUwkzBPH88I8oROrqvimzoxxPTG lVyUp8I95cmBg== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id AC8C58F; Tue, 4 Mar 2025 19:22:22 +0100 (CET) Date: Tue, 4 Mar 2025 19:22:22 +0100 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: Pg Hackers Subject: bogus error message for ALTER TABLE ALTER CONSTRAINT Message-ID: <202503041822.aobpqke3igvb@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="vvywzef3pmz5robm" Content-Disposition: inline Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --vvywzef3pmz5robm Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit I just discovered that trying to set a foreign key as NO INHERIT in ALTER TABLE ALTER CONSTRAINT returns an absurd error message: create table pk (a int primary key); create table fk (a int references pk); alter table fk alter constraint fk_a_fkey deferrable, alter constraint fk_a_fkey no inherit; ERROR: ALTER CONSTRAINT statement constraints cannot be marked NO INHERIT The explanation is that somebody misunderstood what must be given to processCASbits in 2013. The intended message is: ERROR: FOREIGN KEY constraints cannot be marked NO INHERIT Here's the fix along with some additional cleanup. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ --vvywzef3pmz5robm Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-Fix-ALTER-TABLE-error-message.patch" From ba95124d06e337c285a31b0376230702e6b1fc3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Tue, 4 Mar 2025 18:48:14 +0100 Subject: [PATCH] Fix ALTER TABLE error message 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. --- src/backend/parser/gram.y | 2 +- src/test/regress/expected/constraints.out | 7 +++---- src/test/regress/expected/foreign_key.out | 10 +++++++++- src/test/regress/sql/constraints.sql | 3 +-- src/test/regress/sql/foreign_key.sql | 4 +++- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 7d99c9355c6..c11a3beff06 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -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); diff --git a/src/test/regress/expected/constraints.out b/src/test/regress/expected/constraints.out index 692a69fe457..4f39100fcdf 100644 --- a/src/test/regress/expected/constraints.out +++ b/src/test/regress/expected/constraints.out @@ -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; diff --git a/src/test/regress/expected/foreign_key.out b/src/test/regress/expected/foreign_key.out index 374dcb266e7..6a3374d5152 100644 --- a/src/test/regress/expected/foreign_key.out +++ b/src/test/regress/expected/foreign_key.out @@ -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. diff --git a/src/test/regress/sql/constraints.sql b/src/test/regress/sql/constraints.sql index d6742f83fb9..21ce4177de4 100644 --- a/src/test/regress/sql/constraints.sql +++ b/src/test/regress/sql/constraints.sql @@ -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; diff --git a/src/test/regress/sql/foreign_key.sql b/src/test/regress/sql/foreign_key.sql index bc0adb8cfe9..44945b0453a 100644 --- a/src/test/regress/sql/foreign_key.sql +++ b/src/test/regress/sql/foreign_key.sql @@ -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 -- 2.39.5 --vvywzef3pmz5robm--