Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 688FE1337B39 for ; Wed, 24 Nov 2010 16:16:50 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 11634-10 for ; Wed, 24 Nov 2010 20:16:43 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ey0-f174.google.com (mail-ey0-f174.google.com [209.85.215.174]) by mail.postgresql.org (Postfix) with ESMTP id 791E71336971 for ; Wed, 24 Nov 2010 16:16:42 -0400 (AST) Received: by eye27 with SMTP id 27so75400eye.19 for ; Wed, 24 Nov 2010 12:16:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:content-type :content-transfer-encoding; bh=zyyP8zRcG18w7ktkVCl5bJOTKgdi8ylZeKupvs/ngYk=; b=bLqjfXv0rf0HK0MTZlHypUV7ucPIdRvHrp6Ml5d8TkSgcmhUz2FkgDGfDISVEvqzg0 ctzsktLIpDZswf3tsm6OFiOHgG/QpahO5IXuZOTTYVMuWm2xV3MDXQzEdXxUAOnIV8LB H20DL5ebcroC/1hmEeU435IcXlFd63wPRCLjk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; b=fcVaPPM3aPSnQ6vZDSGdepBnyiD/JbdkduJGF0AdL9oGpS/PCfUBldZKxFm8KsZpUK HJHcmKH7WuGH3CQJQDozIiJndT87/pkO60v1wZvBM8GbvtyfmApffY6ZW4EyOA/8hPrv D8yRttnigcL2Q7tiQ7KN5R7eoHrrvEZ6w1NFg= Received: by 10.213.27.208 with SMTP id j16mr1523025ebc.24.1290629801257; Wed, 24 Nov 2010 12:16:41 -0800 (PST) Received: from [178.49.3.188] (l49-3-188.cn.ru [178.49.3.188]) by mx.google.com with ESMTPS id q58sm7296752eeh.9.2010.11.24.12.16.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 24 Nov 2010 12:16:39 -0800 (PST) Message-ID: <4CED72BB.3070905@gmail.com> Date: Thu, 25 Nov 2010 02:16:59 +0600 From: "Mikhail V. Puzanov" User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.15) Gecko/20101030 Icedove/3.0.10 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: atomic multi-threaded upsert Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201011/52 X-Sequence-Number: 34790 Hi, All I'm trying to make a kind of upsert for the following table: CREATE TABLE sequences ( section VARCHAR( 50 ) NOT NULL, name VARCHAR( 50 ) NOT NULL, counter BIGINT NOT NULL ); CREATE UNIQUE INDEX IDX_SEQUENCES_SN ON sequences(section, name); Next, I execute the following two queries for that table: -- increment and get the counter if exists UPDATE sequences SET counter = counter + 1 WHERE section = 'testSection' AND name = 'testKey' RETURNING counter; -- insert new counter if not exists INSERT INTO sequences ( section, name, counter ) SELECT 'testSection', 'testKey', 0 WHERE NOT EXISTS ( SELECT * FROM sequences WHERE section = 'testSection' AND name = 'testKey' ); It works OK when executed in single thread. When such pairs of queries are executed in parallel (3-10 threads, 1 JDBC connection for each thread, with autoCommit set), occasionally I get the following error: ERROR: duplicate key value violates unique constraint "idx_sequences_sn" 1) That looks weird actually, should the INSERT ... NOT EXISTS (...) be executed in atomic fashion? Also, AFAIK, UNIQUE INDEX locks rows before insert (or not?). Can it be related to JDBC? Or it's the result of MVCC conflict resolution? 2) Are there some patterns for such kind of task? Thanks.