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 1st39J-00D7tP-88 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2024 10:52:06 +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 1st39I-006DNP-JX for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Sep 2024 10:52:04 +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 1st39H-006DNG-6Q for pgsql-hackers@lists.postgresql.org; Tue, 24 Sep 2024 10:52:04 +0000 Received: from fhigh-a8-smtp.messagingengine.com ([103.168.172.159]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1st39C-000pKS-GP for pgsql-hackers@lists.postgresql.org; Tue, 24 Sep 2024 10:52:02 +0000 Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id C012F11401E9; Tue, 24 Sep 2024 06:51:57 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Tue, 24 Sep 2024 06:51:57 -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=1727175117; x= 1727261517; bh=E/ACqAN+gfzxvJQefrVQ7mtAK0YaH5HJecJPrZcVka0=; b=Z vZQ0OOV+JhR3L9QvBXbOuoNRuN4YJmo7N8d22Iqgv0zlnoXYgDWVrhd7vzOmqX4Q 7SZaOyhl/1I0LcgWQUPAbIgzs5HoA0P8yPSgtWHh+x9GxQGax7OnHWh55ue+wBRm JBzKrudeXexpLG8IMU4qdI6tEvvcVvtFHhOlt8WiJESXOiTy7RFkNYAPkPgOyosD y+HjC9zhZ1p8+lQ5j8SmobQ3GPrzPBDGnrYoEqxyvqaw7dphI4o/k7qvW0XsPksr kynnNaA7Y1WLc7KYoZ9KrQlYCP0SP8rGY2Iiu81l+R/bRpJJ698Odtz3sIZWyJCk f5MN2MtJAxDGdyW8e9Ukw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddtvddgfedvucetufdoteggodetrfdotf 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 06:51:57 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1727175114; bh=MNUBFYrh3VgD6SFIWpQ+4gI2CnyNTo2wkU+QZfElEBI=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=qXKUpWrE8F0vfF6nxe99zUlGPpbd/ViRSeXrkYqzCjfmKFooDPQNB28u8SqmR6y7/ xJP/9n1DauNi2267CzPHMpVpLAnURleAdYP+EAhFFlcqrijCJ6VMuevuCWJ33U1fmL PcITqwZ7nVm0EPvWlT/GN04vQfxhSvLsHwIzAsDq6EbfofDl1oQej+8w8oRHkCPg97 EbfSmqiLNdBGg07G4wPWKea2jJvuBtObIFCNHW6Yi+XQwzEhOXm2Y8Fzec8V2F2HWt 3h5Z1TIG+S7UWXv/laiMu6Vn0NIEhK1MTtxjIloBUIz9OKXXBhKeEYcH5BFfa99ckW OcWI29trQxK9w== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 3F4C4D9; Tue, 24 Sep 2024 12:51:54 +0200 (CEST) Date: Tue, 24 Sep 2024 12:51:54 +0200 From: Alvaro Herrera To: jian he Cc: Tender Wang , Pg Hackers Subject: Re: not null constraints, again Message-ID: <202409241051.xhdeeqazf63f@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 void > set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, bool recurse, > LOCKMODE lockmode) > { > What do you think of the above refactor? > (I intentionally deleted empty new line) Looks nicer ... but you know what? After spending some more time with it, I realized that one caller is dead code (in AttachPartitionEnsureIndexes) and another caller doesn't need to ask for recursion, because it recurses itself (in ATAddCheckNNConstraint). So that leaves us with a grand total of zero callers that need the recursion here ... which means we can simplify it to the case that it only examines a single relation and never recurses. So I've stripped it down to its bare minimum: /* * Helper to set pg_attribute.attnotnull if it isn't set, and to tell phase 3 * to verify it. * * When called to alter an existing table, 'wqueue' must be given so that we * can queue a check that existing tuples pass the constraint. When called * from table creation, 'wqueue' should be passed as NULL. */ static void set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, LOCKMODE lockmode) { Oid reloid = RelationGetRelid(rel); HeapTuple tuple; Form_pg_attribute attForm; CheckAlterTableIsSafe(rel); tuple = SearchSysCacheCopyAttNum(reloid, attnum); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for attribute %d of relation %u", attnum, reloid); attForm = (Form_pg_attribute) GETSTRUCT(tuple); if (!attForm->attnotnull) { Relation attr_rel; attr_rel = table_open(AttributeRelationId, RowExclusiveLock); attForm->attnotnull = true; CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple); if (wqueue && !NotNullImpliedByRelConstraints(rel, attForm)) { AlteredTableInfo *tab; tab = ATGetQueueEntry(wqueue, rel); tab->verify_new_notnull = true; } CommandCounterIncrement(); table_close(attr_rel, RowExclusiveLock); } heap_freetuple(tuple); } -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/