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 1s6W0T-0003l0-Qn for pgsql-hackers@arkaria.postgresql.org; Mon, 13 May 2024 13:46:23 +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 1s6VzV-0000IP-TC for pgsql-hackers@arkaria.postgresql.org; Mon, 13 May 2024 13:45:21 +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 1s6VzU-0000Hy-BD for pgsql-hackers@lists.postgresql.org; Mon, 13 May 2024 13:45:21 +0000 Received: from wfhigh8-smtp.messagingengine.com ([64.147.123.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 1s6Vyx-0000zN-Iy for pgsql-hackers@lists.postgresql.org; Mon, 13 May 2024 13:44:56 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfhigh.west.internal (Postfix) with ESMTP id AFC4018000C7; Mon, 13 May 2024 09:44:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 13 May 2024 09:44: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=1715607884; x= 1715694284; bh=SvndPM6NKQmQvmgl9UDUnW4dOOFlInszboXHPsbDBXk=; b=i K4HYG8GpPgJaWKT2JRI8lVgcgt3W68O9ify/uD+v70+dQBObbSi7O9UwzaE0BTN0 BBsXfZXlqnqhLdAxe/kTVeibX7WUAlRhMVQIa5KAfl40QMrAI3e3ICvxBU3Nwfv1 1mZeH+hapU5MLwaNY5NEzD0f1R8/FdmCI+X8clEvEbx9syuYDhqRgNhpk4flXKe5 yjBIm3t/kJ+CKDa8NmlAgiBWJKWg+cjb8238VWLr9JEB1xC2BXRiNxLHh5RpLLPU nU/ON5ZgfLqtb+opdb954he/SaD0KyIlReXrlijwfsujtDWpMSAIpN04X7KV7DDt N1IHRuGewZGz4NnA3R7jQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffhjeejhffgieeuueekjeek fffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh enucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhv hhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 May 2024 09:44:43 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1715607880; bh=OY8wl1aOPbsaHLgvhdl6G/0OTTovNFVvo1W5IknS/LQ=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=HHc+qq4sgcWsB1SDcVkR39Pfrt0QqeAJB9n+9zgq85UPIlUgz1YUCyHBUgASmfAcK wqbSnZQniIROQMZEJmYx/v2EpAczQzScRxtsEvDRyhWOdXRahwcZwuxKxEC3YNEtxk B5v9vnGYHxrQUatkk9nX1xUjyT9KThKKjDQkXJjfBnNdFMfcZItBGjohv3BAw/chLX J0i8dGzGD0NYLelEBYkyYqqzCK/MMZi5lb+D+R2XfIKDXE6KGat/zIRcO3ce/husOI 0y8Hnih98EZLsopkpvtLoFPwbUBMq2XYJFj9rI0wuL5wsmTCDk0k07q2DLu+XwAvwF 5ortDiLW4zmqQ== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 097E8F1; Mon, 13 May 2024 15:44:40 +0200 (CEST) Date: Mon, 13 May 2024 15:44: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: <202405131344.stk24vg66gxm@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 Sat, May 11, 2024 at 5:40 AM Alvaro Herrera wrote: > > Specifically, the problem is that I mentioned that we could restrict the > > NOT NULL NO INHERIT addition in pg_dump for primary keys to occur only > > in pg_upgrade; but it turns this is not correct. In normal > > dump/restore, there's an additional table scan to check for nulls when > > the constraints is not there, so the PK creation would become measurably > > slower. (In a table with a million single-int rows, PK creation goes > > from 2000ms to 2300ms due to the second scan to check for nulls). > > I have a feeling that any theory of the form "X only needs to happen > during pg_upgrade" is likely to be wrong. pg_upgrade isn't really > doing anything especially unusual: just creating some objects and > loading data. Those things can also be done at other times, so > whatever is needed during pg_upgrade is also likely to be needed at > other times. Maybe that's not sound reasoning for some reason or > other, but that's my intuition. True. It may be that by setting up the upgrade SQL script differently, we don't need to make the distinction at all. I hope to be able to do that. > I'm sorry that I haven't been following this thread closely, but I'm > confused about how we ended up here. What exactly are the user-visible > behavior changes wrought by this patch, and how do they give rise to > these issues? 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. However, at some point we realized that we needed to add NOT NULL constraints in child tables for the columns in which the parent had a primary key. Then things become messy because we had the throwaway constraints on one hand and the not-nulls that descend from the PK on the other hand, where one was NO INHERIT and the other wasn't; worse if the child also has a primary key. It turned out that we didn't have any mechanism to transform a NO INHERIT constraint into a regular one that would be inherited. I added one, didn't like the way it worked, tried to restrict it but that caused other problems; this is the mess that led to the revert (pg_dump in normal mode would emit scripts that fail for some legitimate cases). One possible way forward might be to make pg_dump smarter by adding one more query to know the relationship between constraints that must be dropped and those that don't. Another might be to allow multiple not-null constraints on the same column (one inherits, the other doesn't, and you can drop them independently). There may be others. > The other possibility that occurs to me is that I think the motivation > for cataloging NOT NULL constraints was that we wanted to be able to > track dependencies on them, or something like that, which seems like > it might be able to create issues of the type that you're facing, but > the details aren't clear to me. NOT VALID constraints would be extremely useful, for one thing (because then you don't need to exclusively-lock the table during a long scan in order to add a constraint), and it's just one step away from having these constraints be catalogued. It was also fixing some inconsistent handling of inheritance cases. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/