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 1sySnd-00059H-6r for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Oct 2024 09:16:06 +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 1sySme-0001bL-5z for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Oct 2024 09:15:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sySmc-0001au-O6 for pgsql-hackers@lists.postgresql.org; Wed, 09 Oct 2024 09:15:04 +0000 Received: from fout-a8-smtp.messagingengine.com ([103.168.172.151]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sySmY-0008GN-HF for pgsql-hackers@postgresql.org; Wed, 09 Oct 2024 09:15:02 +0000 Received: from phl-compute-11.internal (phl-compute-11.phl.internal [10.202.2.51]) by mailfout.phl.internal (Postfix) with ESMTP id 2885F1380255; Wed, 9 Oct 2024 05:14:57 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-11.internal (MEProxy); Wed, 09 Oct 2024 05:14:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=compiler.org; h= 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=1728465297; x=1728551697; bh=fCpBcC8o9s2P2HbgM8x4JqXgdQTchXANxSe8P/HkKjE=; b= Q3eOz9oiLlGW6oxCy95FLpsl4HXRH6OVEVH+msX7cEEE3clOiwsD8K9S98INjw5l 3o5fCTtTO+Yc4uoAKFHigCMZk77zZN9TFGFLfLuHUvZ6wBtVZhWRLiUWKKR4WpiS j8Ny3TRk47rK/x9HvEpB5sQqsmPXKk1BIyct0LrIclueaIb62W8Z5Y5M8/7HQKnH ww++pkXf2YDim/amMclxqP1RPGhnYbn2bTrrYrcgGh8wX+f5Q7LJ/AjI8ynR/n93 UhZBsn0nC30u2BG+WclK3tmGdaAKnp9szZthau23M+ktaVELsBYrLkw4fXPOB/5H +63uLE5mrVcwKqx7g48Hrg== 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:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1728465297; x= 1728551697; bh=fCpBcC8o9s2P2HbgM8x4JqXgdQTchXANxSe8P/HkKjE=; b=e oQgAh0mvCvzbHYc+Yj5PwrfO86G98Yd8xf7naDJT5dcfuxAhxVKmYkqnB7YrBcY6 yLI26EVRfbUq4c0eRMiBbKG2g3Wu7dwH+vcrJ1PXN+dPCew4QMZ3bvOCLUJZnQmZ uI++PHrmp1L9V4YVxFR/XxuBs0g66TQDmdM7TNDTgvrY4cEgsXLN4pHGHnhblgUh z4ffVYZy/BvWhyfin6723YKa3Z3plHBQuuzAu7JMVET16MtYYkY+PCbRIb1TK8Pk r952Ee8lKH8InDQOGM6Wdc4mRT2Z+u8wazxVcIskcHLzm1NiCctwI6bNSNQTquzh Kbx9jmuiSj0ocRQP+hsdg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdeffedgudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthejredtredttden ucfhrhhomhepfdflohgvlhculfgrtghosghsohhnfdcuoehjohgvlhestghomhhpihhlvg hrrdhorhhgqeenucggtffrrghtthgvrhhnpeevlefftdeiudfgvdfgffdtvdeiveelteei tefgtedtffejtdejfefflefhleffieenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehjohgvlhestghomhhpihhlvghrrdhorhhgpdhnsggprhgt phhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehsuhhlrghmuhhlse hgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphhoshht ghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ic6394509:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id B15D2B00068; Wed, 9 Oct 2024 05:14:56 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 Date: Wed, 09 Oct 2024 11:14:35 +0200 From: "Joel Jacobson" To: "Amul Sul" , pgsql-hackers Message-Id: <239bcb22-3e2b-4d94-be36-a76f554b94df@app.fastmail.com> In-Reply-To: References: Subject: Re: NOT ENFORCED constraint feature Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Oct 8, 2024, at 11:06, Amul Sul wrote: > The attached patch proposes adding the ability to define CHECK and > FOREIGN KEY constraints as NOT ENFORCED. Thanks for working on this! > Adding NOT ENFORCED to CHECK constraints is simple, see 0001 patch, I've looked at the 0001 patch and think it looks simple and straight forward. > but implementing it for FOREIGN KEY constraints requires more code due > to triggers, see 0002 - 0005 patches. I can't say that much yet about the code changes in 0002 - 0005 yet, but I've tested the patches and successfully experimented with the feature. Also think the documentation is good and sound. Only found a minor typo: - Adding a enforced CHECK or NOT NULL + Adding an enforced CHECK or NOT NULL > There are various approaches for > implementing NOT ENFORCED foreign keys, what I thought of: > > 1. When defining a NOT ENFORCED foreign key, skip the creation of > triggers used for referential integrity check, while defining an > ENFORCED foreign key, remain the same as the current behaviour. If an > existing foreign key is changed to NOT ENFORCED, the triggers are > dropped, and when switching it back to ENFORCED, the triggers are > recreated. > > 2. Another approach could be to create the NOT ENFORCED constraint > with the triggers as usual, but disable those triggers by updating the > pg_trigger catalog so that they are never executed for the check. And > enable them when the constraint is changed back to ENFORCED. > > 3. Similarly, a final approach would involve updating the logic where > trigger execution is decided and skipping the execution if the > constraint is not enforced, rather than modifying the pg_trigger > catalog. > > In the attached patch, the first approach has been implemented. This > requires more code changes but prevents unused triggers from being > left in the database and avoids the need for changes all over the > place to skip trigger execution, which could be missed in future code > additions. I also like the first approach, since I think it's nice the pg_trigger entires are inserted / deleted upon enforced / not enforced. > The ALTER CONSTRAINT operation in the patch added code to handle > dropping and recreating triggers. An alternative approach would be to > simplify the process by dropping and recreating the FK constraint, > which would automatically handle skipping or creating triggers for NOT > ENFORCED or ENFORCED FK constraints. However, I wasn't sure if this > was the right approach, as I couldn't find any existing ALTER > operations that follow this pattern. I think the current approach of dropping and recreating the triggers is best, since if we would instead be dropping and recreating the FK constraint, that would cause problems if some other future SQL feature would need to introduce dependencies on the FK constraints via pg_depend. Best regards, Joel