Thread: Transaction isolation and constraints

Transaction isolation and constraints

From
cliff@krumvieda.com
Date:
Hi:

I'd like to know how PostgreSQL's transaction isolation mechanisms
interact with (e.g., UNIQUE) constraints.  Section 12.2 of the manual
mentions that UPDATE, DELETE, SELECT FOR UPDATE, and SELECT FOR SHARE
commands may block when a concurrent transaction updates a target row
(for both isolation levels, Read Committed and Serializable).  But
suppose a table has a UNIQUE constraint on a column, and two
concurrent transactions attempt to INSERT a row with the same value
for that column:

  o Will the "first" INSERT wait to see if the "second" aborts (as is
    done with UPDATE, DELETE, et al.)?

  o Or will it immediately abort?

  o Or will it continue until immediately before commit, then abort?

It's not clear when the constraint check is run (or what version of
the table it sees).

Note this point isn't specific to INSERTs, another example would be
two concurrent transactions that UPDATE completely different rows and
in so doing violate a UNIQUE constraint.

Thanks.

                --Cliff Krumvieda

Re: Transaction isolation and constraints

From
Tom Lane
Date:
cliff@krumvieda.com writes:
> suppose a table has a UNIQUE constraint on a column, and two
> concurrent transactions attempt to INSERT a row with the same value
> for that column:

Whichever one manages to get to the index page first will go through.
The second one will block waiting to see if the first one commits,
and will error out if so --- or proceed, if it aborts.

            regards, tom lane

Re: Transaction isolation and constraints

From
cliff@krumvieda.com
Date:
Hi, Tom:

>Whichever one manages to get to the index page first will go through.
>The second one will block waiting to see if the first one commits,
>and will error out if so --- or proceed, if it aborts.

I see, this makes sense.  What if the two transactions insert rows
that don't violate the constraint: will they be able to proceed in
parallel?  Or will one wait for the other, because they both need to
update the shared index?  I.e., does the mechanism work by waiting for
one index update to commit before permitting the next?

                   --Cliff

Re: Transaction isolation and constraints

From
Bruce Momjian
Date:
cliff@krumvieda.com wrote:
> Hi, Tom:
>
> >Whichever one manages to get to the index page first will go through.
> >The second one will block waiting to see if the first one commits,
> >and will error out if so --- or proceed, if it aborts.
>
> I see, this makes sense.  What if the two transactions insert rows
> that don't violate the constraint: will they be able to proceed in
> parallel?  Or will one wait for the other, because they both need to
> update the shared index?  I.e., does the mechanism work by waiting for
> one index update to commit before permitting the next?

They proceed in parallel.  Backends wait only on specific rows that
conflict, not the index page.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://postgres.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +