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 1tx6mN-002ypD-6C for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Mar 2025 16:05:27 +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 1tx6mL-006K1C-NH for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Mar 2025 16:05:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tx6mL-006K14-Aj for pgsql-hackers@lists.postgresql.org; Tue, 25 Mar 2025 16:05:25 +0000 Received: from fout-b2-smtp.messagingengine.com ([202.12.124.145]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tx6mI-0016bt-1o for pgsql-hackers@lists.postgresql.org; Tue, 25 Mar 2025 16:05:24 +0000 Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfout.stl.internal (Postfix) with ESMTP id 0C9BE11401F8; Tue, 25 Mar 2025 12:05:20 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Tue, 25 Mar 2025 12:05:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to: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=fm2; t=1742918719; x=1743005119; bh=S 2gVISGX4YJj5cLSloSkXPf9EerLOXgIXcfEqNKqe6E=; b=dtJpxz8aJ/fKXGTIz i9odVQuZuNqDKMNoId/Lugo43O8mIAac4Y2QHb9C9J4xsHCLeQyNjgcGJXhUQHdl OoiSUUQWiGlW49dp2qiJZMVh7MZRUw5wheU8uFqE8TYXQ+fujUP3Zb2/KYEIku9A E99qBDmcmkfCGcRK+6QkJzhN5Vml5oErHaXobQgn+snIRoecM3F+86s+SwA6blqG DdylSsSfF8MhoHKplikoviGHEcEjPtEW/V+SuktVxDpPmKgpoD6xI7EwaWGZvAJN 6uxQ0TE5U7qvTpgqti7q+c2z75/qZ4HrQXrr+KEeRGg7v7cBER+xkDl2XfVC88F2 fWKdg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduieeftdekucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesmhekreertddt jeenucfhrhhomheplmhlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlh hvhhdrnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepieejvddugeegfeelleeh ffeiheelhfduuefftdehgeeuudejuddvgeetfffhffeunecuffhomhgrihhnpegvnhhtvg hrphhrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsg gprhgtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehsuhhrrghj rdhkhhgrrhgrghgvsegvnhhtvghrphhrihhsvggusgdrtghomhdprhgtphhtthhopehsuh hlrghmuhhlsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghr sheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 25 Mar 2025 12:05:19 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1742918717; bh=BGwaX8jLnvY2qzG/Wh6RM0K1nZr29c7i0d3F1fWRHYU=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=eWgUCZ2N2M/dhRlH0Orn3sEtDgnvL+PoNwulcbCfgg5Lh0E8375lP/Rysz9Zw78H2 UjfD0zPsdO1dHRAOEzsP693oHck6XMr61IbCUuRg4H+RPGdDFksJXmD6tvuAE0OXLL kVdSlUP8TFb62artTm30FM0ZThCOx2oMxvxUDI4g1CECCdbq2YEpIvX9RSOc6yp0Zp PbMzWm+sANWV4qiuGrZRCEXdbmxkGqoaYF6REbeWSEAZiOnHkL4MV/XOzyA2PTsKwA cR5o/7GQ6t7+vM8KZQWQISGETtQ4hWwYN3t0ZgkycnG9rNO81w1f84YwXTWKhIiRtb DHsxZkHqg0plg== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 8861685; Tue, 25 Mar 2025 17:05:17 +0100 (CET) Date: Tue, 25 Mar 2025 17:05:17 +0100 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: Pg Hackers Cc: Suraj Kharage , Amul Sul Subject: Re: simplifying grammar for ALTER CONSTRAINT .. SET [NO] INHERIT Message-ID: <202503251605.e6yngze7aeim@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="vdsowoncv3mpwt24" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <202503251602.vsxaehsyaoac@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --vdsowoncv3mpwt24 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2025-Mar-25, Álvaro Herrera wrote: > With commit f4e53e10b6ce we introduced a way to flip the NO INHERIT bit > on not-null constraints. However, because of the way the grammar > dealt with ALTER CONSTRAINT, we were too blind to see a way to implement > it using the existing production. Patch attached. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "Someone said that it is at least an order of magnitude more work to do production software than a prototype. I think he is wrong by at least an order of magnitude." (Brian Kernighan) --vdsowoncv3mpwt24 Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="0001-change-syntax-from-SET-NO-INHERIT-to-just-NO-INHERIT.patch" From ad94ec630c4d4daa3ca2e2983ca2087e089ae120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Herrera?= Date: Tue, 25 Mar 2025 17:03:32 +0100 Subject: [PATCH] change syntax from SET NO INHERIT to just NO INHERIT --- doc/src/sgml/ref/alter_table.sgml | 6 ++--- src/backend/parser/gram.y | 26 ++++++-------------- src/test/regress/expected/foreign_key.out | 4 +-- src/test/regress/expected/inherit.out | 30 +++++++++++------------ src/test/regress/sql/inherit.sql | 30 +++++++++++------------ 5 files changed, 42 insertions(+), 54 deletions(-) diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index 4f15b89a98f..11d1bc7dbe1 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -59,7 +59,7 @@ ALTER TABLE [ IF EXISTS ] name ADD table_constraint [ NOT VALID ] ADD table_constraint_using_index ALTER CONSTRAINT constraint_name [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] - ALTER CONSTRAINT constraint_name SET [ INHERIT | NO INHERIT ] + ALTER CONSTRAINT constraint_name [ INHERIT | NO INHERIT ] VALIDATE CONSTRAINT constraint_name DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] DISABLE TRIGGER [ trigger_name | ALL | USER ] @@ -564,8 +564,8 @@ WITH ( MODULUS numeric_literal, REM - ALTER CONSTRAINT ... SET INHERIT - ALTER CONSTRAINT ... SET NO INHERIT + ALTER CONSTRAINT ... INHERIT + ALTER CONSTRAINT ... NO INHERIT These forms modify a inheritable constraint so that it becomes not diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 271ae26cbaf..0fc502a3a40 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -2662,15 +2662,19 @@ alter_table_cmd: n->subtype = AT_AlterConstraint; n->def = (Node *) c; c->conname = $3; - c->alterDeferrability = true; + if ($4 & (CAS_DEFERRABLE | CAS_NOT_DEFERRABLE | + CAS_INITIALLY_DEFERRED | CAS_INITIALLY_IMMEDIATE)) + c->alterDeferrability = true; + if ($4 & CAS_NO_INHERIT) + c->alterInheritability = true; processCASbits($4, @4, "FOREIGN KEY", &c->deferrable, &c->initdeferred, - NULL, NULL, NULL, yyscanner); + NULL, NULL, &c->noinherit, yyscanner); $$ = (Node *) n; } - /* ALTER TABLE ALTER CONSTRAINT SET INHERIT */ - | ALTER CONSTRAINT name SET INHERIT + /* ALTER TABLE ALTER CONSTRAINT INHERIT */ + | ALTER CONSTRAINT name INHERIT { AlterTableCmd *n = makeNode(AlterTableCmd); ATAlterConstraint *c = makeNode(ATAlterConstraint); @@ -2681,20 +2685,6 @@ alter_table_cmd: c->alterInheritability = true; c->noinherit = false; - $$ = (Node *) n; - } - /* ALTER TABLE ALTER CONSTRAINT SET NO INHERIT */ - | ALTER CONSTRAINT name SET NO INHERIT - { - AlterTableCmd *n = makeNode(AlterTableCmd); - ATAlterConstraint *c = makeNode(ATAlterConstraint); - - n->subtype = AT_AlterConstraint; - n->def = (Node *) c; - c->conname = $3; - c->alterInheritability = true; - c->noinherit = true; - $$ = (Node *) n; } /* ALTER TABLE VALIDATE CONSTRAINT ... */ diff --git a/src/test/regress/expected/foreign_key.out b/src/test/regress/expected/foreign_key.out index 6a3374d5152..7f678349a8e 100644 --- a/src/test/regress/expected/foreign_key.out +++ b/src/test/regress/expected/foreign_key.out @@ -1284,9 +1284,7 @@ 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... - ^ +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 LINE 1: ...ER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT VALID; diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out index e671975a281..58461e7f53f 100644 --- a/src/test/regress/expected/inherit.out +++ b/src/test/regress/expected/inherit.out @@ -2754,7 +2754,7 @@ alter table inh_nn2 inherit inh_nn1; create table inh_nn3 (f4 float) inherits (inh_nn2); create table inh_nn4 (f5 int, f4 float, f2 text, f3 int, f1 int); alter table inh_nn4 inherit inh_nn2, inherit inh_nn1, inherit inh_nn3; -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3', 'inh_nn4') @@ -2767,8 +2767,8 @@ select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinheri inh_nn4 | inh_nn1_f1_not_null | {5} | 3 | f | f (4 rows) --- ALTER CONSTRAINT SET NO INHERIT should work on top-level constraints -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set no inherit; +-- ALTER CONSTRAINT NO INHERIT should work on top-level constraints +alter table inh_nn1 alter constraint inh_nn1_f1_not_null no inherit; select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3', 'inh_nn4') @@ -2799,15 +2799,15 @@ drop table inh_nn1, inh_nn2, inh_nn3, inh_nn4; create table inh_nn1 (f1 int not null no inherit); create table inh_nn2 (f2 text, f3 int) inherits (inh_nn1); insert into inh_nn2 values(NULL, 'sample', 1); -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; ERROR: column "f1" of relation "inh_nn2" contains null values delete from inh_nn2; create table inh_nn3 () inherits (inh_nn2); create table inh_nn4 () inherits (inh_nn1, inh_nn2); NOTICE: merging multiple inherited definitions of column "f1" alter table inh_nn1 -- test multicommand alter table while at it - alter constraint inh_nn1_f1_not_null set inherit, - alter constraint inh_nn1_f1_not_null set no inherit; + alter constraint inh_nn1_f1_not_null inherit, + alter constraint inh_nn1_f1_not_null no inherit; select conrelid::regclass, conname, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3', 'inh_nn4') @@ -2837,10 +2837,10 @@ select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinheri (2 rows) -- error: inh_nn3 has an incompatible NO INHERIT constraint -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; ERROR: cannot change NO INHERIT status of NOT NULL constraint "nn3_f1" on relation "inh_nn3" -alter table inh_nn3 alter constraint nn3_f1 set inherit; -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; -- now it works +alter table inh_nn3 alter constraint nn3_f1 inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; -- now it works select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3') @@ -2853,21 +2853,21 @@ select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinheri (3 rows) drop table inh_nn1, inh_nn2, inh_nn3; --- Negative scenarios for alter constraint .. set inherit. +-- Negative scenarios for alter constraint .. inherit. create table inh_nn1 (f1 int check(f1 > 5) primary key references inh_nn1, f2 int not null); -- constraints other than not-null are not supported -alter table inh_nn1 alter constraint inh_nn1_f1_check set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_check inherit; ERROR: constraint "inh_nn1_f1_check" of relation "inh_nn1" is not a not-null constraint -alter table inh_nn1 alter constraint inh_nn1_pkey set inherit; +alter table inh_nn1 alter constraint inh_nn1_pkey inherit; ERROR: constraint "inh_nn1_pkey" of relation "inh_nn1" is not a not-null constraint -alter table inh_nn1 alter constraint inh_nn1_f1_fkey set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_fkey inherit; ERROR: constraint "inh_nn1_f1_fkey" of relation "inh_nn1" is not a not-null constraint -- try to drop a nonexistant constraint -alter table inh_nn1 alter constraint foo set inherit; +alter table inh_nn1 alter constraint foo inherit; ERROR: constraint "foo" of relation "inh_nn1" does not exist -- Can't modify inheritability of inherited constraints create table inh_nn2 () inherits (inh_nn1); -alter table inh_nn2 alter constraint inh_nn1_f2_not_null set no inherit; +alter table inh_nn2 alter constraint inh_nn1_f2_not_null no inherit; ERROR: cannot alter inherited constraint "inh_nn1_f2_not_null" on relation "inh_nn2" drop table inh_nn1, inh_nn2; -- diff --git a/src/test/regress/sql/inherit.sql b/src/test/regress/sql/inherit.sql index 4e73c70495c..8df0f8af08a 100644 --- a/src/test/regress/sql/inherit.sql +++ b/src/test/regress/sql/inherit.sql @@ -1099,13 +1099,13 @@ alter table inh_nn2 inherit inh_nn1; create table inh_nn3 (f4 float) inherits (inh_nn2); create table inh_nn4 (f5 int, f4 float, f2 text, f3 int, f1 int); alter table inh_nn4 inherit inh_nn2, inherit inh_nn1, inherit inh_nn3; -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3', 'inh_nn4') order by 2, 1; --- ALTER CONSTRAINT SET NO INHERIT should work on top-level constraints -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set no inherit; +-- ALTER CONSTRAINT NO INHERIT should work on top-level constraints +alter table inh_nn1 alter constraint inh_nn1_f1_not_null no inherit; select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3', 'inh_nn4') @@ -1122,13 +1122,13 @@ drop table inh_nn1, inh_nn2, inh_nn3, inh_nn4; create table inh_nn1 (f1 int not null no inherit); create table inh_nn2 (f2 text, f3 int) inherits (inh_nn1); insert into inh_nn2 values(NULL, 'sample', 1); -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; delete from inh_nn2; create table inh_nn3 () inherits (inh_nn2); create table inh_nn4 () inherits (inh_nn1, inh_nn2); alter table inh_nn1 -- test multicommand alter table while at it - alter constraint inh_nn1_f1_not_null set inherit, - alter constraint inh_nn1_f1_not_null set no inherit; + alter constraint inh_nn1_f1_not_null inherit, + alter constraint inh_nn1_f1_not_null no inherit; select conrelid::regclass, conname, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3', 'inh_nn4') @@ -1144,26 +1144,26 @@ select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinheri conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3') order by 2, 1; -- error: inh_nn3 has an incompatible NO INHERIT constraint -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; -alter table inh_nn3 alter constraint nn3_f1 set inherit; -alter table inh_nn1 alter constraint inh_nn1_f1_not_null set inherit; -- now it works +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; +alter table inh_nn3 alter constraint nn3_f1 inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_not_null inherit; -- now it works select conrelid::regclass, conname, conkey, coninhcount, conislocal, connoinherit from pg_constraint where contype = 'n' and conrelid::regclass::text in ('inh_nn1', 'inh_nn2', 'inh_nn3') order by 2, 1; drop table inh_nn1, inh_nn2, inh_nn3; --- Negative scenarios for alter constraint .. set inherit. +-- Negative scenarios for alter constraint .. inherit. create table inh_nn1 (f1 int check(f1 > 5) primary key references inh_nn1, f2 int not null); -- constraints other than not-null are not supported -alter table inh_nn1 alter constraint inh_nn1_f1_check set inherit; -alter table inh_nn1 alter constraint inh_nn1_pkey set inherit; -alter table inh_nn1 alter constraint inh_nn1_f1_fkey set inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_check inherit; +alter table inh_nn1 alter constraint inh_nn1_pkey inherit; +alter table inh_nn1 alter constraint inh_nn1_f1_fkey inherit; -- try to drop a nonexistant constraint -alter table inh_nn1 alter constraint foo set inherit; +alter table inh_nn1 alter constraint foo inherit; -- Can't modify inheritability of inherited constraints create table inh_nn2 () inherits (inh_nn1); -alter table inh_nn2 alter constraint inh_nn1_f2_not_null set no inherit; +alter table inh_nn2 alter constraint inh_nn1_f2_not_null no inherit; drop table inh_nn1, inh_nn2; -- 2.39.5 --vdsowoncv3mpwt24--