Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VmsH4-0000AN-KF for pgsql-bugs@arkaria.postgresql.org; Sat, 30 Nov 2013 21:40:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VmsH4-0006xJ-2B for pgsql-bugs@arkaria.postgresql.org; Sat, 30 Nov 2013 21:40:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VmsH2-0006vg-BS for pgsql-bugs@postgresql.org; Sat, 30 Nov 2013 21:40:24 +0000 Received: from moutng.kundenserver.de ([212.227.17.9]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VmsGz-000417-5W for pgsql-bugs@postgresql.org; Sat, 30 Nov 2013 21:40:23 +0000 Received: from anarazel.de (p54AF4684.dip0.t-ipconnect.de [84.175.70.132]) by mrelayeu.kundenserver.de (node=mreu3) with ESMTP (Nemesis) id 0LkEtr-1VBKOn3reV-00cNSW; Sat, 30 Nov 2013 22:40:19 +0100 Received: by anarazel.de (Postfix, from userid 1000) id 165EE101530; Sat, 30 Nov 2013 22:40:18 +0100 (CET) Date: Sat, 30 Nov 2013 22:40:18 +0100 From: Andres Freund To: Tom Lane Cc: Oliver Seemann , pgsql-bugs@postgresql.org Subject: Re: Update with subselect sometimes returns wrong result Message-ID: <20131130214018.GJ31100@awork2.anarazel.de> References: <25601.1385834264@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="VrqPEDrXMn8OVzN4" Content-Disposition: inline In-Reply-To: <25601.1385834264@sss.pgh.pa.us> X-Provags-ID: V02:K0:3b2f70b0WV0kL7DEGTM8DVNhlI/90TOyi7+Iz6Qg6Fg XcdMr9QIonMAhew+ah8sGusj3cSDlW/c0PSiDdSA19epRfhHur JRnOIfkIxFajh/T98aEviZfYW3P4mzYB4cipsAGnZC5tGpaPmh Gxf2f926yadD/UpqAQS6QkYb/GMZcmKiinmt2Bfi7fRoZPPhL4 GYDoX2KlOOiQgJq3MLV4sOah6VQ23GhbgII20UiqWHttTYW++h KeJuRkeZZPygEc/G14vbMjXCJvq36jeLNKZ4Oe4fi4kXqyXr99 ta+zTreDzSQlnK3pNmFtQC9YmgSmf9O+7DAvp7oKDWr9VdWyZV 9h3xkmAfAcduGdQgezDLXMVE3kuO9weW1GZmiAWxv X-Pg-Spam-Score: -1.9 (-) 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 --VrqPEDrXMn8OVzN4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On 2013-11-30 12:57:44 -0500, Tom Lane wrote: > Oliver Seemann writes: > > Given the following table: > > > CREATE TABLE t1 (id INTEGER); > > INSERT INTO t1 VALUES (0), (1); > > > Then the following UPDATE should return exactly one row: > > > UPDATE t1 SET id = t1.id > > FROM (SELECT id FROM t1 LIMIT 1 FOR UPDATE) AS subset > > WHERE t1.id = subset.id > > RETURNING t1.id > > > And it does so, most of of the time. But when run repeatedly in a loop like > > in the attached script, then it will occasionally return 2 rows with two > > different id values, something the LIMIT 1 should prevent. In my tests it > > took from anywhere between 0 to 10 minutes and on average 1 to 2 minutes to > > trigger the problem. > > I failed to reproduce the claimed misbehavior in git tip of any active > branch. I'd like to think this means we fixed the problem in the last > two months, but I don't see anything that looks like a promising candidate > in the commit logs. Perhaps there is some important contributing factor > you've not mentioned --- nondefault postgresql.conf settings, for > instance. Looks reproducable here as well, manually executing VACUUMs on the table greatly speeds things up. Fails within seconds when doing so. So, it looks like the limit returns more than one row, it's not updating the same row twice. Slightly hacked up (probably python 2 only) version of the test script attached. I'll get to trying to write the release stuff rather then playing with more interesting things ;) new row at: (0,4) updated row from (0,2) to (0,1) iter 400 deleted row at: (0,1) deleted row at: (0,5) new row at: (0,1) new row at: (0,5) updated row from (0,1) to (0,3) iter 401 deleted row at: (0,2) deleted row at: (0,3) new row at: (0,2) new row at: (0,3) updated row from (0,1) to (0,3) iter 402 deleted row at: (0,2) deleted row at: (0,3) new row at: (0,2) new row at: (0,3) updated row from (0,4) to (0,1) iter 403 deleted row at: (0,1) deleted row at: (0,5) new row at: (0,1) new row at: (0,5) updated row from (0,1) to (0,3) iter 404 deleted row at: (0,2) deleted row at: (0,3) new row at: (0,2) new row at: (0,3) updated row from (0,1) to (0,3) iter 405 deleted row at: (0,2) deleted row at: (0,3) new row at: (0,2) new row at: (0,3) updated row from (0,4) to (0,6) iter 406 ... deleted row at: (0,2) deleted row at: (0,3) new row at: (0,2) new row at: (0,3) updated row from (0,4) to (0,1) iter 447 updated row from (0,5) to (0,2) iter 447 Traceback (most recent call last): File "/tmp/pgbug.py", line 80, in test_bug() File "/tmp/pgbug.py", line 51, in test_bug update(cur, i) File "/tmp/pgbug.py", line 76, in update assert(len(rows) == 1) AssertionError There's clearly something wrong. (0,4) has been updated several times, but seems to still be visible. Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --VrqPEDrXMn8OVzN4 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="pgbug.py" #!/usr/bin/env python # -*- coding: utf-8 -*- # vim:set ts=4 sts=4 sw=4 expandtab: """ The UPDATE statement below should always return only 1 row, due to the LIMIT 1 clause in the sub-select and unique values of the joined id column. Most of the time it correctly returns only one row. But when run repeatedly, after on average about 2 minutes, it will return two rows with different id values. This should never happen. This problem has been repeatedly reproduced on the following platforms: PG 9.3.1 on Debian Wheezy, Opteron 2347, Python 2.7.3, psycopg2 2.5.1 PG 9.0.13 on Debian Wheezy, Opteron 2347, Python 2.7.3, psycopg2 2.5.1 PG 9.1.10 on Ubuntu 13.04, Core 2 E8400, Python 2.7.4, psycopg2 2.4.5 PG 9.0.14 on OS X 10.8.5, Core i5, Python 2.7.2, psycopg2 2.5.1 The Debian packages are from the PGDG APT repository. The Ubuntu package is the distribution default. The OS X package is from MacPorts. The Ubuntu and OS X machines were running the default PG config. The issue could not be reproduced when autovacuum is disabled! Run the script with a PG_BUG_CONNSTR environment variable containing a postgresql connection string to an empty database: $ PG_BUG_CONNSTR="dbname=pgbug" python pgbug.py Make sure the psycopg2 module is available. """ import os import psycopg2 def test_bug(): conn = psycopg2.connect(os.environ['PG_BUG_CONNSTR']) conn.autocommit = True cur = conn.cursor() cur.execute("DROP TABLE IF EXISTS t1") cur.execute("CREATE TABLE t1 (id INTEGER)") cur.execute("INSERT INTO t1 VALUES (0), (1) RETURNING ctid") for i in range(1, 1000000): update(cur, i) def update(cur, i): cur.execute("DELETE FROM t1 RETURNING ctid") rows = cur.fetchall() for row in rows: print "deleted row at: ", row[0] assert(len(rows) == 2) cur.execute("INSERT INTO t1 VALUES (0), (1) RETURNING ctid") row2 = cur.fetchall() for row in rows: print "new row at: ", row[0] cur.execute(""" UPDATE t1 SET id = t1.id FROM (SELECT id, ctid FROM t1 LIMIT 1 FOR UPDATE) AS subset WHERE t1.id = subset.id RETURNING t1.id, subset.ctid oldctid, t1.ctid newctid """) rows = cur.fetchall() for row in rows: print "updated row from %s to %s iter %u " % (row[1], row[2], i) assert(len(rows) == 1) if __name__ == '__main__': test_bug() --VrqPEDrXMn8OVzN4 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --VrqPEDrXMn8OVzN4--