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 1strjs-002HGT-Pv for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Sep 2024 16:53:13 +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 1strjs-00B9gU-13 for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Sep 2024 16:53:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1strjq-00B9cM-Fv for pgsql-hackers@lists.postgresql.org; Thu, 26 Sep 2024 16:53:11 +0000 Received: from fhigh-a2-smtp.messagingengine.com ([103.168.172.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1strjm-001Baf-MB for pgsql-hackers@lists.postgresql.org; Thu, 26 Sep 2024 16:53:08 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfhigh.phl.internal (Postfix) with ESMTP id 281DC1140159; Thu, 26 Sep 2024 12:53:05 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-11.internal (MEProxy); Thu, 26 Sep 2024 12:53:05 -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=1727369585; x= 1727455985; bh=2SQBaA2ljs1MIePUCznrgGPtv7VHeCL6oz1Bj6jiD+U=; b=c M0jdhOua8SG+RzuXdztiTT0TFcJgYJB85ePJRchmEF32B0Fl7r9McwNBo1d5ZGsH 1hSarAhxdS13wU7lXmHLtJaiyi803hn0RkGqCYBQminWTE/XDnOYn2ewn2jWWmmO QgkPS7rB8CJ+C5M5tkwEuG2P7Krg0Gac+fqU6D/oLIbsgWjIr8EryU2R+HqWaEHC s/IJrO+tolunVFHPchXdIE77NNevfj7c814SmpROfZAM+mXVnm6qHsak9YwL2AOp zgu3KYKL3Q2raf34hqXE5EF2pUrGfeqGrgL7wOHn15whnQ2sKJZ4L3RUR0Kvh5qo hH1sJHtyAceSCae4X+qGg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddtjedguddtjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpeffhffvvefukfggtggugfgjsehtkeertddttdej necuhfhrohhmpeetlhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvh hhrdhnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhnpefgtdegleeggffftdeiuddu fffhveekgfdufeethefgtefhkedvgeejlefhteehvdenucffohhmrghinhepvghnthgvrh hprhhishgvuggsrdgtohhmpdhpohhsthhgrhgvshhqlhdrohhrghenucevlhhushhtvghr ufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvh hhrdhnohdqihhprdhorhhgpdhnsggprhgtphhtthhopeefpdhmohguvgepshhmthhpohhu thdprhgtphhtthhopehjihgrnhdruhhnihhvvghrshgrlhhithihsehgmhgrihhlrdgtoh hmpdhrtghpthhtohepthhnughrfigrnhhgsehgmhgrihhlrdgtohhmpdhrtghpthhtohep phhgshhqlhdqhhgrtghkvghrsheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 26 Sep 2024 12:53:04 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1727369581; bh=3UnJhYFnoxCU13NOQRZfhgj6P5WT+hhb4pARmFq6pZY=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=Q4Ba5THTLmgR9HceTXX60DjjfeM/3Ot00BTKnwYXqwLSK9153NdNDctoVFrP3RwrU bDaUEwHieR/JeR+X+BLTKt9m+NzFuoTQ8lVJ/5CCJiQ3xSuPzKMc3W3IGBq3BflfYI ORFyDOZUF1HHUsZD1dDxYM/mKyA0K7//pbSEqYgxAzzBHVYZIE5o6NOlMX0yCFW0fd D8p2hixzQ6eqSmiy6fckGB2HGO2I/9N+B7aDdkMHbhtb+NJH35MxPzu1uvOvRduuwl nGSDUo4YaFN8MTbvuYAUV+EUXDOlAhJwDBFYbN/HKVKaCP0TeMkzvsBFV62/kQH05m Ga6UUW9G+L+ag== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 5B3D4A2; Thu, 26 Sep 2024 18:53:01 +0200 (CEST) Date: Thu, 26 Sep 2024 18:53:01 +0200 From: Alvaro Herrera To: jian he Cc: Tender Wang , Pg Hackers Subject: Re: not null constraints, again Message-ID: <202409261653.zrz32zjsaqh4@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-26, jian he wrote: > +-- a PK in parent must have a not-null in child that it can mark inherited > +create table inh_parent (a int primary key); > +create table inh_child (a int primary key); > +alter table inh_child inherit inh_parent; -- nope > +alter table inh_child alter a set not null; > +alter table inh_child inherit inh_parent; -- now it works > +ERROR: relation "inh_parent" would be inherited from more than once > in src/test/regress/sql/inherit.sql, the comments at the end of the > command, seem to conflict with the output? Outdated, useless -- removed. > ------------------------------------------------------------------------------- > > ALTER TABLE ALTER COLUMN SET NOT NULL > implicitly means > ALTER TABLE ALTER COLUMN SET NOT NULL NO INHERIT. > > So in ATExecSetNotNull > if (conForm->connoinherit && recurse) > ereport(ERROR, > errcode(ERRCODE_FEATURE_NOT_SUPPORTED), > errmsg("cannot change NO INHERIT status of NOT > NULL constraint \"%s\" on relation \"%s\"", > NameStr(conForm->conname), > RelationGetRelationName(rel))); > should be > if (conForm->connoinherit) > ereport(ERROR, > errcode(ERRCODE_FEATURE_NOT_SUPPORTED), > errmsg("cannot change NO INHERIT status of NOT > NULL constraint \"%s\" on relation \"%s\"", > NameStr(conForm->conname), > RelationGetRelationName(rel))); > > then we can avoid the weird case like below: > > drop table if exists pp1; > create table pp1 (f1 int not null no inherit); > ALTER TABLE pp1 ALTER f1 SET NOT NULL; > ALTER TABLE ONLY pp1 ALTER f1 SET NOT NULL; Hmm, I don't understand why you say SET NOT NULL implicitly means SET NOT NULL NO INHERIT. That's definitely not the intention. As I explained earlier, the normal state is that a constraint is inheritable, so if you do SET NOT NULL you want that constraint to be INHERIT. Anyway, I don't see what you see as weird in the commands you list. To me it reacts like this: =# create table pp1 (f1 int not null no inherit); CREATE TABLE =# ALTER TABLE pp1 ALTER f1 SET NOT NULL; ERROR: cannot change NO INHERIT status of NOT NULL constraint "pp1_f1_not_null" on relation "pp1" =# ALTER TABLE ONLY pp1 ALTER f1 SET NOT NULL; ALTER TABLE =# \d+ pp1 Tabla «public.pp1» Columna │ Tipo │ Ordenamiento │ Nulable │ Por omisión │ Almacenamiento │ Compresión │ Estadísticas │ Descripción ─────────┼─────────┼──────────────┼──────────┼─────────────┼────────────────┼────────────┼──────────────┼───────────── f1 │ integer │ │ not null │ │ plain │ │ │ Not-null constraints: "pp1_f1_not_null" NOT NULL "f1" NO INHERIT Método de acceso: heap which seems to be exactly what we want. > ------------------------------------------------------------------------------- > > + else if (rel->rd_rel->relhassubclass && > + find_inheritance_children(RelationGetRelid(rel), NoLock) != NIL) > + { > + ereport(ERROR, > + errcode(ERRCODE_INVALID_TABLE_DEFINITION), > + errmsg("not-null constraint on column \"%s\" must be removed in > child tables too", > + colName), > + errhint("Do not specify the ONLY keyword.")); > + } > this part in ATExecDropNotNull is not necessary? > > per alter_table.sql > <<<<<<---------->>>>>> > -- make sure we can drop a constraint on the parent but it remains on the child > CREATE TABLE test_drop_constr_parent (c text CHECK (c IS NOT NULL)); > CREATE TABLE test_drop_constr_child () INHERITS (test_drop_constr_parent); > ALTER TABLE ONLY test_drop_constr_parent DROP CONSTRAINT > "test_drop_constr_parent_c_check"; > <<<<<<---------->>>>>> > by the same way, we can drop a not-null constraint ONLY on the parent, > but it remains on the child. > if we not remove the above part then > ALTER TABLE ONLY DROP CONSTRAINT > will behave differently from > ALTER TABLE ONLY ALTER COLUMN DROP NOT NULL. > > example: > drop table pp1,cc1, cc2; > create table pp1 (f1 int not null); > create table cc1 (f2 text, f3 int) inherits (pp1); > create table cc2(f4 float) inherits(pp1,cc1); > > alter table only pp1 drop constraint pp1_f1_not_null; --works. > alter table only pp1 alter column f1 drop not null; --- error, should also work. > ------------------------------------------------------------------------------- Hmm. I'm not sure I like this behavior, but there is precedent in CHECK, and since DROP CONSTRAINT also already works that way, I suppose DROP NOT NULL should do that too. I'll get it changed. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "I must say, I am absolutely impressed with what pgsql's implementation of VALUES allows me to do. It's kind of ridiculous how much "work" goes away in my code. Too bad I can't do this at work (Oracle 8/9)." (Tom Allison) http://archives.postgresql.org/pgsql-general/2007-06/msg00016.php