From Julius.Stroffek@sun.com Fri Sep 7 14:56:11 2007 Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id C68579F9C44 for ; Fri, 7 Sep 2007 11:56:11 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 25443-04 for ; Fri, 7 Sep 2007 11:51:23 -0300 (ADT) X-Greylist: delayed 00:16:48.832406 by SQLgrey-1.7.5 Received: from gmp-eb-mail-2.sun.com (gmp-eb-mail-2.eu.sun.com [192.18.6.24]) by postgresql.org (Postfix) with ESMTP id 223F99F9BAA for ; Fri, 7 Sep 2007 11:47:37 -0300 (ADT) Received: from fe-emea-09.sun.com (gmp-eb-lb-2-fe3.eu.sun.com [192.18.6.12]) by gmp-eb-mail-2.sun.com (8.13.7+Sun/8.12.9) with ESMTP id l87EUgnT027556 for ; Fri, 7 Sep 2007 14:30:46 GMT Received: from conversion-daemon.fe-emea-09.sun.com by fe-emea-09.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007)) id <0JO000K0145PAZ00@fe-emea-09.sun.com> (original mail from Julius.Stroffek@Sun.COM) for pgsql-bugs@postgresql.org; Fri, 07 Sep 2007 15:30:42 +0100 (BST) Received: from [129.157.18.88] by fe-emea-09.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007)) with ESMTPSA id <0JO0008N35N4MQD0@fe-emea-09.sun.com> for pgsql-bugs@postgresql.org; Fri, 07 Sep 2007 15:30:40 +0100 (BST) Date: Fri, 07 Sep 2007 16:29:44 +0200 From: Julius Stroffek Subject: 'on insert do instead' rule with a where clause responds 'INSERT 0 0' To: pgsql-bugs@postgresql.org Message-id: <46E16058.4070602@Sun.COM> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_x2f0PT5Htt/PTNpUZqOanA)" User-Agent: Thunderbird 1.5.0.8 (X11/20061204) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/47 X-Sequence-Number: 18274 This is a multi-part message in MIME format. --Boundary_(ID_x2f0PT5Htt/PTNpUZqOanA) Content-type: multipart/alternative; boundary="Boundary_(ID_MM0Qfg2qW15/K5Vqo4cyfA)" --Boundary_(ID_MM0Qfg2qW15/K5Vqo4cyfA) Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7BIT Hi, Attached is the example script 'repro.sql' which creates two relations tab1 and tab2. It also creates a rule on tab1 which simply does insert into tab2. The insert statement into tab1 is executed afterwards. It responds with 'INSERT 0 1'. However if I would create the same rule with the where clause the response to the same insert statement is 'INSERT 0 0'. The output of the script executed through psql is in 'repro.out'. Is this a bug? Is there any reason why the second insert should respond 'INSERT 0 0' instead of 'INSERT 0 1'? Thanks. Cheers Julo --Boundary_(ID_MM0Qfg2qW15/K5Vqo4cyfA) Content-type: text/html; charset=ISO-8859-1 Content-transfer-encoding: 7BIT Hi,

Attached is the example script 'repro.sql' which creates two relations tab1 and tab2. It also creates a rule on tab1 which simply does insert into tab2. The insert statement into tab1 is executed afterwards. It responds with 'INSERT 0 1'. However if I would create the same rule with the where clause the response to the same insert statement is 'INSERT 0 0'. The output of the script executed through psql is in 'repro.out'.

Is this a bug? Is there any reason why the second insert should respond 'INSERT 0 0' instead of 'INSERT 0 1'? Thanks.

Cheers

Julo


--Boundary_(ID_MM0Qfg2qW15/K5Vqo4cyfA)-- --Boundary_(ID_x2f0PT5Htt/PTNpUZqOanA) Content-type: text/plain; name=repro.out Content-transfer-encoding: 7BIT Content-disposition: inline; filename=repro.out CREATE TABLE CREATE TABLE CREATE RULE INSERT 0 1 id ----- 100 (1 row) DROP RULE CREATE RULE DELETE 1 id ---- (0 rows) INSERT 0 0 id ----- 100 (1 row) DROP RULE DROP TABLE DROP TABLE --Boundary_(ID_x2f0PT5Htt/PTNpUZqOanA) Content-type: text/x-sql; name=repro.sql Content-transfer-encoding: 7BIT Content-disposition: inline; filename=repro.sql create table tab1 (id int); create table tab2 (id int); create rule tab1_insert as on insert to tab1 do instead insert into tab2 values (new.id); insert into tab1 values (100); select * from tab2; drop rule tab1_insert on tab1; create rule tab1_insert as on insert to tab1 where 1<2 do instead insert into tab2 values (new.id); delete from tab2; select * from tab2; insert into tab1 values (100); select * from tab2; drop rule tab1_insert on tab1; drop table tab2; drop table tab1; --Boundary_(ID_x2f0PT5Htt/PTNpUZqOanA)-- From tgl@sss.pgh.pa.us Fri Sep 7 15:36:30 2007 Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 11C4F9F977C for ; Fri, 7 Sep 2007 12:36:30 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 24530-03-3 for ; Fri, 7 Sep 2007 12:36:21 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 91A2F9F9C8C for ; Fri, 7 Sep 2007 12:36:04 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l87Fa3iv022419; Fri, 7 Sep 2007 11:36:03 -0400 (EDT) To: Julius Stroffek cc: pgsql-bugs@postgresql.org Subject: Re: 'on insert do instead' rule with a where clause responds 'INSERT 0 0' In-reply-to: <46E16058.4070602@Sun.COM> References: <46E16058.4070602@Sun.COM> Comments: In-reply-to Julius Stroffek message dated "Fri, 07 Sep 2007 16:29:44 +0200" Date: Fri, 07 Sep 2007 11:36:03 -0400 Message-ID: <22418.1189179363@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200709/48 X-Sequence-Number: 18275 Julius Stroffek writes: > Attached is the example script 'repro.sql' which creates two relations > tab1 and tab2. It also creates a rule on tab1 which simply does insert > into tab2. The insert statement into tab1 is executed afterwards. It > responds with 'INSERT 0 1'. However if I would create the same rule with > the where clause the response to the same insert statement is 'INSERT 0 > 0'. The output of the script executed through psql is in 'repro.out'. > Is this a bug? No. See http://www.postgresql.org/docs/8.2/static/rules-status.html and note that you don't have an unconditional INSTEAD rule. regards, tom lane From Julius.Stroffek@Sun.COM Thu Oct 18 13:20:33 2007 Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 38D029FBE9B; Thu, 18 Oct 2007 10:20:33 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 35298-01; Thu, 18 Oct 2007 10:20:17 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from gmp-eb-mail-1.sun.com (gmp-eb-mail-1.sun.com [192.18.6.21]) by postgresql.org (Postfix) with ESMTP id 482489FBCDC; Thu, 18 Oct 2007 10:20:25 -0300 (ADT) Received: from fe-emea-09.sun.com (gmp-eb-lb-2-fe2.eu.sun.com [192.18.6.11]) by gmp-eb-mail-1.sun.com (8.13.7+Sun/8.12.9) with ESMTP id l9IDKA9m028834; Thu, 18 Oct 2007 13:20:20 GMT Received: from conversion-daemon.fe-emea-09.sun.com by fe-emea-09.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007)) id <0JQ300H01YDM0600@fe-emea-09.sun.com> (original mail from Julius.Stroffek@Sun.COM); Thu, 18 Oct 2007 14:20:10 +0100 (BST) Received: from [129.157.71.74] by fe-emea-09.sun.com (Sun Java System Messaging Server 6.2-8.04 (built Feb 28 2007)) with ESMTPSA id <0JQ300HGDZPJXR60@fe-emea-09.sun.com>; Thu, 18 Oct 2007 14:20:08 +0100 (BST) Date: Thu, 18 Oct 2007 15:20:05 +0200 From: Julius Stroffek Subject: Re: 'on insert do instead' rule with a where clause responds 'INSERT 0 0' In-reply-to: <22418.1189179363@sss.pgh.pa.us> To: Tom Lane , pgsql-jdbc@postgresql.org Cc: pgsql-bugs@postgresql.org Message-id: <47175D85.4080801@sun.com> MIME-version: 1.0 Content-type: multipart/mixed; boundary="Boundary_(ID_0L2f/r1S0h6i7mlWYppvCQ)" References: <46E16058.4070602@Sun.COM> <22418.1189179363@sss.pgh.pa.us> User-Agent: Thunderbird 2.0.0.4 (X11/20070622) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/136 X-Sequence-Number: 18534 This is a multi-part message in MIME format. --Boundary_(ID_0L2f/r1S0h6i7mlWYppvCQ) Content-type: multipart/alternative; boundary="Boundary_(ID_1p3dftaHTfQLoo5nN6+WAQ)" --Boundary_(ID_1p3dftaHTfQLoo5nN6+WAQ) Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7BIT Hi, please see my comments inline. Tom Lane wrote: > Julius Stroffek writes: > >> Attached is the example script 'repro.sql' which creates two relations >> tab1 and tab2. It also creates a rule on tab1 which simply does insert >> into tab2. The insert statement into tab1 is executed afterwards. It >> responds with 'INSERT 0 1'. However if I would create the same rule with >> the where clause the response to the same insert statement is 'INSERT 0 >> 0'. The output of the script executed through psql is in 'repro.out'. >> >> Is this a bug? >> > > No. See > http://www.postgresql.org/docs/8.2/static/rules-status.html > and note that you don't have an unconditional INSTEAD rule. > I explored this and agree that the current PostgreSQL behavior as described in the above link is correct. However, this behavior is a serious issue when using Java Persistence through Hibernate (and probably using other providers as well). I have created a simple application running on glassfish just inserting records to the tables using Hibernate and PostgreSQL as a persistence provider. If I would use partitioning of the tables all the insert transactions would be marked for rollback and would be rolled back. After playing a bit with a very ugly code of glassfish trying to change it to report the root cause why the transactions are marked for roll back I discovered that Hibernate uses Statement.executeBatch method to execute the sql statements which then is supposed to return the number of rows affected by the passed statements. Hibernate then compares the value returned by this function with the number of records it passes to the batch. The number of affected rows is determined in PostgreSQL JDBC driver in QueryExecutorImpl.interpretCommandStatus method by parsing the command status string returned. The JDBC javadoc at http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html#executeBatch() describes the behavior and allows to return Statement.SUCCESS_NO_INFO in a case where the number of rows is unknown. More severe issue is that JDBC spec requires a set of methods Statement.executeUpdate which are forced to return the number of rows affected and can not return Statement.SUCCESS_NO_INFO. Any thoughts how to deal with this issue? Was there a discussion on this already in JDBC driver team? There is only one option that comes to my mind - always return Statment.SUCCESS_NO_INFO in executeBatch (or possibly only depending on some java property). I can not see any simple solution for Statement.executeUpdate since the number of rows affected may differ depending on the rules and might be also difficult to calculate. Thanks Cheers Julo --Boundary_(ID_1p3dftaHTfQLoo5nN6+WAQ) Content-type: text/html; charset=ISO-8859-1 Content-transfer-encoding: 7BIT Hi,

please see my comments inline.

Tom Lane wrote:
Julius Stroffek <Julius.Stroffek@sun.com> writes:
  
Attached is the example script 'repro.sql' which creates two relations 
tab1 and tab2. It also creates a rule on tab1 which simply does insert 
into tab2. The insert statement into tab1 is executed afterwards. It 
responds with 'INSERT 0 1'. However if I would create the same rule with 
the where clause the response to the same insert statement is 'INSERT 0 
0'. The output of the script executed through psql is in 'repro.out'.
    
Is this a bug?
    

No.  See
http://www.postgresql.org/docs/8.2/static/rules-status.html
and note that you don't have an unconditional INSTEAD rule.
  
I explored this and agree that the current PostgreSQL behavior as described in the above link is correct.

However, this behavior is a serious issue when using Java Persistence through Hibernate (and probably using other providers as well). I have created a simple application running on glassfish just inserting records to the tables using Hibernate and PostgreSQL as a persistence provider.

If I would use partitioning of the tables all the insert transactions would be marked for rollback and would be rolled back. After playing a bit with a very ugly code of glassfish trying to change it to report the root cause why the transactions are marked for roll back I discovered that Hibernate uses Statement.executeBatch method to execute the sql statements which then is supposed to return the number of rows affected by the passed statements. Hibernate then compares the value returned by this function with the number of records it passes to the batch. The number of affected rows is determined in PostgreSQL JDBC driver in QueryExecutorImpl.interpretCommandStatus method by parsing the command status string returned.

The JDBC javadoc at http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html#executeBatch() describes the behavior and allows to return Statement.SUCCESS_NO_INFO in a case where the number of rows is unknown. More severe issue is that JDBC spec requires a set of methods Statement.executeUpdate which are forced to return the number of rows affected and can not return Statement.SUCCESS_NO_INFO.

Any thoughts how to deal with this issue? Was there a discussion on this already in JDBC driver team?

There is only one option that comes to my mind - always return Statment.SUCCESS_NO_INFO in executeBatch (or possibly only depending on some java property). I can not see any simple solution for Statement.executeUpdate since the number of rows affected may differ depending on the rules and might be also difficult to calculate.

Thanks

Cheers

Julo


--Boundary_(ID_1p3dftaHTfQLoo5nN6+WAQ)-- --Boundary_(ID_0L2f/r1S0h6i7mlWYppvCQ) Content-type: text/x-sql; name=repro.sql Content-transfer-encoding: 7BIT Content-disposition: inline; filename=repro.sql create table tab1 (id int); create table tab2 (id int); create rule tab1_insert as on insert to tab1 do instead insert into tab2 values (new.id); insert into tab1 values (100); select * from tab2; drop rule tab1_insert on tab1; create rule tab1_insert as on insert to tab1 where 1<2 do instead insert into tab2 values (new.id); delete from tab2; select * from tab2; insert into tab1 values (100); select * from tab2; drop rule tab1_insert on tab1; drop table tab2; drop table tab1; --Boundary_(ID_0L2f/r1S0h6i7mlWYppvCQ) Content-type: text/plain; name=repro.out Content-transfer-encoding: 7BIT Content-disposition: inline; filename=repro.out CREATE TABLE CREATE TABLE CREATE RULE INSERT 0 1 id ----- 100 (1 row) DROP RULE CREATE RULE DELETE 1 id ---- (0 rows) INSERT 0 0 id ----- 100 (1 row) DROP RULE DROP TABLE DROP TABLE --Boundary_(ID_0L2f/r1S0h6i7mlWYppvCQ)-- From oliver@opencloud.com Thu Oct 18 21:00:14 2007 Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 3CA9D9FC38F; Thu, 18 Oct 2007 18:00:14 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 87404-02; Thu, 18 Oct 2007 17:59:59 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from tremor.opencloud.co.nz (tremor.opencloud.co.nz [131.203.101.230]) by postgresql.org (Postfix) with ESMTP id 926F19FC3CE; Thu, 18 Oct 2007 18:00:02 -0300 (ADT) Received: from 222-154-138-58.jetstream.xtra.co.nz ([222.154.138.58] helo=[10.1.1.3]) by tremor.opencloud.co.nz with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1IicTC-0001no-Sg; Fri, 19 Oct 2007 09:59:55 +1300 Message-ID: <4717C94D.7030605@opencloud.com> Date: Fri, 19 Oct 2007 09:59:57 +1300 From: Oliver Jowett User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 To: Julius Stroffek CC: Tom Lane , pgsql-jdbc@postgresql.org, pgsql-bugs@postgresql.org Subject: Re: [JDBC] Re: 'on insert do instead' rule with a where clause responds 'INSERT 0 0' References: <46E16058.4070602@Sun.COM> <22418.1189179363@sss.pgh.pa.us> <47175D85.4080801@sun.com> In-Reply-To: <47175D85.4080801@sun.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/140 X-Sequence-Number: 18538 Julius Stroffek wrote: > There is only one option that comes to my mind - always return > Statment.SUCCESS_NO_INFO in executeBatch (or possibly only depending on > some java property). I can not see any simple solution for > Statement.executeUpdate since the number of rows affected may differ > depending on the rules and might be also difficult to calculate. The server is reporting to the driver that zero rows were affected (not "unknown", *zero*) so I don't see any reason why the driver should not report that as the number of rows affected. Returning SUCCESS_NO_INFO reduces the usefulness of the driver in the other 98% of cases where there are no INSTEAD rules. The protocol docs say: > CommandComplete (B) [...] > For an INSERT command, the tag is INSERT oid rows, where rows is the number of rows inserted. oid is the object ID of the inserted row if rows is 1 and the target table has OIDs; otherwise oid is 0. So if the server is not returning "the number of rows inserted" then either the server has a bug or the protocol docs are wrong. -O