Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 212281337B49 for ; Tue, 18 Jan 2011 13:28:25 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 68000-08 for ; Tue, 18 Jan 2011 17:28:17 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from web33207.mail.mud.yahoo.com (web33207.mail.mud.yahoo.com [209.191.69.155]) by mail.postgresql.org (Postfix) with SMTP id 478EC1337B69 for ; Tue, 18 Jan 2011 13:28:17 -0400 (AST) Received: (qmail 59930 invoked by uid 60001); 18 Jan 2011 17:28:15 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1295371695; bh=uESCJ/YdappeBLpnP+mNNCBRAeZrIzVAqAttQcbGGAg=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=fmboTdHwSxoIa9+6QKcKh5RyETW6qv8g3IaQDZMxap7mIzZlj5pIXiu7uAZ7sHFMlMi1Owm1yeb6qtyfYk7ltTB36juMsdqsDFlEWENL5KI0XsSWwHS8k8mE1pbUkchkYpKuH5NBYfNlpM2y8gMB74xShsO2ExirOb1xXfhFqy8= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=tGchuk2KxfE7rhqITkto437FWwvU69qYU3OJd2fCayoQ6qD9COpuAQarbPr7uKtlO3t3Ad9ZCvvT5aqnJ2bF0D+aZPuLhb0o6gs/DaulfptrO9hiYsfKRmDZOz4sdCzyhHCw5S69gc5I4sq0ji4MQHNjR/dS/eYEqNud9F01u4k=; Message-ID: <654041.58993.qm@web33207.mail.mud.yahoo.com> X-YMail-OSG: SjpntvwVM1nywf7wvmgLwWn6f1XZJzlKAzYLDCD3BVcHL1q sqKBJxjbQtdhEyGBenKvvOjz6mdj5_zvA9TMCda4IQWNwiRa1G1._M2lVBOG JpHlp6qii01bdkg5YaGfVizxLVdJNPI_0pty2g1gLojxIIo3mpb9TukINzUk es3nip9rk1TeEOL7j2YfAvg2TZQ13fx3rCugO06E3xwyFjj8RPtBxJPR.aOb Tp8bnTkBVgQka4k.3L8NcvjxiiNFu4xbGyvazcaUQYbPj6QjmxuLzODXkoim UnJFcyBeHEhaGCIMfW1.4lWoSUQ05J_lHf5vNme9UkZa5KstFxR5CEx1UlNe 7g.IlA4Jh Received: from [199.84.5.210] by web33207.mail.mud.yahoo.com via HTTP; Tue, 18 Jan 2011 09:28:15 PST X-Mailer: YahooMailClassic/11.4.20 YahooMailWebService/0.8.107.285259 Date: Tue, 18 Jan 2011 09:28:15 -0800 (PST) From: Quartz Subject: jdbc spec violation for autocommit=true & addbatch/executeBatch To: pgsql-jdbc@postgresql.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.898 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RFC_ABUSE_POST=0.001, T_RP_MATCHES_RCVD=-0.01, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201101/48 X-Sequence-Number: 21380 jdbc spec violation for autocommit=true & addbatch/executeBatch We are moving to postgresql. I have unexpected deadlocks on batches even though I don't use transaction (I use autocommit=true). The test program is simple. On a 20+ row table with primary key 'id', one thread does 20 updates added with statement.addBatch() from id 1 to 20. Meanwhile the other thread does updates from row id 20 down to 1. To facilitate the contention, I used a "pg_sleep(3)" call inside each update. That gives me time to start both program (i.e. both threads). update data set f1=if(exists(select * from pg_sleep(3)), 1, 0) where id=1 update data set f2=if(exists(select * from pg_sleep(3)), 1, 0) where id=2 and so on. (I just made 20 fields to see the f1, f2 .. in the sql string to distinguish at which statement a connection is at. Could have been the same field all the time.) (Under mysql 5+: update data set f1=sleep(3) where id=1 update data set f2=sleep(3) where id=2 and so on.) In a nutshell, each program should run for about 60 seconds (20x3 seconds). The results: Mysql myisam (table locking) never fails, but the 2 programs are interlaced and take nearly 120s as expected. Mysql innodb without transaction never fails, never deadlock. Mysql innodb with transaction (autocommit=false & commit()) does deadlock as expected. Postgres with transaction (autocommit=false & commit()) does deadlock as expected. Postgres without transaction (autocommit=true) deadlocks UNEXPECTEDLY. The jdbc javadoc says clearly: http://download.oracle.com/javase/6/docs/api/java/sql/Connection.html#setAutoCommit%28boolean%29 "If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions." This is simply not true for postgresql. For now I cannot tell if this is a bad driver setting up the connection, of if postgresql server simply cannot be wrapped by jdbc for this API. All I can tell is that this is a show stopper for everyone doing simple transaction-less batches, as there is no way to avoid deadlock risks completely, besides an expensive (cpu-wise and design-wise) sort of intended statements to produce a predictable row locking order. Yet, I ran many threads doing the id 1..20 updates concurrently, and guess what? The 2nd instance was blocked for the entire duration of the 1st instance, showing about 60 seconds of duration for the statement on row id=1. This is unacceptable concurrency.