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 1srCUO-00G3X4-VC for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Sep 2024 08:26: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 1srCUM-006eSf-IQ for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Sep 2024 08:26:10 +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 1srCUM-006eSW-0M for pgsql-hackers@lists.postgresql.org; Thu, 19 Sep 2024 08:26:10 +0000 Received: from fout8-smtp.messagingengine.com ([103.168.172.151]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1srCUG-001wJj-PS for pgsql-hackers@lists.postgresql.org; Thu, 19 Sep 2024 08:26:08 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfout.phl.internal (Postfix) with ESMTP id 919D61380145; Thu, 19 Sep 2024 04:26:03 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Thu, 19 Sep 2024 04:26:03 -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=1726734363; x= 1726820763; bh=fl0k0BOXUIJ1SD3bzp9hbNPpVhWh2vEU3fnXh1DNzJs=; b=M Jc8FWucA+pb9/rypo1LU9zjanh/btg6gFuS2gQSpqUUi0sWSDWYVEnREaPN+YtMW XLslMWANsQo21WC5tpcstlZ2T/3G+paE0zLmaTgAMUygUPArsef9nKmxo0XLKD6w Ycm7n1t5P3X5WRUypGs3lIXxfzKZDoU+HUBYfHTn7dvUtGFJ5MOAqkJX2ZrJHrpu /NQU1Hm3WhOLU04r9Ou4Axu70C5Ty8mBTXbo1RWyoX3JKuG8CX/Ge386MNQVfzoe lpCF0kTXaT3dWcUbwXelynPAV5AaGztLC9X1Epr3x+zQoVg9yb27DwHc79dL1mOD ENwfx1VGeqsFHDmSqiiEw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeluddgtddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeen ucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffh jeejhffgieeuueekjeekfffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprh gtphhtthhopeefpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjihgrnhdruhhn ihhvvghrshgrlhhithihsehgmhgrihhlrdgtohhmpdhrtghpthhtohepthhnughrfigrnh hgsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrsheslhhi shhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 19 Sep 2024 04:26:02 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1726734360; bh=fqrdQCRdxquzAbDUUeuCFxynvFSR1vtVfIWd7KWb46s=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=hGaUuZxHpjjuvk5zNEi3ukZuAjYhGLIcI2aaDlEd8IZeZFtp3uvEGi4AEz6Uesqpo rrBEUZYQYg5/GeNqtR+yNh+OUB2l3IJgtZF19SNyq3/39hHPGR94WJM22GVAeekuyp B6KZyJnd7QcxvUhP3hyOfGw/9+9K/rctEOetghsXHmLdAeNpt/drJIRrD7Ao8DE1fj m3rfJoJHwyFI6ThN6QvVt5V2jJ/Z+DKIcTBd1zLuu3E2rRNjvQZxiKlssY910NxJPd 4pmBJ9KumsZD2xAXZ4RLlCdXkdWa7nVcjJO1HodltJU+2fsUmMthgHltTav+K6Jlrm M1/b8EAqHsbnA== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 50F83B9; Thu, 19 Sep 2024 10:26:00 +0200 (CEST) Date: Thu, 19 Sep 2024 10:26:00 +0200 From: Alvaro Herrera To: jian he Cc: Tender Wang , Pg Hackers Subject: Re: not null constraints, again Message-ID: <202409190826.qty7bw7d6uce@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-19, jian he wrote: > still based on v3. > in src/sgml/html/ddl-partitioning.html > << Both CHECK and NOT NULL constraints of a partitioned table are always > inherited by all its partitions. > CHECK constraints that are marked NO INHERIT are not allowed to be > created on partitioned tables. > You cannot drop a NOT NULL constraint on a partition's column if the > same constraint is present in the parent table. > << we can change > "CHECK constraints that are marked NO INHERIT are not allowed to be > created on partitioned tables." > to > "CHECK and NOT NULL constraints that are marked NO INHERIT are not > allowed to be created on partitioned tables." Right. Your proposed text is correct but sounds a bit repetitive with the phrase just prior, and also the next one about inability to drop a NOT NULL applies equally to CHECK constraints; so I modified the whole paragraph to this: Both CHECK and NOT NULL constraints of a partitioned table are always inherited by all its partitions; it is not allowed to create NO INHERIT constraints of those types. You cannot drop a constraint of those types if the same constraint is present in the parent table. > in sql-altertable.html we have: > << ATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT } > If any of the CHECK constraints of the table being attached are marked > NO INHERIT, the command will fail; such constraints must be recreated > without the NO INHERIT clause. > << > create table idxpart (a int constraint nn not null) partition by range (a); > create table idxpart0 (a int constraint nn not null no inherit); > alter table idxpart attach partition idxpart0 for values from (0) to (1000); > > In the above sql query case, > we changed a constraint ("nn" on idxpart0) connoinherit attribute > after ATTACH PARTITION. > (connoinherit from true to false) > Do we need extra sentences to explain it? > here not-null constraint behavior seems to divert from CHECK constraint. Ah, yeah, the docs are misleading: we do allow these constraints to mutate from NO INHERIT to INHERIT. There's no danger in this, because such a table cannot have children: no inheritance children (because inheritance-parent tables cannot be partitions) and no partitions either, because partitioned tables are not allowed to have NOT NULL NO INHERIT constraints. So this can only happen on a standalone table, and thus changing the existing not-null constraint from NO INHERIT to normal does no harm. I think we could make CHECK behave the same way on this point; but in the meantime, I propose this text: If any of the CHECK constraints of the table being attached are marked NO INHERIT, the command will fail; such constraints must be recreated without the NO INHERIT clause. By contrast, a NOT NULL constraint that was created as NO INHERIT will be changed to a normal inheriting one during attach. > drop table if exists idxpart, idxpart0, idxpart1 cascade; > create table idxpart (a int) partition by range (a); > create table idxpart0 (a int primary key); > alter table idxpart attach partition idxpart0 for values from (0) to (1000); > alter table idxpart alter column a set not null; > alter table idxpart0 alter column a drop not null; > alter table idxpart0 drop constraint idxpart0_a_not_null; > > "alter table idxpart0 alter column a drop not null;" > is logically equivalent to > "alter table idxpart0 drop constraint idxpart0_a_not_null;" > > the first one (alter column) ERROR out, > the second success. > the second "drop constraint" should also ERROR out? > since it violates the sentence in ddl-partitioning.html > "You cannot drop a NOT NULL constraint on a partition's column if the > same constraint is present in the parent table." Yeah, I modified this code already a few days ago, and now it does error out like this ERROR: cannot drop inherited constraint "idxpart0_a_not_null" of relation "idxpart0" Anyway, as I mentioned back then, the DROP CONSTRAINT didn't _actually_ remove the constraint; it only marked the constraint as no longer locally defined (conislocal=false), which had no practical effect other than changing the representation during pg_dump. Even detaching the partition after having "dropped" the constraint would make the not-null constraint appear again as coninhcount=0,conislocal=true rather than drop it. Speaking of pg_dump, I'm still on the nightmarish trip to get it to behave correctly for all cases (esp. for pg_upgrade). It seems I tripped up on my own code from the previous round, having under-commented and misunderstood it :-( -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/ "The eagle never lost so much time, as when he submitted to learn of the crow." (William Blake)