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 1tiDzm-00D7qH-L1 for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Feb 2025 14:45:46 +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 1tiDzk-009kP8-GG for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Feb 2025 14:45:45 +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 1tiDzj-009kOv-IJ for pgsql-hackers@lists.postgresql.org; Wed, 12 Feb 2025 14:45:44 +0000 Received: from fhigh-a7-smtp.messagingengine.com ([103.168.172.158]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tiDzi-000Qe1-0L for pgsql-hackers@postgresql.org; Wed, 12 Feb 2025 14:45:43 +0000 Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfhigh.phl.internal (Postfix) with ESMTP id C6D6D11401AD; Wed, 12 Feb 2025 09:45:40 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Wed, 12 Feb 2025 09:45:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:cc:content-transfer-encoding:content-type:content-type :date:date:from:from:in-reply-to:in-reply-to:message-id :mime-version:references:reply-to:subject:subject:to:to; s=fm2; t=1739371540; x=1739457940; bh=DB2xmN3a6XZOQaC4CpMJu7ozh1NDdU5J LUgmch4HuoQ=; b=FdHzEfJAX/e2SARPdQhIENXMU5Dv2b0g10mUXItQDU+FcYwS mwKzPmgTG42SkXOqsd/GQcmU0qsQqgqketKogtMeq0700fLAw17p5A+sJEKqqIZW /Bjqa0eS5N57UgePAQmkpDBccpPGIrS14w//shu50dPn6SPa7WkfjVtkX73LlqRE 6Y3ATN1usNii97pDMkRqhzgtUqWLJISSvb0kSbJFI6Cq9Cn+3L9/QaLXI/tU/ZNI gGdM3NXw1BWO4v0+yHOywKeLQL5q7Kynq9Q8jfczm85W9d0hCu8quv6r11gPgp2i BjlpbodA2YzzI9xpsX07j5ZgRuLpXhNdgOppbA== 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 :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1739371540; x= 1739457940; bh=DB2xmN3a6XZOQaC4CpMJu7ozh1NDdU5JLUgmch4HuoQ=; b=K /bwBgAFbAU1BVjXQcYzflgUsx4GWVOpaiChzGDvYXmJOef8mAUQonvjk4UgQaMJn BpWtFqwb0qWhUQ+icpzKyvGnqIyiO2W/H0o0HjL5jegbeKiSQFM0pVK1Qm6DWFeG bO3GsBOq0wJaE2YC91TrU67+zcrNlGDcHeo2rHXDhwOj5nur45kNlssr98YgGKk4 e/53Wdm38MjTY7wzRkQ8We9diXARz7Ac1ksszjuC8AE5fNjsLHs5mpNYdnjWgSPa YSIg9yJmphTpfjKdPER/X1Ko/Czx4mVASMc4Hqt6TZ5Ox5OA//NxuXwBJp7GKrCa EsY0zBF1jhTY+0SARESWg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdeggeduiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddv jeenucfhrhhomheprfgvthgvrhcugfhishgvnhhtrhgruhhtuceophgvthgvrhesvghish gvnhhtrhgruhhtrdhorhhgqeenucggtffrrghtthgvrhhnpedtjeeujeetvefghedtveeg vdeihfeiheejgedugeehjeevgeduleeuheehkeelhfenucffohhmrghinhepphhoshhtgh hrvghsqhhlrdgvuhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhl fhhrohhmpehpvghtvghrsegvihhsvghnthhrrghuthdrohhrghdpnhgspghrtghpthhtoh epkedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheprghlvhhhvghrrhgvsegrlhhv hhdrnhhoqdhiphdrohhrghdprhgtphhtthhopegrshhhuhhtohhshhdrsggrphgrthdroh hsshesghhmrghilhdrtghomhdprhgtphhtthhopehishgrrggtrdhmohhrlhgrnhgusehg mhgrihhlrdgtohhmpdhrtghpthhtohepshhulhgrmhhulhesghhmrghilhdrtghomhdprh gtphhtthhopehjihgrnhdruhhnihhvvghrshgrlhhithihsehgmhgrihhlrdgtohhmpdhr tghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphhoshhtghhrvghsqhhlrdhorhhgpd hrtghpthhtohepjhhovghlsegtohhmphhilhgvrhdrohhrghdprhgtphhtthhopehsuhhr rghjrdhkhhgrrhgrghgvsegvnhhtvghrphhrihhsvggusgdrtghomh X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 12 Feb 2025 09:45:38 -0500 (EST) Message-ID: <50f46903-20e1-4e23-918c-a6cfdf1a9f4a@eisentraut.org> Date: Wed, 12 Feb 2025 15:45:36 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: NOT ENFORCED constraint feature To: =?UTF-8?Q?=C3=81lvaro_Herrera?= , Ashutosh Bapat Cc: Isaac Morland , Amul Sul , jian he , PostgreSQL-development , Joel Jacobson , Suraj Kharage References: <202502121113.rhieqp66w3sg@alvherre.pgsql> Content-Language: en-US From: Peter Eisentraut In-Reply-To: <202502121113.rhieqp66w3sg@alvherre.pgsql> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12.02.25 12:13, Álvaro Herrera wrote: > On 2025-Feb-12, Ashutosh Bapat wrote: > >> I have been asking a different question: What's the use of >> not-enforced constraints if we don't allow VALID, NOT ENFORCED state >> for them? > > That's a question for the SQL standards committee. They may serve > schema documentation purposes, for example. > https://www.postgresql.eu/events/pgconfeu2024/schedule/session/5677-exploring-postgres-databases-with-graphs/ > >> OTOH, consider an application which "knows" that the constraint is >> valid for the data (either because of checks at application level, or >> because the data was replicated from some other system where the >> cosntraints were applied). It's a natural ask to use the constraints >> for, say optimization, but don't take unnecessary overhead of >> validating them. VALID, NOT ENFORCED state helps in such a scenario. >> Of course an application can misuse it (just like stable marking on a >> function), but well ... they will be penalised for their misuse. > > I disagree that we should see a VALID NOT ENFORCED constraint as one > that can be used for query optimization purposes. This is only going to > bring users pain, because it's far too easy to misuse and they will get > wrong query results, possibly without knowing for who knows how long. I've been digging into the ISO archives for some more background on the intended meaning of this feature. Result: "NOT ENFORCED" just means "off" or "disabled", "could contain anything". You can use this to do data loads, or schema surgery, or things like that. Or just if you want it for documentation. This idea that a not-enforced constraint should contain valid data anyway is not supported by anything I could find written down. I've heard that in discussions, but those could have been speculations. (I still think that could be a feature, but it's clearly not this one, at least not in its default state.) So considering that, I think a three-state system makes more sense. Something like: 1) NOT ENFORCED -- no data is checked 2) NOT VALID -- existing data is unchecked, new data is checked 3) ENFORCED -- all data is checked Transitions: (1) - [ ALTER TABLE ... ALTER CONSTRAINT ... NOT VALID ] -> (2) (1) - [ ALTER TABLE ... ALTER CONSTRAINT ... ENFORCED ] -> (3) (2) - [ ALTER TABLE ... VALIDATE CONSTRAINT ... ] -> (3) (2|3) - [ ALTER TABLE ... ALTER CONSTRAINT ... NOT ENFORCED ] -> (1) (3) - [ ALTER TABLE ... ALTER CONSTRAINT ... NOT VALID ] -> (2)