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 1qPL5N-00AiYb-Qi for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Jul 2023 10:52:41 +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 1qPL5M-00FLel-7y for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Jul 2023 10:52:40 +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 1qPL2H-00FHqd-23 for pgsql-hackers@lists.postgresql.org; Fri, 28 Jul 2023 10:49:29 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qPL2D-000rm8-79 for pgsql-hackers@lists.postgresql.org; Fri, 28 Jul 2023 10:49:28 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id A1A2A5C01B5; Fri, 28 Jul 2023 06:49:21 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 28 Jul 2023 06:49:21 -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:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1690541361; x= 1690627761; bh=KZCP6bF/jB2mIMeOmca2I1Zb33LTftZ5tY6xHcUoL94=; b=o Tdjn7xQ+MEo49Y1qajuebGu2za0MOyD7FIx8O2eFN2RvISL+wWw4h909pU3EAlDu lZWM0xsoB/Mi5XWvKNr+xO+WFBZ2fgURAZ/t+l1gKJFDJmKUJGBkB7KxhEMv7+x1 x4hjEaWF3UdBwvRrEh06ivnCwtxlELhw2jgRo+TzStOffECm0iu5g9sCiVQSEAnV YKp+xoL9xquA0QBA7Nqx37oVjmuKSlK1GG17c2e/mOt5s4ui2++STOu5T/GZxcRx bwnmR9Zm9N+LfrA3t0snUdKpQ/UGZteMGlBFGDlr2cWi2lME/Pb2OwyraUSJIE4V h0/0P6UZi2OtZ9HBd6SDA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrieeigdeftdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfggtggugfgjsehtkeertddttdejnecuhfhrohhmpeetlhhvrghr ohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqe enucggtffrrghtthgvrhhnpedvkedtffduffdtffffheffhfejjefhgfeiueeukeejkeff gfdufffhudffffeuveenucffohhmrghinhepvghnthgvrhhprhhishgvuggsrdgtohhmne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhh vghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 28 Jul 2023 06:49:20 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 873B2E6; Fri, 28 Jul 2023 12:47:44 +0200 (CEST) Date: Fri, 28 Jul 2023 12:47:44 +0200 From: Alvaro Herrera To: Dean Rasheed Cc: Robert Haas , Peter Eisentraut , pgsql-hackers@lists.postgresql.org Subject: Re: cataloguing NOT NULL constraints Message-ID: <20230728104744.wvolubqxph3ag5hh@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230726134939.2zdmokgjwqsiknum@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > > Given the following sequence: > > > > drop table if exists p,c; > > create table p(a int primary key); > > create table c() inherits (p); > > alter table p drop constraint p_pkey; > > However, c.a remains non-nullable, with a NOT NULL constraint that > > claims to be inherited: > > > > \d+ c > > Table "public.c" > > Column | Type | Collation | Nullable | Default | Storage | > > Compression | Stats target | Description > > --------+---------+-----------+----------+---------+---------+-------------+--------------+------------- > > a | integer | | not null | | plain | > > | | > > Not null constraints: > > "c_a_not_null" NOT NULL "a" (inherited) > > Inherits: p > > Access method: heap > > > > That's a problem, because now the NOT NULL constraint on c cannot be > > dropped (attempting to drop it on c errors out because it thinks it's > > inherited, but it can't be dropped via p, because p.a is already > > nullable). So I implemented a fix for this (namely: fix the inhcount to be 0 initially), and it works well, but it does cause a definitional problem: any time we create a child table that inherits from another table that has a primary key, all the columns in the child table will get normal, visible, droppable NOT NULL constraints. Thus, pg_dump for example will output that constraint exactly as if the user had specified it in the child's CREATE TABLE command. By itself this doesn't bother me, though I admit it seems a little odd. When you restore such a setup from pg_dump, things work perfectly -- I mean, you don't get a second constraint. But if you do drop the constraint, then it will be reinstated by the next pg_dump as if you hadn't dropped it, by way of it springing to life from the PK. To avoid that, one option would be to make this NN constraint undroppable ... but I don't see how. One option might be to add a pg_depend row that links the NOT NULL constraint to its PK constraint. But this will be a strange case that occurs nowhere else, since other NOT NULL constraint don't have such pg_depend rows. Also, I won't know how pg_dump likes this until I implement it. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/