Re: DEFERRABLE NOT NULL constraint

From: Andreas Joseph Krogh <andreak(at)officenet(dot)no>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: DEFERRABLE NOT NULL constraint
Date: 2013-02-05 11:41:21
Message-ID: OrigoEmail.177.31dd368d29b309c1.13caa28b34f@prod2.officenet.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<div>På tirsdag 05. februar 2013 kl. 11:26:20, skrev Alban Hertroys &lt;<a href="mailto:haramrae(at)gmail(dot)com" target="_blank">haramrae(at)gmail(dot)com</a>&gt;:</div>

<blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">On 5 February 2013 11:15, Andreas Joseph Krogh <span dir="ltr">&lt;<a href="mailto:andreak(at)officenet(dot)no" target="_blank">andreak(at)officenet(dot)no</a>&gt;</span> wrote:

<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>På tirsdag 05. februar 2013 kl. 11:04:27, skrev Thomas Kellerer &lt;<a href="mailto:spam_eater(at)gmx(dot)net" target="_blank">spam_eater(at)gmx(dot)net</a>&gt;:</div>

<div class="im">
<blockquote style="border-left:1px solid rgb(204,204,204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
<div style="display:inline;font-family:monospace;font-size:12px">Andreas Joseph Krogh, 05.02.2013 10:57:<br>
&gt; The value of having NOT NULL deferrable is, well, to not check for<br>
&gt; NULL until the tx commits. When working with ORMs this often is the<br>
&gt; case, especially with circular FKs.<br>
<br>
With circular FKs it's enough to define the FK constraint as deferred.</div>
</blockquote>

<div> </div>
</div>

<div>I meant; circular FKs which are also NOT NULL </div>
</blockquote>

<div> </div>

<div> If you would use that, every pair of circular inserts would require 2 inserts and an update (=insert &amp; delete in MVCC):</div>

<div> </div>

<div>1; insert node 1 with FK null,</div>

<div>2; insert node 2 referencing node1,</div>

<div>3; update node 1 with FK to node 2.</div>
</div>
 

<div>OTOH, when you decide the FK from node 1 to node 2 before inserting node 1 and have the FK constraint(s) deferrable, then you only need to insert both records:</div>

<div> </div>

<div>1; decide FK key from node 1 to node 2,</div>

<div>2; insert node 1 referencing node 2,</div>

<div>3; insert node 2 referencing node 1</div>

<div> </div>

<div>This case typically only occurs when you're using surrogate keys, but even in that case you can select nextval(...).</div>

<div> </div>

<div>The deferred FK approach has the benefit that you don't create 3 copies of the record for node 1, so table and index bloat will be less.</div>
--<br>
If you can't see the forest for the trees,<br>
Cut the trees and you'll see there is no forest.</blockquote>

<div> </div>

<div>There are lots of things you can do, but when it's the ORM which does it you have limited control, and that's the way it should to be (me as application-developer having to worry less about such details).</div>

<div> </div>

<div class="origo-email-signature">--<br>
Andreas Joseph Krogh &lt;andreak(at)officenet(dot)no&gt;      mob: +47 909 56 963<br>
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no<br>
Public key: http://home.officenet.no/~andreak/public_key.asc</div>

<div> </div>

Attachment Content-Type Size
unknown_filename text/html 2.9 KB

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2013-02-05 12:32:15 Re: DEFERRABLE NOT NULL constraint
Previous Message Raghavendra 2013-02-05 11:22:28 Re: COPY FROM on Windows and accentuated characters in the file path