pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David G Johnston <david.g.johnston@gmail.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #10315: Transactions seem to be releasing locks early?
Date: Tue, 13 May 2014 14:36:55 -0700 (PDT)
Message-ID: <1400017015222-5803816.post@n5.nabble.com> (raw)
In-Reply-To: <20140513205137.11846.86900@wrigleys.postgresql.org>
References: <20140513205137.11846.86900@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

tim.channell wrote
> The following bug has been logged on the website:
> 
> Bug reference:      10315
> Logged by:          Tim Channell
> Email address:      

> tim.channell@

> PostgreSQL version: 9.3.4
> Operating system:   Archlinux
> Description:        
> 
> It seems that sometimes transactions (tested in READ COMMITTED mode, no
> manual locks) are releasing locks prematurely. Or something else wonky is
> happening. Here's my test
> 
> 1. Create a table. I just did
> 
>     CREATE TABLE test (id int);
>     INSERT INTO test (id) values(1),(2);
> 
> 2. Open two psql terminals. Issue BEGIN TRANSACTION in both.
> 
> 3. In the first psql, issue 
> 
>     DELETE FROM test WHERE id = 2;
> 
> that should show "DELETE 1" as the result.
> 
> 4. Repeat #3 in the second psql terminal. It should hang (waiting for lock
> to release). This is expected.
> 
> 5. Now, in the first psql, issue 
> 
>     INSERT INTO test (id) VALUES(2);
> 
> 6. In the first psql, issue COMMIT;
> 
> 7. Back in the second transaction, our DELETE has executed because the
> lock
> was released. It would be expected to show "DELETE 1", because the first
> transaction re-inserted the deleted record. But, it shows DELETE 0.
> 
> This all leads me to believe that the DELETE exclusive lock lifts BEFORE
> the
> insert statement in the first transaction actually succeeds. 

From:

http://www.postgresql.org/docs/9.3/interactive/transaction-iso.html#XACT-READ-COMMITTED

"When a transaction uses this isolation level, a SELECT query (without a FOR
UPDATE/SHARE clause) sees only data committed before the query began; it
never sees either uncommitted data or changes committed during query
execution by concurrent transactions."

As soon as you execute "DELETE" in psql-2 only physical records that exist
at that moment are visible to that statement.  Due to the DELETE in psql-1
that physical record with ID of 2 that existed before no longer exists.  The
locking makes it so psql-2 can see the effects of the delete as soon as
psql-1 commits.  However, the record you inserted in step 5 comes into
existence after the psql-2 DELETE and so cannot be seen by it - this is a
consequence of "...changes committed during query execution by concurrent
transaction."

Maybe someone else can clarify and confirm but basically even though you
had, and have again, a record record with the same ID they exist in
different times and the concurrent psql-2 can only see one of them - in this
case the one that was subsequently deleted in psql-1.

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/BUG-10315-Transactions-seem-to-be-releasing-locks-early-tp58...
Sent from the PostgreSQL - bugs mailing list archive at Nabble.com.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (7+ messages)  latest in thread

Message-ID: <1400017015222-5803816.post@n5.nabble.com>
Permalink:  ../1400017015222-5803816.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1400017015222-5803816.post@n5.nabble.com

 · 

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-bugs@postgresql.org
  Cc: david.g.johnston@gmail.com
  Subject: Re: BUG #10315: Transactions seem to be releasing locks early?
  In-Reply-To: <1400017015222-5803816.post@n5.nabble.com>
  References: <20140513205137.11846.86900@wrigleys.postgresql.org> <1400017015222-5803816.post@n5.nabble.com>

* 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