Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZqNNk-0006lk-A1 for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Oct 2015 15:38:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZqNNj-0006Fl-N1 for pgsql-bugs@arkaria.postgresql.org; Sun, 25 Oct 2015 15:38:51 +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 1ZqNNj-0006FU-8L for pgsql-bugs@postgresql.org; Sun, 25 Oct 2015 15:38:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZqNNg-0003kF-JF for pgsql-bugs@postgresql.org; Sun, 25 Oct 2015 15:38:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t9PFcNw1031957; Sun, 25 Oct 2015 11:38:24 -0400 From: Tom Lane To: chjischj@163.com cc: pgsql-bugs@postgresql.org Subject: Re: BUG #13723: "duplicate key" error occurs when update delete and insert the same row concurrently In-reply-to: <20151025110136.3017.39398@wrigleys.postgresql.org> References: <20151025110136.3017.39398@wrigleys.postgresql.org> Comments: In-reply-to chjischj@163.com message dated "Sun, 25 Oct 2015 11:01:36 -0000" Date: Sun, 25 Oct 2015 11:38:23 -0400 Message-ID: <31956.1445787503@sss.pgh.pa.us> 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 chjischj@163.com writes: > 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. I see no bug here; you're just making a mistaken assumption about how cross-transaction serialization works. At some point you're going to end up with a timing in which both clients are trying to do the DELETE. Only one does it; the other waits for that row change to commit, sees it's done, and concludes that there's nothing for it to do. (In particular, it will not see the row that was inserted later in the other transaction, because that's too new.) Now the second one's INSERT fails because there's already a row with id=1. If you want this sort of coding to execute stably, you could consider taking out a table-level lock, or some other way of preventing clients from concurrently deleting+inserting the same key. Or, just don't do that in the first place. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs