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.96) (envelope-from ) id 1vTb5X-005XX2-1N for pgsql-bugs@arkaria.postgresql.org; Thu, 11 Dec 2025 07:27:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vTb5W-002iF0-1E for pgsql-bugs@arkaria.postgresql.org; Thu, 11 Dec 2025 07:27:47 +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.96) (envelope-from ) id 1vTb5W-002iEs-09 for pgsql-bugs@lists.postgresql.org; Thu, 11 Dec 2025 07:27:46 +0000 Received: from fout-a1-smtp.messagingengine.com ([103.168.172.144]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vTb5U-0006XK-38 for pgsql-bugs@lists.postgresql.org; Thu, 11 Dec 2025 07:27:45 +0000 Received: from phl-compute-05.internal (phl-compute-05.internal [10.202.2.45]) by mailfout.phl.internal (Postfix) with ESMTP id D929AEC050F; Thu, 11 Dec 2025 02:27:43 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-05.internal (MEProxy); Thu, 11 Dec 2025 02:27:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kurilemu.de; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to; s=fm3; t=1765438063; x= 1765524463; bh=+qa8oedN5bPBfvw56MdWO1lIUCcjoMiKPU31goy6hoY=; b=S 8Iy/RR00IjCImmYmd245EdKi+TAs0f7p9Vc7ez7rjCR66YtuVLlF/k1jYB2c6/Es HiPlSgURe9UTPUzHUC6hILST/SfmZm8n2XZ1zQloHGmh76PtlLovDkq8MeCEHldj 9b4x59gvqsOfNVA4XaWM/izDarUCMeDuA+oiQFPXUP/NaqaAKNrMPc6oO5hgDY2y t/f93ns+KPOWNWG0zksoCreisuGNCjIxLaKNSeSavqGk8R0soJ/iT2/doH/cTUU/ npSYOIF356D9UkQxvjxKdhMVrHeSdPenUUmCYLCg5bFoEm+41+lR08x9j0Zoa994 qQhWgJM0MmApQqOLNWXNw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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-sender:x-me-sender :x-sasl-enc; s=fm1; t=1765438063; x=1765524463; bh=+qa8oedN5bPBf vw56MdWO1lIUCcjoMiKPU31goy6hoY=; b=i/N+5+AvlKs5m8CF9CxmkgqCo54NL UELzR0cBwbeXbxZgSml2XONar2y9GobKqipfXVfgSA81otB8rqjtUdF7GkRrL54S xt2/bKC0oAkHuWs1eFI+E4TGn4cV9V/rZBjfqnXAvXlCnA7OyN8LM9Lten0rB9dZ IFnCJqbgdrEctIKFmvjQbBL7ze36XQHUOQYeC1ML3ZAV7DFTla+Jeool2J2LXKmn /mNFB9GQET+y7TWfqj3vMrzYNv+vh2z1b+wOCTwFhxEz7UbZIIEBdQa39u3TJ88W hQaPDjTt/hb9YZOW6jyAQAm0KohZI4QElQqrAjqiDGxzrLUT/lCSzljyA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvgeejudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecunecujfgurhepfffhvffukfggtggugfgjsehtkeertddttd ejnecuhfhrohhmpemllhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgeskhhu rhhilhgvmhhurdguvgeqnecuggftrfgrthhtvghrnhephefgteehhffhgffhhfejgeduud ejkeeiveelhefgfeeukeehhfduvdeltdeiveelnecuffhomhgrihhnpegvnhhtvghrphhr ihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegrlhhvhhgvrhhrvgeskhhurhhilhgvmhhurdguvgdpnhgspghrtghpthht ohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepmhhsughntghhihhnrgesud eifedrtghomhdprhgtphhtthhopehpghhsqhhlqdgsuhhgsheslhhishhtshdrphhoshht ghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ie3de48e3:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 11 Dec 2025 02:27:43 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kurilemu.de; s=schmee; t=1765438062; bh=57ySA9kxECVFviUix+lZ7J+eSlLSWebjlE0ulDcA6DE=; h=Date:From:To:Subject:In-Reply-To:From; b=jqYv4P9NytfRaqEfsFRl7kZllH8t+SbdPkDiUuZhSK5uVn6hFkZlbbR3VRpywTyc+ 3jAZuLQ62HidMm0C/xgcbmV66goEH9ahFJ975oZHjHm756vpJiHCK8yEa2Rz9MGMiP ETEPOVziGwRzSPgApf8JpfYqY6FtrF2sQzGBrnEYnSPSUamShZWrlCltLDbWHX/yXv BoBHvREdi+8XgdFdWojLzvGznID0jscfamhOM9Xt1sUSVa/6nFR+JO6RUwL/8MdbUz IvSV7qcoTsjOA0LCPSPTH/b2zPpLoMGTe/26+iL2MUdMCA//2ecV7guMsmkldTNqE4 W0g1AtK+O9Akw== Received: by schmee.kurilemu.internal (Postfix, from userid 1000) id 5B7BF78; Thu, 11 Dec 2025 08:27:42 +0100 (CET) Date: Thu, 11 Dec 2025 08:27:42 +0100 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: msdnchina@163.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19351: in pg18.1,when not null exists in the table , and add constraint problem. Message-ID: <202512110716.jpbcheffhdow@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <19351-8f1c523ead498545@postgresql.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2025-Dec-10, PG Bug reporting form wrote: > postgres=# create table test_null_20251210(c1 int not null,c2 text); > CREATE TABLE > postgres=# \d+ test_null_20251210 > 数据表 "public.test_null_20251210" > 栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 压缩 | 统计目标 | 描述 > ------+---------+----------+----------+------+----------+------+----------+------ > c1 | integer | | not null | | plain | | | > c2 | text | | | | extended | | | > Not-null constraints: > "test_null_20251210_c1_not_null" NOT NULL "c1" > 访问方法 heap > > postgres=# alter table test_null_20251210 add constraint xyzxyz not null > c1; > ALTER TABLE > <<<--- above alter table statement should report 'not null constraint > exists', > <<<---- Maybe this problem is a bug ? Yeah, this is more-or-less intentional behavior from commit 14e87ffa5c54 -- the constraint you're trying to add is compatible with the one that already exists, so there's no point in throwing an error. This is consistent with the long-standing behavior of "ALTER TABLE ... SET NOT NULL" not throwing an error. However, there actually is one point of incompatibility: the constraint name doesn't match. I ruled out throwing an error in this case, but maybe we should. If you try to add a NOT VALID constraint you also get no error. (This is because we combine both the existing definition and a proposed NOT VALID constraint and reach the conclusion that a validated constraint is the correct end result). If you try to add an incompatible not-null constraint you do get an error. I think the only case right now is a NO INHERIT constraint. In the future we'll get NOT ENFORCED constraint and that should also receive an error: =# alter table test_null_20251210 add constraint xyzxyz not null c1 no inherit; ERROR: cannot change NO INHERIT status of NOT NULL constraint "test_null_20251210_c1_not_null" on relation "test_null_20251210" HINT: You might need to make the existing constraint inheritable using ALTER TABLE ... ALTER CONSTRAINT ... INHERIT. I do note that the HINT in this case is wrong, and I'll go fix it. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom." (Isaac Asimov)