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 1s6YpE-000Kbw-OU for pgsql-hackers@arkaria.postgresql.org; Mon, 13 May 2024 16:46:58 +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 1s6YoG-002MEh-P7 for pgsql-hackers@arkaria.postgresql.org; Mon, 13 May 2024 16:45:56 +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 1s6YoF-002M1z-I7 for pgsql-hackers@lists.postgresql.org; Mon, 13 May 2024 16:45:56 +0000 Received: from wfhigh2-smtp.messagingengine.com ([64.147.123.153]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6Yo6-0002Sr-P6 for pgsql-hackers@lists.postgresql.org; Mon, 13 May 2024 16:45:54 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfhigh.west.internal (Postfix) with ESMTP id 99CF31800096; Mon, 13 May 2024 12:45:44 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Mon, 13 May 2024 12:45:45 -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=fm3; t=1715618744; x= 1715705144; bh=3sGZxgdD/qaNzeuQr7hFMQbzPZqwUttSq+lDjGvpbIg=; b=V zoytf1yfkXp6d/iNaRwSK/LAifziaBQONaKmv77fjAsSOE0FhiSUvd4TYu/2CP+a Zld2xQC/rjHOSGG8Xnr7EQ+UpEg5l8HBgCVV33Ue918+qcAiSUoWptdSKaQhxpZG W3lXPFuaw5CsTdNQyWNseQkZUalufUn/cFmawS3RWRCS7MN3CikF6Rka0fpM9q+V 0mNFOhObZHT1Jin+4MLijrqz25RWWYfB/UjJvzQhqN8Mm65jvd/P83E/lwXmN10J FqlY9TgACfSXxNT8egnObdt7REiQl60zySC+TmwEXU2m94fwKwTFX7dUHKxsjPLJ zoqdR5AV/Hq3017UuUJWg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedguddtgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetlhhv rghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorh hgqeenucggtffrrghtthgvrhhnpedvkedtffduffdtffffheffhfejjefhgfeiueeukeej keffgfdufffhudffffeuveenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtoh hmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghl vhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 May 2024 12:45:43 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1715618740; bh=RidYD8LoWWDFPG5n9vy3TL/fvJ8VhGiccFTX9WC9IlQ=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=hSWI5ktlBdubtNhkLWILXfOL3G/fY/QS21az7vShW3rnRGpXe3RYjt4DC07+yJd6O k8cTw5UAIkv8wZiIgogiIarpEz1vshAFXzUAWdWI6e/NkPNTIkqYZ7nMUR+CssdgSo prtcKRY8HI+pKI3lg68lb/VDSPWPPjBZaT8bdBVEPzXTmPy0fhTY47Tab4p8YknF2/ r+5TZjw6G5xaYZXlbiyhbIjy3c6u/j5lmGlUxaoAAxSq6LVd6xFutz98bjk86NePcX dZJoU4DLkzsGOjcen/EAtw2qtf8roz3anqY+EuzFTuy0Z8XnTo2/KHpCsLHpGdWIAX GzQLhTny57W6A== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 59179A4; Mon, 13 May 2024 18:45:40 +0200 (CEST) Date: Mon, 13 May 2024 18:45:40 +0200 From: Alvaro Herrera To: Robert Haas Cc: Kyotaro Horiguchi , exclusion@gmail.com, dean.a.rasheed@gmail.com, andrewbille@gmail.com, peter@eisentraut.org, pgsql-hackers@lists.postgresql.org, Justin Pryzby Subject: Re: cataloguing NOT NULL constraints Message-ID: <202405131645.7pvv4b22npim@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-May-13, Robert Haas wrote: > On Mon, May 13, 2024 at 9:44 AM Alvaro Herrera wrote: > > The problematic point is the need to add NOT NULL constraints during > > table creation that don't exist in the table being dumped, for > > performance of primary key creation -- I called this a throwaway > > constraint. We needed to be able to drop those constraints after the PK > > was created. These were marked NO INHERIT to allow them to be dropped, > > which is easier if the children don't have them. This all worked fine. > > This seems really weird to me. Why is it necessary? I mean, in > existing releases, if you declare a column as PRIMARY KEY, the columns > included in the key are forced to be NOT NULL, and you can't change > that for so long as they are included in the PRIMARY KEY. The point is that a column can be in a primary key and not have an explicit not-null constraint. This is different from having a column be NOT NULL and having a primary key on top. In both cases the attnotnull flag is set; the difference between these two scenarios is what happens if you drop the primary key. If you do not have an explicit not-null constraint, then the attnotnull flag is lost as soon as you drop the primary key. You don't have to do DROP NOT NULL for that to happen. This means that if you have a column that's in the primary key but does not have an explicit not-null constraint, then we shouldn't make one up. (Which we would, if we were to keep an unadorned NOT NULL that we can't drop at the end of the dump.) > So I would have thought that after this patch, you'd end up with the > same thing. At least as I interpret the standard, you wouldn't. > One way of doing that would be to make the PRIMARY KEY depend on the > now-catalogued NOT NULL constraints, and the other way would be to > keep it as an ad-hoc prohibition, same as now. That would be against what [I think] the standard says. > But I don't see why I need to end up with what the patch generates, > which seems to be something like CONSTRAINT pgdump_throwaway_notnull_0 > NOT NULL NO INHERIT. That kind of thing suggests that we're changing > around the order of operations in pg_dump, probably by adding the NOT > NULL constraints at a later stage than currently, and I think the > proper solution is most likely to be to avoid doing that in the first > place. The point of the throwaway constraints is that they don't remain after the dump has restored completely. They are there only so that we don't have to scan the data looking for possible nulls when we create the primary key. We have a DROP CONSTRAINT for the throwaway not-nulls as soon as the PK is created. We're not changing any order of operations as such. > That's not to say that we shouldn't try to make improvements, just > that it may be hard to get right. Sure, that's why this patch has now been reverted twice :-) and has been in the works for ... how many years now? -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/