Re: ERROR : 'tuple concurrently updated'

From: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
To: Stéphan BEUZE <stephan(dot)beuze(at)douane(dot)finances(dot)gouv(dot)fr>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: ERROR : 'tuple concurrently updated'
Date: 2013-10-18 05:01:03
Message-ID: CAA4eK1+vW1cVgTVq-4KMEecvAshErnpKCDExO=BVoSQgZJxUSA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Oct 16, 2013 at 5:55 PM, Stéphan BEUZE
<stephan(dot)beuze(at)douane(dot)finances(dot)gouv(dot)fr> wrote:
> The following query is performed concurrently by two threads logged in with
> two different users:
>
> WITH raw_stat AS (
> SELECT
> host(client_addr) as client_addr,
> pid ,
> usename
> FROM
> pg_stat_activity
> WHERE
> usename = current_user
> )
> INSERT INTO my_stat(id, client_addr, pid, usename)
> SELECT
> nextval('mystat_sequence'), t.client_addr, t.pid, t.usename
> FROM (
> SELECT
> client_addr, pid, usename
> FROM
> raw_stat s
> WHERE
> NOT EXISTS (
> SELECT
> NULL
> FROM
> my_stat u
> WHERE
> current_date = u.creation
> AND
> s.pid = u.pid
> AND
> s.client_addr = u.client_addr
> AND
> s.usename = u.usename
> )
> ) t;
>
> From time to time, I get the following error: "tuple concurrently updated"
>
> I can't figure out what throw this error and why this error is thrown. Can
> you shed a light ?

I have tried by using this query in a loop of 5000 and run the loop
in 2 different connections with different users, but could not get the
error.
What I understood from sql statement is that it will insert new
rows when there are new/different connections, so simply running this
sql statement
from 2 connections might not insert any new rows.
a. Are there any new connections happening, how this table is
getting populated?
b. How did you concluded that above sql statement leads to error,
because this error doesn't seem to occur in path of above sql
statement.
c. Are there any other sql statements in connection where you see this error?

Can you explain a bit more about your scenario, so that this error
can be reproduced easily.

> -------------------------------
> Here is the sql definition of the table mystat.
>
> **mystats.sql**
>
> CREATE TABLE mystat
> (
> id bigint NOT NULL,
> creation date NOT NULL DEFAULT current_date,
>
> client_addr text NOT NULL,
> pid integer NOT NULL,
> usename name NOT NULL,
> CONSTRAINT statistiques_connexions_pkey PRIMARY KEY (id)
> )
> WITH (
> OIDS=FALSE
> );

Some comments about SQL statements:
a. table name provided as part of schema (mystat) is different
from one used in sql statement(my_stat)
b. definition of sequence mystat_sequence is missing, although it
doesn't seem to be necessary, but if you can provide the definition
you are using
then it will be better.

With Regards,
Amit Kapila.
EnterpriseDB: http://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Dean Rasheed 2013-10-18 05:34:38 Re: psql tab completion for updatable foreign tables
Previous Message Jaime Casanova 2013-10-18 04:13:40 Re: Turning recovery.conf into GUCs