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 1sohGy-00D7cZ-93 for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Sep 2024 10:42:01 +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 1sohGx-004LIs-VJ for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Sep 2024 10:41:59 +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 1sohGw-004LGG-8c for pgsql-hackers@lists.postgresql.org; Thu, 12 Sep 2024 10:41:59 +0000 Received: from fhigh6-smtp.messagingengine.com ([103.168.172.157]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sohGs-000mxJ-KM for pgsql-hackers@lists.postgresql.org; Thu, 12 Sep 2024 10:41:57 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id C000B11400E3; Thu, 12 Sep 2024 06:41:52 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-11.internal (MEProxy); Thu, 12 Sep 2024 06:41:52 -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-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1726137712; x= 1726224112; bh=d6WHcnWGsOHFgraTBX/XcYBYsixTwGfgJ9zH5UWWRqE=; b=C sGkZ1lF/3H1LhXNGt5AHuAHQh4FFQKoKwcVH7R1CyrzdCW3rjXju+t7pW3mrEEyA xnYr/BYsOZyBj7a/X44qYyMadG05DxESnrIkpekv1mM8S1CzEyXTlCMf9hcoAYdE EywpTDT7Z2eYB0RZUAHqgDzbLawGRFSCLPIBwBPbhJNe/DdhAdW23Z3mPe7IVZS9 gPqXhdVuzIkpnHcvPmV+My7rPj+OxsXFoNOIeCIMIz/fWdXKLxqHlrwQRHeG5yB6 qmwcTcjeOBF4yCnxQ1xFBF7W24xjTF+8KOPWKf+aiKpTikGAHqYUbLltlwYDjODU g7uDsFsSRxFPymJikxAaQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudejfedgfedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeen ucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepgfdtgeelgefgffdtieduudff hfevkefgudefteehgfethfekvdegjeelhfethedvnecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhdpphhoshhtghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfu ihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrghdpnhgspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhht pdhrtghpthhtohepjhhirghnrdhunhhivhgvrhhsrghlihhthiesghhmrghilhdrtghomh dprhgtphhtthhopehtnhgurhifrghnghesghhmrghilhdrtghomhdprhgtphhtthhopehp ghhsqhhlqdhhrggtkhgvrhhssehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 12 Sep 2024 06:41:52 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1726137710; bh=YaPOKIo9TyfC5eC6DgsAuFRz8CHt2sIyeEdxikL6owI=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=lJN1KOvg9oveYokwl6qtDP8im11YdZ87UKDD6irhEddgbz8C43XOIgZY1yALOJv4P kX37BPvurnkYNVTNRUwkrnOz5NVABrfXczV+BuJyV68QIDYaPyabjZGCa0KUWwmEDm HO8djugxLFM2hU0wyhAKfwyhYh0tHLl8NOZTiHAuIkLRQrAblp61EDkkQMsVDF20yz QJYMpGD7PJLSqT4cB6yLxbYWlJ8ZthuuYepbqbI2lfTsHq2MEdiRdJtDg2v0Vi2+vl zPZRWAyU5/A+gGA9qXUAd4zKaO1GTNmYHf5H2ezWwni8+WFHhrho+WlYowX1X1CWC4 n66xyb2PL2j3g== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 01DEC9E; Thu, 12 Sep 2024 12:41:49 +0200 (CEST) Date: Thu, 12 Sep 2024 12:41:49 +0200 From: Alvaro Herrera To: jian he Cc: Tender Wang , Pg Hackers Subject: Re: not null constraints, again Message-ID: <202409121041.aqwna5onctkq@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Sep-12, jian he wrote: > ---exampleA > drop table pp1,cc1, cc2; > create table pp1 (f1 int); > create table cc1 (f2 text, f3 int) inherits (pp1); > create table cc2(f4 float) inherits(pp1,cc1); > alter table pp1 alter column f1 set not null; > execute constr_meta('{pp1,cc1, cc2}'); > > ---exampleB > drop table pp1,cc1, cc2; > create table pp1 (f1 int not null); > create table cc1 (f2 text, f3 int) inherits (pp1); > create table cc2(f4 float) inherits(pp1,cc1); > execute constr_meta('{pp1,cc1, cc2}'); > > Should exampleA and exampleB > return same pg_constraint->coninhcount > for not-null constraint "cc2_f1_not_null" > ? Yes, they should be identical. In this case example A is in the wrong, the constraint in cc2 should have inhcount=2 (which example B has) and it has inhcount=1. This becomes obvious if you do ALTER TABLE NO INHERIT of both parents -- in example A, it fails the second one with ERROR: relation 43823 has non-inherited constraint "cc2_f1_not_null" because the inhcount was set wrong by SET NOT NULL. Will fix. (I think the culprit is the "readyRels" stuff I had added -- I should nuke that and add a CommandCounterIncrement in the right spot ...) > We only have this Synopsis > ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL Yeah, this syntax is intended to add a "normal" not-null constraint, i.e. one that inherits. > --tests from src/test/regress/sql/inherit.sql > CREATE TABLE inh_nn_parent (a int, NOT NULL a NO INHERIT); > ALTER TABLE inh_nn_parent ALTER a SET NOT NULL; > current fail at ATExecSetNotNull > ERROR: cannot change NO INHERIT status of NOT NULL constraint > "inh_nn_parent_a_not_null" on relation "inh_nn_parent" This is correct, because here you want a normal not-null constraint but the table already has the weird ones that don't inherit. > seems we translate > ALTER TABLE inh_nn_parent ALTER a SET NOT NULL; > to > ALTER TABLE inh_nn_parent ALTER a SET NOT NULL INHERIT Well, we don't "translate" it as such. It's just what's normal. > but we cannot (syntax error) > ALTER TABLE inh_nn_parent ALTER a SET NOT NULL NO INHERIT; I don't feel a need to support this syntax. You can do with with the ADD CONSTRAINT syntax if you need it. > /* > - * Return the address of the modified column. If the column was already NOT > - * NULL, InvalidObjectAddress is returned. > + * ALTER TABLE ALTER COLUMN SET NOT NULL > + * > + * Add a not-null constraint to a single table and its children. Returns > + * the address of the constraint added to the parent relation, if one gets > + * added, or InvalidObjectAddress otherwise. > + * > + * We must recurse to child tables during execution, rather than using > + * ALTER TABLE's normal prep-time recursion. > */ > static ObjectAddress > -ATExecSetNotNull(AlteredTableInfo *tab, Relation rel, > - const char *colName, LOCKMODE lockmode) > +ATExecSetNotNull(List **wqueue, Relation rel, char *conName, char *colName, > + bool recurse, bool recursing, List **readyRels, > + LOCKMODE lockmode) > > you introduced two boolean "recurse", "recursing", don't have enough > explanation. > That is too much to comprehend. Apologies. I think it's a well-established pattern in tablecmds.c. "bool recurse" is for the caller (ATRewriteCatalogs) to request recursion. "bool recursing" is for the function informing itself that it is calling itself recursively, i.e. "I'm already recursing". This is mostly (?) used to skip things like permission checks. > " * We must recurse to child tables during execution, rather than using > " * ALTER TABLE's normal prep-time recursion. > What does this sentence mean for these two boolean "recurse", "recursing"? Here "recurse during execution" means ALTER TABLE's phase 2, that is, ATRewriteCatalogs (which means some ATExecFoo function needs to implement recursion internally), and "normal prep-time recursion" means the recursion set up by phase 1 (ATPrepCmd), which creates separate AlterTableCmd nodes for each child table. See the comments for AlterTable and the code for ATController. > Finally, I did some cosmetic changes, also improved error message > in MergeConstraintsIntoExisting Thanks, will incorporate. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "Once again, thank you and all of the developers for your hard work on PostgreSQL. This is by far the most pleasant management experience of any database I've worked on." (Dan Harris) http://archives.postgresql.org/pgsql-performance/2006-04/msg00247.php