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 1st255-00Cu6U-0j for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2024 09:43:40 +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 1st254-005WGB-7t for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2024 09:43:38 +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 1st253-005WG3-1A for pgsql-hackers@lists.postgresql.org; Tue, 24 Sep 2024 09:43:37 +0000 Received: from fout-a2-smtp.messagingengine.com ([103.168.172.145]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1st24y-000otY-3S for pgsql-hackers@lists.postgresql.org; Tue, 24 Sep 2024 09:43:36 +0000 Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfout.phl.internal (Postfix) with ESMTP id B2BFA1380267; Tue, 24 Sep 2024 05:43:30 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-10.internal (MEProxy); Tue, 24 Sep 2024 05:43:30 -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=fm2; t=1727171010; x= 1727257410; bh=9pxKMK/0pDj9Zu9nlD04XuStIlZvUXsOCrmTFWhOmY4=; b=e steCqc5DoXl71H2x1JfMbwRd/t0SlmTXJbqnKjQbgU+dDqfrhWNiP9DavXW/1zBW l/cAi4cyYYejXGTwIkXT7H5egBZA75Vp5VTnhFkMDRqzRAw5ZuW3piqO6M7R9MVl fZjQ4PdvvlY0nBSypKK+jZlzBnLfesuSBjwnPRIHR72dbIQ3aELv9wpJBrY29vN3 edvx2KiHdF40wIvMsPr1RKOLRGpJ3m8GXk2Wy1LjxDU2AyRA19Imp7Fc9pWrO9Y4 f7LzZ39uHgtLk091Y5WAold+Dy3pUy7/14rRVQ5Myd1QacBUmylu8hQnAKCACVLP qARidi2RcSBkKq3lE9uLw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddtvddgudekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeen ucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffh jeejhffgieeuueekjeekfffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprh gtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjihgrnhdruhhn ihhvvghrshgrlhhithihsehgmhgrihhlrdgtohhmpdhrtghpthhtohepthhnughrfigrnh hgsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrsheslhhi shhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 24 Sep 2024 05:43:29 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1727171006; bh=mQzf6fhtD9k+Zxab2fCEXT3ZMP1N7BEf5n2J/VKZq3g=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=WzbjexBNgrejJ37ItBtjZsWiQ81Enrvq3rqJNtSl+YhJK909j4fphgYg/oCHriwBO gSSWAgUQNQ0IvqhkOME9CG6M2teJiabrndwm4L1fvAbNPFF8/+VHvt+1e1Jv2XzeRd xgAd10M4PnfswZmjeZBPRlSOPi4rRpLnnIKyp3D101OulFWwGgPos6AM8P7ShikL3x lF9IU33WHhuBrqt8psIheb4S+RkeioB9XWyezABRnmDryA/+NtWN9342lanDFU1zhm eLkWeHCrL6FJ8gFbFSNhn0u9jgYEWERWO11xF4tnoexdY4BKwKKODnwPYS5IMkNvkU DQRDf44bVREMQ== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 53977D9; Tue, 24 Sep 2024 11:43:26 +0200 (CEST) Date: Tue, 24 Sep 2024 11:43:26 +0200 From: Alvaro Herrera To: jian he Cc: Tender Wang , Pg Hackers Subject: Re: not null constraints, again Message-ID: <202409240943.vybpb5nq7vt3@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-24, jian he wrote: > static Oid > StoreRelNotNull(Relation rel, const char *nnname, AttrNumber attnum, > bool is_validated, bool is_local, int inhcount, > bool is_no_inherit) > { > Oid constrOid; > Assert(attnum > InvalidAttrNumber); > constrOid = > CreateConstraintEntry(nnname, > RelationGetNamespace(rel), > CONSTRAINT_NOTNULL, > false, > false, > is_validated > .... > } > is is_validated always true, can we add an Assert on it? Sure. FWIW the reason it's a parameter at all, is that the obvious next patch is to add support for NOT VALID constraints. I don't want to introduce support for NOT VALID immediately with the first patch because I'm sure some wrinkles will appear; but a followup patch will surely follow shortly. > in AddRelationNotNullConstraints > for (int outerpos = 0; outerpos < list_length(old_notnulls); outerpos++) > { > } > CookedConstraint struct already has "int inhcount;" > can we rely on that, rather than using add_inhcount? > we can also add an Assert: "Assert(!cooked->is_no_inherit);" I'm not sure that works, because if your parent has two parents, you don't want to add two -- you still have only one immediate parent. I think the best way to check for correctness is to set up a scenario where you would have that cooked->inhcount=2 (using whatever CREATE TABLEs are necessary) and then see if ALTER TABLE NO INHERIT reach the correct count (0) when all [immediate] parents are detached. But anyway, keep in mind that inhcount keeps the number of _immediate_ parents, not the number of ancestors. > /* > * Remember primary key index, if any. We do this only if the index > * is valid; but if the table is partitioned, then we do it even if > * it's invalid. > * > * The reason for returning invalid primary keys for foreign tables is > * because of pg_dump of NOT NULL constraints, and the fact that PKs > * remain marked invalid until the partitions' PKs are attached to it. > * If we make rd_pkindex invalid, then the attnotnull flag is reset > * after the PK is created, which causes the ALTER INDEX ATTACH > * PARTITION to fail with 'column ... is not marked NOT NULL'. With > * this, dropconstraint_internal() will believe that the columns must > * not have attnotnull reset, so the PKs-on-partitions can be attached > * correctly, until finally the PK-on-parent is marked valid. > * > * Also, this doesn't harm anything, because rd_pkindex is not a > * "real" index anyway, but a RELKIND_PARTITIONED_INDEX. > */ > if (index->indisprimary && > (index->indisvalid || > relation->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)) > { > pkeyIndex = index->indexrelid; > pkdeferrable = !index->indimmediate; > } > The comment (especially paragraph "The reason for returning invalid > primary keys") is overwhelming. > Can you also add some sql examples into the comments. > I guess some sql examples, people can understand it more easily? Ooh, thanks for catching this -- this comment is a leftover from previous idea that you could have PKs without NOT NULL. I think it mostly needs to be removed, and maybe the whole "if" clause put back to its original form. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "If it is not right, do not do it. If it is not true, do not say it." (Marcus Aurelius, Meditations)