pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Oliver Seemann <oseemann@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Update with subselect sometimes returns wrong result
Date: Sat, 30 Nov 2013 22:40:18 +0100
Message-ID: <20131130214018.GJ31100@awork2.anarazel.de> (raw)
In-Reply-To: <25601.1385834264@sss.pgh.pa.us>
References: <CANCipfpfzoYnOz5jj=UZ70_R=CwDHv36dqWSpwsi27vpm1z5sA@mail.gmail.com>
	<25601.1385834264@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 2013-11-30 12:57:44 -0500, Tom Lane wrote:
> Oliver Seemann <oseemann@gmail.com> 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 <module>
    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

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

Attachments:

pgbug.py (text/plain, 2.3K) download #!/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()

view thread (24+ messages)  latest in thread

Message-ID: <20131130214018.GJ31100@awork2.anarazel.de>
Permalink:  ../20131130214018.GJ31100@awork2.anarazel.de/
Also on:    postgresql.org/message-id/20131130214018.GJ31100@awork2.anarazel.de

 · 

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: andres@2ndquadrant.com, tgl@sss.pgh.pa.us, oseemann@gmail.com
  Subject: Re: Update with subselect sometimes returns wrong result
  In-Reply-To: <20131130214018.GJ31100@awork2.anarazel.de>
  References: <CANCipfpfzoYnOz5jj=UZ70_R=CwDHv36dqWSpwsi27vpm1z5sA@mail.gmail.com> <25601.1385834264@sss.pgh.pa.us> <20131130214018.GJ31100@awork2.anarazel.de>

* 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