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 1qRzGJ-003qrD-Hl for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Aug 2023 18:10:56 +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 1qRzGG-009muK-6C for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Aug 2023 18:10:52 +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 1qRzGF-009muC-8R for pgsql-hackers@lists.postgresql.org; Fri, 04 Aug 2023 18:10:52 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qRzGB-000a44-3X for pgsql-hackers@lists.postgresql.org; Fri, 04 Aug 2023 18:10:50 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id EC0E35C01CE; Fri, 4 Aug 2023 14:10:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 04 Aug 2023 14:10:44 -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=1691172644; x= 1691259044; bh=/TOTgWXqmSPnZI8ERhkUg6WUtY3f4+ByPUvqmMXydes=; b=t JiSf13Y+JTVlx6n02Y/JSjrJJgbifp4c3Ep3xtIwz7g2Ow28iVVpXeOtb6zR6hpV PU3HMxACzpIn1sRVZhVnMmOSo9BQcltvSRkRyxDw2ttmGWfAg3QgXaXxdw1CL+aZ l9nYFazgjuZmqbGx4jDFmOvvXuNjRnQ5AjK4wD8TM1+ZFTZYk/g1mEnke0PGCvcb OXrvonHc8l7uSb0nPPPCv2INqJZbb3ORkuD/M7Zr/4udGp6rTB13iFnJmk9Rmank nzQ51a5GdClzzMpNkisBTTDx2R75f0qhY3aADQztsQorkoBNp5aPNFI7P4KN3r+L gPgwp7kWWfTddRDYMUymw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrkeeggdduvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgr rhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh eqnecuggftrfgrthhtvghrnheptddukeduteeufeevvdeufeelteeifeeijeekteetkeek vedtudduieetjefggfetnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomh dphhgrthgvshdqshhofhhtfigrrhgvrdgtohhmnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiph drohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 4 Aug 2023 14:10:43 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 11DABE3; Fri, 4 Aug 2023 20:10:42 +0200 (CEST) Date: Fri, 4 Aug 2023 20:10:42 +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: <20230804181042.3hydj5sy2aeapdia@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230728104744.wvolubqxph3ag5hh@alvherre.pgsql> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2023-Jul-28, Alvaro Herrera wrote: > 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. I've been completing the implementation for this. It seems to work reasonably okay; pg_dump requires somewhat strange contortions, but they are similar to what we do in flagInhTables already, so I don't feel too bad about that. What *is* odd and bothersome is that it also causes a problem dropping the child table. For example, CREATE TABLE parent (a int primary key); CREATE TABLE child () INHERITS (parent); \d+ child Tabla «public.child» Columna │ Tipo │ Ordenamiento │ Nulable │ Por omisión │ Almacenamiento │ Compresión │ Estadísticas │ Descripción ─────────┼─────────┼──────────────┼──────────┼─────────────┼────────────────┼────────────┼──────────────┼───────────── a │ integer │ │ not null │ │ plain │ │ │ Not null constraints: "child_a_not_null" NOT NULL "a" Hereda: parent Método de acceso: heap This is the behavior that I think we wanted to prevent drop of the child constraint, and it seems okay to me: =# alter table child drop constraint child_a_not_null; ERROR: cannot drop constraint child_a_not_null on table child because constraint parent_pkey on table parent requires it SUGERENCIA: You can drop constraint parent_pkey on table parent instead. But the problem is this: =# drop table child; ERROR: cannot drop table child because other objects depend on it DETALLE: constraint parent_pkey on table parent depends on table child SUGERENCIA: Use DROP ... CASCADE to drop the dependent objects too. To be clear, what my patch is doing is add one new dependency: dep │ ref │ deptype ────────────────────────────────────────────┼────────────────────────────────────────┼───────── type foo │ table foo │ i table foo │ schema public │ n constraint foo_pkey on table foo │ column a of table foo │ a type bar │ table bar │ i table bar │ schema public │ n table bar │ table foo │ n constraint bar_a_not_null on table bar │ column a of table bar │ a constraint child_a_not_null on table child │ column a of table child │ a constraint child_a_not_null on table child │ constraint parent_pkey on table parent │ i the last row here is what is new. I'm not sure what's the right fix. Maybe I need to invert the direction of that dependency. Even with that fixed, I'd still need to write more code so that ALTER TABLE INHERIT adds the link (I already patched the DROP INHERIT part). Not sure what else might I be missing. Separately, I also noticed that some code that's currently dropconstraint_internal needs to be moved to DropConstraintById, because if the PK is dropped for some other reason than ALTER TABLE DROP CONSTRAINT, some ancillary actions are not taken. Sigh. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ Are you not unsure you want to delete Firefox? [Not unsure] [Not not unsure] [Cancel] http://smylers.hates-software.com/2008/01/03/566e45b2.html