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 1tvICn-003AUK-IQ for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Mar 2025 15:53:14 +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 1tvICe-003n6K-JZ for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Mar 2025 15:53:04 +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 1tvICe-003n2b-7X for pgsql-hackers@lists.postgresql.org; Thu, 20 Mar 2025 15:53:04 +0000 Received: from fout-b8-smtp.messagingengine.com ([202.12.124.151]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tvICc-0009jr-1z for pgsql-hackers@postgresql.org; Thu, 20 Mar 2025 15:53:03 +0000 Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.stl.internal (Postfix) with ESMTP id A576411401E5; Thu, 20 Mar 2025 11:53:01 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Thu, 20 Mar 2025 11:53:01 -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-sender :x-me-sender:x-sasl-enc; s=fm1; t=1742485981; x=1742572381; bh=X 674DGSEiyRIPYpCn1r5o16dmeZVlO/7slpBPPFP+j0=; b=vMwA37lrg2wna7hsY bEeTgPcoTvVbn7Ifdvzg1ZRH5eXNFMDu5sbnG0zofgVwd6GWS17bUV/bnvC4ggf4 ExiNOKWmpG0r5DSg5yY/M+6dUa1WIEGgY130TXfeHhcoqnisJ1bfcXTpJ7CDUuCS xCo5X7cUJ30Z/pSdPD67nz8GSJqxig2IYEfJIlt8h+43ZylS7sr1Ad1BQ1/fN+sR FSX5Bm51TjzfStoei3/2f2FikQSehk2kchR+/TRld5nw6+DZA0JuG4L89SDZejwm VcNMjge9nT7pwihIqrMjKwYXpJXG07gnlJCXOIaw7ixWV37Z6drE1T/SI3a4kZHb ZyBSQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddugeekiedvucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesthekredttddt jeenucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlh hvhhdrnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffeh fffhjeejhffgieeuueekjeekfffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvg hrphhrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsg gprhgtphhtthhopeegpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegrshhhuhht ohhshhdrsggrphgrthdrohhsshesghhmrghilhdrtghomhdprhgtphhtthhopehjihgrnh druhhnihhvvghrshgrlhhithihsehgmhgrihhlrdgtohhmpdhrtghpthhtoheprhhushhh rggshhdrlhgrthhhihgrsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhh grtghkvghrshesphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 20 Mar 2025 11:53:00 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1742485977; bh=DHLzD2vDajn7sVngSHWfAcetpBfTsxpCOdWFNoMmWQI=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=Bd1OubevVaOXJ2KCqMQPFIvY24h++MhEh8Wo67dOQeQBpng3e8COxnq7Q5KOcy0BU +nWY8jeZz1WXko72pPRv4us6oXl22mtoHMfuKW6U437+v1vgP++EQIqeXvEpEIo79p yz5TJH9c/iEK6RWeEEc46RAdZjRqV5zoHkmOBTn12fjcxsPNGbnLknOXqwShlTvm2j cz+4MetfxPA/0K8sprWvFP/i6CPyqufXWFxy1dbenYQ92L6OZ3M1g+DUnKh4IBP0yR dt/YAIfrIrecbN2SJ964qV71JEb7uNKwirN01/7Rzf/ggIcPHA2CI2butIFM3fr1ch ksM4hnSM7UZ6g== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 2650887; Thu, 20 Mar 2025 16:52:57 +0100 (CET) Date: Thu, 20 Mar 2025 16:52:57 +0100 From: Alvaro Herrera To: jian he Cc: Ashutosh Bapat , Rushabh Lathia , PostgreSQL Hackers Subject: Re: Support NOT VALID / VALIDATE constraint options for named NOT NULL constraints Message-ID: <202503201552.civdhzzi5zc6@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 2025-Mar-20, jian he wrote: > > Is it expected that a child may have VALID constraint but parent has > > not valid constraint? > > but the MergeConstraintsIntoExisting logic is when > ALTER TABLE ATTACH PARTITION, > it expects the child table to also have an equivalent constraint > definition on it. > see MergeConstraintsIntoExisting: > ereport(ERROR, > (errcode(ERRCODE_DATATYPE_MISMATCH), > errmsg("child table is missing constraint \"%s\"", > NameStr(parent_con->conname)))); > > So I decided not to support it. > * partitioned table can not have NOT NULL NOT VALID. I'm not sure I understand what you're saying here. I think a partitioned table can be allowed to have a NOT VALID constraint. BUT if it does, then all the children must have a corresponding constraint. The constraint on children may be valid or may be invalid; the parent doesn't force the issue one way or the other. But it has to exist. Also, if you run ALTER TABLE VALIDATE CONSTRAINT on the parent, then at that point you have to validate that all those corresponding constraints on the children are also validated. > * one column one NOT NULL, if you want to change status, it's not > allowed, it will error out, give you hints. I think we discussed this already. If you say ALTER TABLE .. ALTER COLUMN .. SET NOT NULL and an invalid constraint exists, then we can simply validate that constraint. However, if you say ALTER TABLE .. ADD CONSTRAINT foobar NOT NULL col; and an invalid constraint exists whose name is different from foobar, then we should raise an error, because the user's requirement that the constraint is named foobar cannot be satisfied. If the constraint is named foobar, OR if the user doesn't specify a constraint name ALTER TABLE .. ADD NOT NULL col; then it's okay to validate that constraint without raising an error. The important thing being that the user requirement is satisfied. > * it can only be added using ALTER TABLE, not with CREATE TABLE (a > warning will be issued) I think the issue of adding constraints with NOT VALID during CREATE TABLE is the topic of another thread. We already silently ignore the NOT VALID markers during CREATE TABLE for other types of constraints. > * pg_attribute.attinvalidnotnull meaning: this attnum has a > (convalidated == false) NOT NULL pg_constraint entry to it. > * if attnotnull is true, then attinvalidnotnull should be false. > Conversely, if attinvalidnotnull is true, then attnotnull should be false. I don't like this. It seems baroque and it will break existing applications, because they currently query for attnotnull and assume that inserting a null value will work, but in reality it will fail because attinvalidnotnull is true (meaning an invalid constraint exists, which prevents inserting nulls). I think the idea should be: attnotnull means that a constraint exists; it doesn't imply anything regarding the constraint being valid or not. attnotnullvalid will indicate whether the constraint is valid; this column can only be true if attnotnull is already true. > * an invalid not-null cannot be used while adding a primary key. Check. > * if attinvalidnotnull is true, this column can not accept NULL values, > but the existing column value may contain NULLs, we need to > VALIDATE the not-null constraint to check if this column exists NULL > values or not. Check. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/