Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WkKNg-0000MK-Mg for pgsql-bugs@arkaria.postgresql.org; Tue, 13 May 2014 21:37:00 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WkKNg-0001BF-0n for pgsql-bugs@arkaria.postgresql.org; Tue, 13 May 2014 21:37:00 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WkKNf-0001B9-AX for pgsql-bugs@postgresql.org; Tue, 13 May 2014 21:36:59 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WkKNc-00078U-OM for pgsql-bugs@postgresql.org; Tue, 13 May 2014 21:36:58 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WkKNb-00057x-7O for pgsql-bugs@postgresql.org; Tue, 13 May 2014 14:36:55 -0700 Date: Tue, 13 May 2014 14:36:55 -0700 (PDT) From: David G Johnston To: pgsql-bugs@postgresql.org Message-ID: <1400017015222-5803816.post@n5.nabble.com> In-Reply-To: <20140513205137.11846.86900@wrigleys.postgresql.org> References: <20140513205137.11846.86900@wrigleys.postgresql.org> Subject: Re: BUG #10315: Transactions seem to be releasing locks early? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 2.7 (++) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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-tp5803812p5803816.html 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