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 1oJkjY-0006jQ-R7 for pgsql-general@arkaria.postgresql.org; Thu, 04 Aug 2022 23:58:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oJkiY-0002X0-Df for pgsql-general@arkaria.postgresql.org; Thu, 04 Aug 2022 23:57:30 +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 1oJkiX-0002Wr-Ur for pgsql-general@lists.postgresql.org; Thu, 04 Aug 2022 23:57:30 +0000 Received: from mail-qt1-x830.google.com ([2607:f8b0:4864:20::830]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oJkiT-0000uM-RL for pgsql-general@lists.postgresql.org; Thu, 04 Aug 2022 23:57:28 +0000 Received: by mail-qt1-x830.google.com with SMTP id bz13so1013448qtb.7 for ; Thu, 04 Aug 2022 16:57:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wi3ck-info.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=5CytqqypGLSCRVbRpKA/HI/Ur9T2fBR3I7Cu80/2DJY=; b=JvZmZFJF48mJPOgvwjttbEibrNE29ZVywmiPMhzejyurOK5gvNgCqgQdRqL7UdV5yU fz/mfQytfZDqkGgkb1wEJeYuQRu2rYMV2xCN9Fw3qyNs+crs7DR+SkPwZgghmZ69hhv+ nNGxkqVrS/W4evSzrvWbylnCGcZ/ZklD71v29OMRUbNaNSJC3BQIsVvSbV27ab1mVezh wuXQ/2lSRbZEaBiRPz1bJnHZVwfw9uieh7Vc818elzR5PioegMry6pf/QYuieeOydzM/ xlDxQpWxs2wQx0EeNduWORFPsXevSZogA9NYcCm9Tv8gRwC2RF2/g8ZMeYkYHuXuhOgI 2gXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=5CytqqypGLSCRVbRpKA/HI/Ur9T2fBR3I7Cu80/2DJY=; b=MAkjnXaql2GtCfnRGKMfvAtFzF9NE+5gipTrzDaCiqtu0Bc9cWFonJzm6A/RWq5d8D EGymkFuicK6ctjG6L68XfZFzOSf6GqJU4FpH4gknmouNGvWXFdncFZGksSAQUx337tEl YswU+kLm/GkLALgywYlimGXLX2e7JLWUFIb6izyIsjXmTtm2UCApN0As6wA5q3d1NSN5 JJEeMLEUuABQTzkZaT6DJTu8xC4a771XLf540r0A+UVlBtlR0QU/aeuNc0PHfGAS+1vy O3ZG/XIdr/SvXv8S2qkOhK6KMwtUJ6V6vUGmQhpXkuiEwyDNpH2ZyFy8jyC4/6KifZDs 06sQ== X-Gm-Message-State: ACgBeo28YVXeTnuM5XjKNK+CyvUgxDYrEpVytKnSb/MbvnC1EEAf+ROS NCpcWENjzB8t7Aee85rFtf20zg== X-Google-Smtp-Source: AA6agR54t24R57rNZSv33ubMsoH/T3p/+ACKR1XXitqrQ9gbafIfqAOQuukIZ+pWhjtsHlPvtRC/LA== X-Received: by 2002:a05:622a:185:b0:342:1aa6:4c21 with SMTP id s5-20020a05622a018500b003421aa64c21mr3046812qtw.397.1659657444458; Thu, 04 Aug 2022 16:57:24 -0700 (PDT) Received: from [172.21.8.10] (pool-98-114-240-226.phlapa.fios.verizon.net. [98.114.240.226]) by smtp.gmail.com with ESMTPSA id n186-20020a3727c3000000b006b5c5987ff2sm1581903qkn.96.2022.08.04.16.57.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Aug 2022 16:57:24 -0700 (PDT) Message-ID: Date: Thu, 4 Aug 2022 19:57:22 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: Purpose of DEFERRABLE _and_ INITIALLY DEFERRED foreign key constraint checking? Content-Language: en-US To: Ron , "pgsql-generallists.postgresql.org" References: From: Jan Wieck In-Reply-To: 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: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? This is as per the Standard. The default is NOT DEFERRABLE and when DEFERRABLE is specified then the default is INITIALLY DEFERRED. This can then be overriden inside a transaction with SET CONSTRAINT so that one or more (or all) DEFERRABLE constraints will be deferred until the end of transaction OR until they are explicitly set to IMMEDIATE again. Setting a previously DEFERRED constraint to IMMEDIATE will immediately run all the queued up checks. This gives the application absolute fine control as to when constraints are checked. The purpose of deferrable constraints is to do things that normally are impossible. Like for example a circular constraint because you want table A and table B to have a guaranteed 1:1 content on their primary key. For every row in A there must be a corresponding row in B and vice versa. This is implemented with two constraints where A and B point at each other. Without deferring those constraints it would be impossible to ever get a single row into either of them. Regards, Jan