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 1oJPQh-0002Ja-L3 for pgsql-general@arkaria.postgresql.org; Thu, 04 Aug 2022 01:13:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oJPQg-0004vY-GB for pgsql-general@arkaria.postgresql.org; Thu, 04 Aug 2022 01:13:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oJPQg-0004qB-2W for pgsql-general@lists.postgresql.org; Thu, 04 Aug 2022 01:13:38 +0000 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oJPQd-0006e2-D7 for pgsql-general@lists.postgresql.org; Thu, 04 Aug 2022 01:13:37 +0000 Received: by mail-oi1-x233.google.com with SMTP id n133so21798040oib.0 for ; Wed, 03 Aug 2022 18:13:35 -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:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=/HJpr7AE5ZfP7HO5q3rxZeOnJFSUQ0tP8zOUzIbsWlE=; b=cYxPiXBG91sgkPSx9cvaPm2e4tOy+ravPiQiMaaKzJdIipWyUPHn46U6bfBQr1EyJG GPebpzrsfaFuPGQhzP2YFKbUIK/W3dw99tJM8vcqywJX5TeaBHqF6eoIina+GWAcomlj R6UaNCdQksBNUo1+JNTcwlyxHjuy0ehAp5L7fDKTm1j38KL+O9PPfuaCcnkHfXHjE5Zd YK/BBvvkzVoL2zdQJcN54w8eSGYIxM4AkNpe7O8QKLnX8lm7MbV72Z0MMJQMZvExUyod Kt0nJvR85BnhylgWLAsPr9s4wKA6pCfAdMRm+0C8qfEJuohOpAsfxkPFMGJFXOkC+/AU KB2Q== 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:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=/HJpr7AE5ZfP7HO5q3rxZeOnJFSUQ0tP8zOUzIbsWlE=; b=ywZzdF+D60Irgy+vZHxFsJ+UIVvi2T2dXSQOm05LG88QRJsMtEElRFPzWbpeTky1W5 ys6LFRQKgt5voZloE23AXCvlNd3fMRjQACO50iQrjZd228YAthrw03inEr6RvufKty96 sgZUCnGYtQbm7xWyB1Pc6gevufpQqVbVr3a9AkGupU2prTzDU6f70Xk5yRj0QJLWi2nW 3hF8DrGBWWzHebsRJ/RcorYH4YTO2eZLkePUVslE/M2Pgcxiy5rgBBYOV5ITysr59s0H Rh4WDxCnVBojKsoKNSCu4d8NVjRGnHCXIELbD4jnKFjR/mdct8t73iDMdhv157efKXTU fC3Q== X-Gm-Message-State: ACgBeo0u3S2cSojxsWv/2tyXiiK+AFsZxP0U2vMQk2Ww5wpqrevgZ1ts oC4qJr9PBdj6M37c3NMBgGwQemJhLnE= X-Google-Smtp-Source: AA6agR6vyHPV9h4OmCGZRpDUvXOoQiBn8fwfc7JT+oSRoimkyxvcrCuqvOQ4dOw0DEQAgq/9vG2kMg== X-Received: by 2002:aca:d882:0:b0:33a:432:ac59 with SMTP id p124-20020acad882000000b0033a0432ac59mr2872093oig.282.1659575614697; Wed, 03 Aug 2022 18:13:34 -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 f23-20020a9d0397000000b00636968be9f3sm79433otf.34.2022.08.03.18.13.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Aug 2022 18:13:34 -0700 (PDT) Message-ID: Date: Wed, 3 Aug 2022 20:13:33 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: Purpose of DEFERRABLE _and_ INITIALLY DEFERRED foreign key constraint checking? Content-Language: en-US To: Adrian Klaver , "pgsql-generallists.postgresql.org" References: <0317c0c5-a7e5-9073-01b9-80391491108b@aklaver.com> From: Ron In-Reply-To: <0317c0c5-a7e5-9073-01b9-80391491108b@aklaver.com> 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 8/3/22 20:02, Adrian Klaver wrote: > On 8/3/22 17:30, Ron wrote: >> 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? > > Because from the same page: > > [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ] > > and > > 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 (using the SET CONSTRAINTS command). NOT DEFERRABLE is > the default. Currently, only UNIQUE, PRIMARY KEY, EXCLUDE, and REFERENCES > (foreign key) constraints accept this clause. NOT NULL and CHECK > constraints are not deferrable. Note that deferrable constraints cannot be > used as conflict arbitrators in an INSERT statement that includes an ON > CONFLICT DO UPDATE clause. > > > 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. The > constraint check time can be altered with the SET CONSTRAINTS command. > > > So the default > > NOT DEFERRABLE > > and: > > "A constraint that is not deferrable will be checked immediately after > every command." > > When you do > > DEFERRABLE > > the default is > > INITIALLY IMMEDIATE > > You have to explicitly set: > > INITIALLY DEFERRED. And https://www.postgresql.org/docs/12/sql-set-constraints.html seems to say that SET CONSTRAINTS can make DEFERRABLE FKs temporarily not deferrable. What's the point? -- Angular momentum makes the world go 'round.