Re: SELECT FOR UPDATE on rows that don't exist

From: Joe <symphony(dot)red+pg(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: pgsql-novice <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: SELECT FOR UPDATE on rows that don't exist
Date: 2022-03-24 05:31:47
Message-ID: CAD9Bb3s9acbwQ5FuMZ8nKEv8Ys__OBXpdk_5i9WMXpEsdcyjfg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-novice

David,
Thank you very much. I missed the advisory locks feature. That feels like
the perfect thing; the BIGINT keys are a bit clunky but that's minor.

Your description of the sequence of events for the two SELECT FOR UPDATE
transactions makes sense. To apply it to my pseudo code, the second
transaction would unblock when the row was deleted in the first rather than
when the first transaction commits or rolls back.

One question though:

You've informed the system you are going to be updating rows on the table
> but as yet have not given it specific rows to protect.
>
>>
>> Without #*1, a simple experiment shows that two processes can be in the
>> critical section at the same time. Add #*1 seems to achieve the desired
>> behavior, but is it really?
>>
>
> The index will not allow duplicates to be inserted and the first one to
> try forces all other potential insertions to wait until the first one
> commits;
>

Is this true in all situations? That is, will an index insertion in one
transaction block the index insertion in another transaction? Presumably
only only if the index entries match, but how does this work in practice?
Does the second transaction block when it sees the conflict? Or does it
proceed on its own version of the index proceeding until it tries to commit?

Thanks again,
Joe

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Fredrik Gustavsson 2022-03-30 11:00:55 ALTER DEFAULT PRIVILEGES
Previous Message David G. Johnston 2022-03-23 22:18:56 Re: SELECT FOR UPDATE on rows that don't exist