Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 9E49E1337B82 for ; Thu, 21 Apr 2011 11:34:42 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 02568-07 for ; Thu, 21 Apr 2011 14:34:35 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 34986133798E for ; Thu, 21 Apr 2011 11:34:35 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p3LEYYW7009538; Thu, 21 Apr 2011 10:34:34 -0400 (EDT) To: Humair Mohammed cc: pgsql-sql@postgresql.org Subject: Re: In-reply-to: References: Comments: In-reply-to Humair Mohammed message dated "Wed, 20 Apr 2011 09:45:07 -0500" Date: Thu, 21 Apr 2011 10:34:34 -0400 Message-ID: <9537.1303396474@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201104/34 X-Sequence-Number: 35281 Humair Mohammed writes: > I am running into a behavior with a postgresql function with a SETOF refcursor's returning multiple columns. Not sure if there is a different way to retrieve a SETOF refcursor's with variable columns? Alternatively can I return a primitive value and a refcursor from the same function. I tried specifying this as OUT parameters without any luck. In Oracle you can pass this in functions: > Platform:postgresql-x64-9.0 (PostgreSQL 9.0.1, compiled by Visual C++ build 1500, 64-bit)Java1.6JDBC4 Postgresql Driver, Version 9.0-801 > Function:CREATE OR REPLACE FUNCTION test() RETURNS SETOF refcursor AS$BODY$DECLAREref1 refcursor;ref2 refcursor;BEGIN OPEN ref1 FOR SELECT 1; RETURN NEXT ref1; OPEN ref2 FOR SELECT 2, 3; RETURN NEXT ref2; RETURN;END; $BODY$ LANGUAGE plpgsql > Java Code:CallableStatement cs = conn.prepareCall("{ call test() }");ResultSet rs = cs.executeQuery(); > while (rs.next()) { System.out.println(rs.getString(1)); ResultSet rs2 = (ResultSet)rs.getObject(1); while (rs2.next()) { ResultSetMetaData rsmd = rs2.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); System.out.println("numberOfColumns: " + numberOfColumns); System.out.println(rs2.getString(1)); System.out.println(rs2.getString(2)); }} > Output:numberOfColumns: 11org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1. at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkColumnIndex(AbstractJdbc2ResultSet.java:2680) at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2697) at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1872) at PgBlob.test(PgBlob.java:64) at PgBlob.main(PgBlob.java:37) > It appears the second result-set takes in the number of columns from the first irrespective of the number of columns from the second. If the change the function to return 2 refcursor's with same number of columns then it works as expected. > Function:CREATE OR REPLACE FUNCTION test() RETURNS SETOF refcursor AS$BODY$DECLAREref1 refcursor;ref2 refcursor;BEGIN OPEN ref1 FOR SELECT 1, null; RETURN NEXT ref1; OPEN ref2 FOR SELECT 2, 3; RETURN NEXT ref2; RETURN;END; $BODY$ LANGUAGE plpgsql > Output:numberOfColumns: 214numberOfColumns: 223 The example function works okay for me in psql. I think this is actually a question about how to deal with such cases through the JDBC driver, so I'd suggest asking on the pgsql-jdbc list. (Perhaps in a less messy format this time, and could we ask for a useful Subject: line too?) regards, tom lane