Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y4WkQ-0001LW-I1 for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Dec 2014 15:24:14 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y4WkO-0008WO-P4 for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Dec 2014 15:24:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y4WkL-0008W9-R0 for pgsql-hackers@postgresql.org; Fri, 26 Dec 2014 15:24:10 +0000 Received: from nm3-vm2.bullet.mail.ne1.yahoo.com ([98.138.91.19]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Y4WkG-0002JG-To for pgsql-hackers@postgresql.org; Fri, 26 Dec 2014 15:24:07 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ymail.com; s=s2048; t=1419607441; bh=nWWsEj9SMAYmZSd230gH3LXJP2mFEpIC04ESkj6skF0=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=eu6pKd0wvuPlOjn7mkNhBWNmjbpl3qV5lAyfQo0GitrCQ/bAw4EHLEnEedz9qq1FlkLA4PKIUCoAqCBggBYmCNMqO6pQDTS3Qgs2LUamT5a+H2V754+SKI6/IJM+DXhWnFGQCakRm4FjjtHPKk7NZidUBrWl78xSS+HqZvJUTjpqp9SKop36N1vJHr7sV7JozZqAYdqGCXAyOPl68GFmT++6UFzSwx/+hZnOef/cI2VBz+KEFeCaJv7Qf/K48kRWuFmG+E6T6i0SHXhSlzbds4yWAcaoLkWnApQumAuQNQkMOpNx0J4f85OU02dsBJ6HFhXHhZk5/P0nh4bWDw2Ndw== DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=ymail.com; b=e/dGogq2Zqszn8RZ/o4aGirihml9nW+NXInxsSpKHT8xKAUKAnluwTZu4Hmn/rmEJplGqz2ASjR+W1n2kpOh/Sv9giO9uB8DuGTWn06sNn/oGs2Uhlzmc4LeQFwZjBkA/gOyXah7EXYgrVcz6f09hXft5o8rJaP45ej5WrrRx/EDdnSjFPhE+PwrZUn2WThBEw4AViDXF7sKRVBn4qWa3TBEQwlNmF85zSwV1uWQIf/GvfJHHS7OdfPig87nFtyWIgenXm9ah2Kqg4KHYVui4DEDwUyFIX9IK1gnhwudq6dnIzM0T22YYxXNxSgVEQRjA3rF2jDMwqIeaJ23/3bOSA==; Received: from [98.138.100.113] by nm3.bullet.mail.ne1.yahoo.com with NNFMP; 26 Dec 2014 15:24:01 -0000 Received: from [98.138.89.175] by tm104.bullet.mail.ne1.yahoo.com with NNFMP; 26 Dec 2014 15:24:01 -0000 Received: from [127.0.0.1] by omp1031.mail.ne1.yahoo.com with NNFMP; 26 Dec 2014 15:24:01 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 336964.16272.bm@omp1031.mail.ne1.yahoo.com X-YMail-OSG: Hkxj12IVM1kPKmmGDBHF2OzaZczs1dhrdcmmAis_wa36rzCkHKZANHG.rH4RtHs 0SqGoPUztxFwkIwoXki2r6oYZ70C9k72bba.CS5jmS2JkwpcAG8bi__g3CjVQZjcq5yEG0d4q3lU kgD8QhRahab3lvnK9VVKHvI9ATtuoAFvrPuYgIpdKoZ4UJzLWF6KF9cfnWgBaIcjRGJr2mJSJKOh sAy6zBjgIzc2ib27YtvR7yYfAt8OCt7a_Uvip8kp_Vq0uuW0MlKfEWjgfv7Ief4RC4.8WWIN2w.q H7d2EIGYqNCXUBr9j0bXy_43uAsQxEKFpNwdlo7rZhG52av7W_eQthjlRHKt1VkiHcUxxjrqfGkp 50tFtA5FmqGv6rROinq49zTvz71KtP2uLtXnG72pJ21YGOUDlsWjUc7S6yLc9hJ6H5O4C7WvoRaH BcFVwdbG0NxDYJYe1s43IUgb.LoZnsU0PKJ3lxgNOIyBtOL_zkObk3LkEHqP6R0P5q6W7v8Nw0vF Oeg9OqQHnwc3NE0e9ibs_9J0tIJr5R.r8X1eCq8Y8jFnRFmGGEqBexElcRtM- Received: by 98.138.101.174; Fri, 26 Dec 2014 15:24:00 +0000 Date: Fri, 26 Dec 2014 15:23:37 +0000 (UTC) From: Kevin Grittner Reply-To: Kevin Grittner To: "nikita.y.volkov@mail.ru" , "pgsql-hackers@postgresql.org" Message-ID: <969937206.892402.1419607417808.JavaMail.yahoo@jws10072.mail.ne1.yahoo.com> In-Reply-To: <20141224140153.11501.49752@wrigleys.postgresql.org> References: <20141224140153.11501.49752@wrigleys.postgresql.org> Subject: Re: BUG #12330: ACID is broken for unique constraints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Length: 2286 X-Pg-Spam-Score: -1.7 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org "nikita.y.volkov@mail.ru" wrote: > Executing concurrent transactions inserting the same value of a > unique key fails with the "duplicate key" error under code > "23505" instead of any of transaction conflict errors with a > "40***" code. This is true, and can certainly be inconvenient when using serializable transactions to simplify handling of race conditions, because you can't just test for a SQLSTATE of '40001' or '40P01' to indicate the need to retry the transaction. You have two reasonable ways to avoid duplicate keys if the values are synthetic and automatically generated. One is to use a SEQUENCE object to generate the values. The other (really only recommended if gaps in the sequence are a problem) is to have the serializable transaction update a row to "claim" the number. Otherwise you need to consider errors related to duplicates as possibly being caused by a concurrent transaction. You may want to do one transaction retry in such cases, and fail if an identical error is seen. Keep in mind that these errors do not allow serialization anomalies to appear in the committed data, so are arguably not violations of ACID principles -- more of a wart on the otherwise clean technique of using serializable transactions to simplify application programming under concurrency. Thinking about it just now I think we might be able to generate a write conflict instead of a duplicate key error for this case by checking the visibility information for the duplicate row. It might not even have a significant performance impact, since we need to check visibility information to generate the duplicate key error. That would still leave similar issues (where similar arguments can be made) relating to foreign keys; but those can largely be addressed already by declaring the constraints to be DEFERRED -- and anyway, that would be a separate fix. I'm moving this discussion to the -hackers list so that I can ask other developers: Are there any objections to generating a write conflict instead of a duplicate key error if the duplicate key was added by a concurrent transaction? Only for transactions at isolation level REPEATABLE READ or higher? -- Kevin Grittner EDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers