pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jan Wieck <jan@wi3ck.info>
To: Ron <ronljohnsonjr@gmail.com>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Purpose of DEFERRABLE _and_ INITIALLY DEFERRED foreign key constraint checking?
Date: Thu, 4 Aug 2022 19:57:22 -0400
Message-ID: <c2d2e383-de2d-571b-5153-bc911f8874e3@wi3ck.info> (raw)
In-Reply-To: <d7529945-a3a3-f5fe-9c63-08703f2c3825@gmail.com>
References: <d7529945-a3a3-f5fe-9c63-08703f2c3825@gmail.com>

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





view thread (7+ messages)

Message-ID: <c2d2e383-de2d-571b-5153-bc911f8874e3@wi3ck.info>
Permalink:  ../c2d2e383-de2d-571b-5153-bc911f8874e3@wi3ck.info/
Also on:    postgresql.org/message-id/c2d2e383-de2d-571b-5153-bc911f8874e3@wi3ck.info

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: jan@wi3ck.info, ronljohnsonjr@gmail.com
  Subject: Re: Purpose of DEFERRABLE _and_ INITIALLY DEFERRED foreign key constraint checking?
  In-Reply-To: <c2d2e383-de2d-571b-5153-bc911f8874e3@wi3ck.info>
  References: <d7529945-a3a3-f5fe-9c63-08703f2c3825@gmail.com> <c2d2e383-de2d-571b-5153-bc911f8874e3@wi3ck.info>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox