Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZqJ4q-0004ZY-43 for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Oct 2015 11:03:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZqJ4p-00022N-I6 for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Oct 2015 11:03:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZqJ4C-0001Ln-CF for pgsql-bugs@postgresql.org; Sun, 25 Oct 2015 11:02:24 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1ZqJ3z-0006r2-3A for pgsql-bugs@postgresql.org; Sun, 25 Oct 2015 11:02:18 +0000 MIME-Version: 1.0 Subject: BUG #13723: "duplicate key" error occurs when update delete and insert the same row concurrently To: pgsql-bugs@postgresql.org From: chjischj@163.com Date: Sun, 25 Oct 2015 11:01:36 +0000 Message-ID: <20151025110136.3017.39398@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 13723 Logged by: ChenHuajun Email address: chjischj@163.com PostgreSQL version: 9.4.5 Operating system: CentOS 6 Description: When i run sysbench's complex test with PostgreSQL, the following error always occured. duplicate key value violates unique constraint "%s" It seems to be a bug which occurs when executing update,delete and insert(within one transaction) the same row concurrently. And i reproduced this issue via pgbench. Just as the following 1. prepare table & data create table tb1(id int primary key,c int); insert into tb1 values(1,1); 2. prepare test sql script [postgres@localhost ~]$ cat test.sql begin; update tb1 set c=2 where id=1 delete from tb1 where id=1; insert into tb1 values(1,2); commit; 3. run test [postgres@localhost ~]$ pgbench -n -f test.sql -c 2 -j 2 -t 2 client 1 aborted in state 3: ERROR: duplicate key value violates unique constraint "tb1_pkey" DETAIL: Key (id)=(1) already exists. transaction type: Custom query scaling factor: 1 query mode: simple number of clients: 2 number of threads: 2 number of transactions per client: 2 number of transactions actually processed: 2/4 latency average: 0.000 ms tps = 130.047467 (including connections establishing) tps = 225.060485 (excluding connections establishing) 4. show log [postgres@localhost ~]$ cat pg95data/pg_log/postgresql-2015-10-25_141648.log 2015-10-25 14:16:48.144 EDT 57177 0 LOG: database system was shut down at 2015-10-25 14:16:47 EDT 2015-10-25 14:16:48.146 EDT 57177 0 LOG: MultiXact member wraparound protections are now enabled 2015-10-25 14:16:48.149 EDT 57175 0 LOG: database system is ready to accept connections 2015-10-25 14:16:48.150 EDT 57181 0 LOG: autovacuum launcher started 2015-10-25 14:16:57.960 EDT 57184 0 LOG: connection received: host=[local] 2015-10-25 14:16:57.961 EDT 57184 0 LOG: connection authorized: user=postgres database=postgres 2015-10-25 14:16:57.971 EDT 57186 0 LOG: connection received: host=[local] 2015-10-25 14:16:57.971 EDT 57187 0 LOG: connection received: host=[local] 2015-10-25 14:16:57.972 EDT 57186 0 LOG: connection authorized: user=postgres database=postgres 2015-10-25 14:16:57.972 EDT 57187 0 LOG: connection authorized: user=postgres database=postgres 2015-10-25 14:16:57.975 EDT 57186 0 LOG: statement: begin; 2015-10-25 14:16:57.975 EDT 57186 0 LOG: statement: update tb1 set c=2 where id=1 2015-10-25 14:16:57.975 EDT 57187 0 LOG: statement: begin; 2015-10-25 14:16:57.976 EDT 57187 0 LOG: statement: update tb1 set c=2 where id=1 2015-10-25 14:16:57.978 EDT 57186 39682 LOG: statement: delete from tb1 where id=1; 2015-10-25 14:16:57.979 EDT 57186 39682 LOG: statement: insert into tb1 values(1,2); 2015-10-25 14:16:57.979 EDT 57186 39682 LOG: statement: commit; 2015-10-25 14:16:57.980 EDT 57186 0 LOG: statement: begin; 2015-10-25 14:16:57.981 EDT 57186 0 LOG: statement: update tb1 set c=2 where id=1 2015-10-25 14:16:57.981 EDT 57187 39683 LOG: statement: delete from tb1 where id=1; 2015-10-25 14:16:57.981 EDT 57186 39684 LOG: statement: delete from tb1 where id=1;//*ERROR!*, Both process 57186 and 57187 had excuted "update tb1 set c=2 where id=1" successfully at the same time. 2015-10-25 14:16:57.981 EDT 57186 39684 LOG: statement: insert into tb1 values(1,2); 2015-10-25 14:16:57.981 EDT 57186 39684 LOG: statement: commit; 2015-10-25 14:16:57.983 EDT 57187 39683 LOG: statement: insert into tb1 values(1,2); 2015-10-25 14:16:57.983 EDT 57187 39683 ERROR: duplicate key value violates unique constraint "tb1_pkey" 2015-10-25 14:16:57.983 EDT 57187 39683 DETAIL: Key (id)=(1) already exists. 2015-10-25 14:16:57.983 EDT 57187 39683 STATEMENT: insert into tb1 values(1,2); I have tested PostgreSQL 9.3.1,9.4.5 and 9.5beta1,all of them has this issue. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs