Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oJOl9-0008JP-Ci for pgsql-general@arkaria.postgresql.org; Thu, 04 Aug 2022 00:30:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oJOl6-0006It-5c for pgsql-general@arkaria.postgresql.org; Thu, 04 Aug 2022 00:30:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oJOl5-0006Ij-OF for pgsql-general@lists.postgresql.org; Thu, 04 Aug 2022 00:30:39 +0000 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oJOl2-0002VT-NH for pgsql-general@lists.postgresql.org; Thu, 04 Aug 2022 00:30:39 +0000 Received: by mail-oo1-xc2c.google.com with SMTP id u7-20020a4a8c07000000b00435ac1584a6so3279386ooj.1 for ; Wed, 03 Aug 2022 17:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:to:content-language:from :subject; bh=jBdiDzC63AfmdCReowHV27AWYM9oZqaDK8w1CLrbHr4=; b=PTKeelK8qQHCAREHmwsr5Sx5mtr8njVMm3bsR6/oSfWCYc9uUdag7tNZc0XI00REqU 3gvWyL0J5TKPU/IdIzTbKVXdX8vheQBOM0dFdkqJ4SH1wKpLSWvodF9QeJEhxqKak2/E wCwg+dqJfvK1WnYW44yPko6s3RmeM5m2tVC0dPwfQyGOuhETsT6nuWPGyKu/dqeXY7Lk HYAOGanllK0kDz+FzIaACXU0jF0qrB2DBuCxLCKDn1KywzLW1Udc6Rm9yKVN+F/At8fg 4VEYrNUGaOWR1lMssJti+cf2S5zxxSvuNvP8CjdOnP95HTiLjkjohmdfN4PtflYJ+jo7 bv5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:to :content-language:from:subject; bh=jBdiDzC63AfmdCReowHV27AWYM9oZqaDK8w1CLrbHr4=; b=i3Stc9RgiLnD1JfUqH5bFdPnp2sh3fAQ7Xnn4fv/roxFpwjbJ/qclh+TWCgGbDkRKF ndl9+I3KxKgZuud4neoM6QhMEn6BjnEOPwAKVgS+8hhNmGzMjrFwKfyorVM7FSlc9GFk G3GQFGiQFyAE81O/N0myxZMQ3eJ653nATXZ3qco32YXancWXOMtuN2lbm02ZVr91jj/k Oa0Sg6I67lRzksbfRMfyABA4rVNUDKd7d2LNLv32Okpu3SlDaLnwzO75gYXx6X1sji2R CwAhQXcN9cncEXVL279FvnLaQnOjtfr8CRQfSLyI2g+o6pyvpIGezEMMU8mtH7HvsmlZ 7dcw== X-Gm-Message-State: AJIora/lR0jkAoxWBjiy/sGA4Yv3pBdOby7FHBXOR0erhtRuC1nfz/H+ NMCFxKFrFRHSr4ZOYzDrojihGiprrr0= X-Google-Smtp-Source: AGRyM1uZdjKV2/QwaTB2Qn7pnFuMjQmwIrvBhjeVam+hgzuDOYVDLBesT3mSfnJOfI+4QbbbnWtEnQ== X-Received: by 2002:a4a:be06:0:b0:435:8b69:e35b with SMTP id l6-20020a4abe06000000b004358b69e35bmr9036843oop.78.1659573033402; Wed, 03 Aug 2022 17:30:33 -0700 (PDT) Received: from [192.168.88.10] (ip68-11-68-85.no.no.cox.net. [68.11.68.85]) by smtp.googlemail.com with ESMTPSA id h26-20020a9d641a000000b0061c530ec022sm4289070otl.79.2022.08.03.17.30.32 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Aug 2022 17:30:32 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------I3ro01YnB6jrn6g0fp04UckB" Message-ID: Date: Wed, 3 Aug 2022 19:30:31 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 To: "pgsql-generallists.postgresql.org" Content-Language: en-US From: Ron Subject: Purpose of DEFERRABLE _and_ INITIALLY DEFERRED foreign key constraint checking? List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------I3ro01YnB6jrn6g0fp04UckB Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit AWS RDS Postgresql 12.10 https://www.postgresql.org/docs/12/sql-createtable.html [quote] |DEFERRABLE| |NOT DEFERRABLE| This controls whether the constraint can be deferred. A constraint that is not deferrable will be checked immediately after every command. *Checking of constraints that are deferrable can be postponed until the end of the transaction*[/quote] [/quote] But yet a |DEFERRABLE| FK constraint in a transaction immediately failed on a FK constraint violation. [quote] |INITIALLY IMMEDIATE| |INITIALLY DEFERRED| If a constraint is deferrable, this clause specifies the default time to check the constraint. If the constraint is|INITIALLY IMMEDIATE|, it is checked after each statement. This is the default. *If the constraint is****|INITIALLY DEFERRED|**, it is checked only at the end of the transaction.* [/quote] INITIALLY DEFERRED solved my problem.  Why do both clauses exist? (A naive interpretation just by looking at the clause words led me to think that INITIALLY DEFERRED would not check record validity when a constraint is *added* to a table, but obviously that's wrong too.) -- Angular momentum makes the world go 'round. --------------I3ro01YnB6jrn6g0fp04UckB Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit AWS RDS Postgresql 12.10

https://www.postgresql.org/docs/12/sql-createtable.html

[quote]
DEFERRABLE
NOT DEFERRABLE

This controls whether the constraint can be deferred. A constraint that is not deferrable will be checked immediately after every command. Checking of constraints that are deferrable can be postponed until the end of the transaction [/quote]

[/quote]

But yet a DEFERRABLE FK constraint in a transaction immediately failed on a FK constraint violation.

[quote]
INITIALLY IMMEDIATE
INITIALLY DEFERRED

If a constraint is deferrable, this clause specifies the default time to check the constraint. If the constraint is INITIALLY IMMEDIATE, it is checked after each statement. This is the default. If the constraint is INITIALLY DEFERRED, it is checked only at the end of the transaction. 

[/quote]

INITIALLY DEFERRED solved my problem.  Why do both clauses exist?

(A naive interpretation just by looking at the clause words led me to think that INITIALLY DEFERRED would not check record validity when a constraint is added to a table, but obviously that's wrong too.)

--
Angular momentum makes the world go 'round.
--------------I3ro01YnB6jrn6g0fp04UckB--