pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: George Woodring <george.woodring@iglass.net>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: How to make transaction delete see data from a just completed concurrent transaction?
Date: Mon, 05 Aug 2019 15:20:12 -0400
Message-ID: <32709.1565032812@sss.pgh.pa.us> (raw)
In-Reply-To: <CACi+J=RQce2i2UCm2btezTb2V-gssFoJ48eML=niwvusUPmsOg@mail.gmail.com>
References: <CACi+J=RQce2i2UCm2btezTb2V-gssFoJ48eML=niwvusUPmsOg@mail.gmail.com>

George Woodring <george.woodring@iglass.net> writes:
> We have been using the model of updating certain data in a table of
> begin;
> delete from foo where bar='myfoo';
> insert into foo all of the correct data for myfoo;
> commit;

> Our thinking was that if you had two running at close to the same time, the
> first transaction would finish and then the second one would run making the
> table consistent.  However this is not the case.  The second transaction is
> not deleting anything and we are getting double the inserted data.  Our
> guess is that the second transaction is trying to delete the data from the
> start of the transaction, not from when the lock on the table is released,
> and that data is already gone from the first transaction.

I think a closer statement is that the "delete" doesn't see any
uncommitted rows that might be in process of insertion by another
transaction.

I think that you could handle this by using serializable transactions,
which will fail if any concurrent transaction modifies data they
depend on.  You'd then need to have a retry loop for serialization
failures.

Another approach is to try to design the table such that a uniqueness or
exclusion constraint disallows having multiple sets of data for the same
key.  You'd still get failures and have to be willing to retry, but the
cause of the failure would be much more narrowly defined than it is
with a serialization failure.

			regards, tom lane





view thread (2+ messages)

Message-ID: <32709.1565032812@sss.pgh.pa.us>
Permalink:  ../32709.1565032812@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/32709.1565032812@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, george.woodring@iglass.net
  Subject: Re: How to make transaction delete see data from a just completed concurrent transaction?
  In-Reply-To: <32709.1565032812@sss.pgh.pa.us>
  References: <CACi+J=RQce2i2UCm2btezTb2V-gssFoJ48eML=niwvusUPmsOg@mail.gmail.com> <32709.1565032812@sss.pgh.pa.us>

* 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