X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 68252D1D6D0 for ; Wed, 12 May 2004 00:07:51 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 13724-06 for ; Wed, 12 May 2004 00:07:29 -0300 (ADT) Received: from mail1.yozons.com (mail1.yozons.com [206.80.109.179]) by svr1.postgresql.org (Postfix) with ESMTP id 93A77D1B367 for ; Wed, 12 May 2004 00:07:28 -0300 (ADT) Received: from rasta (soho2.yozons.com [206.80.109.178] (may be forged)) (authenticated bits=0) by mail1.yozons.com (8.12.8/8.12.8) with ESMTP id i4C37K01001902 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NOT); Tue, 11 May 2004 20:07:21 -0700 Message-ID: <042801c437ce$422f4000$3201a8c0@rasta> Reply-To: "David Wall" From: "David Wall" To: "Oliver Jowett" Cc: , References: <20040511103520.1ADC6D1B1EE@svr1.postgresql.org> <40A0CB55.5060400@opencloud.com> <024101c4376d$d567d050$3201a8c0@rasta> <1084305394.1534.121.camel@localhost.localdomain> <036601c43797$4312ee40$3201a8c0@rasta> <1084315868.1536.132.camel@localhost.localdomain> <03f401c437b6$1bb615b0$3201a8c0@rasta> <40A178C9.5040903@opencloud.com> Subject: Re: JDBC and processing large numbers of rows Date: Tue, 11 May 2004 20:07:12 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200405/68 X-Sequence-Number: 10078 Thanks Oliver and Sean. I was just trying to do it the hard way using plain SQL. It's great that the ResultSet can handle this for me. I'll give that a try. David ----- Original Message ----- From: "Oliver Jowett" To: "David Wall" Cc: ; Sent: Tuesday, May 11, 2004 6:07 PM Subject: Re: [JDBC] JDBC and processing large numbers of rows > David Wall wrote: > > Thanks, Dave. Does anybody have any simple examples of the series of JDBC > > calls used to declare, open, fetch and close a cursor in PG? In Oracle? I > > know this is a PG list, so if no Oracle examples, can anybody at least > > confirm that using cursors with Oracle and standard JDBC is possible? > > > > There's nothing like having to write custom code to implement what Java > > purports to be write once, run anywhere! It seems that the JDBC spec would > > have to be severely lacking if you can't do something as simple (and old) as > > use cursors in a standard way. > > I don't know -- that seems more a criticism of SQL than of JDBC. I don't > think DECLARE as a query statement exists in the SQL specs at all? The > DECLARE docs say: > > > The SQL standard only makes provisions for cursors in embedded SQL. The > > PostgreSQL server does not implement an OPEN statement for cursors; a > > cursor is considered to be open when it is declared. However, ECPG, the > > embedded SQL preprocessor for PostgreSQL, supports the standard SQL > > cursor conventions, including those involving DECLARE and OPEN > > statements. > > Anyway, back to your original question .. > > JDBC *does* provide a standard way of using cursors to page through > result data. It's called ResultSet. Take a look at the ResultSet row > movement primitives -- they look suspiciously like cursor movement > primitives, don't they? > > I'd suggest using an appropriate resultset type (SCROLLABLE_* or > FORWARD_ONLY, depending on your access patterns) and use setFetchSize() > and setFetchDirection() to hint to the driver about what you're doing. > Then just run your unmodified, cursor-less query. With a good driver > implementation you should get paging of the resultset transparently. > This is why all the different resultset types and fetch hints are there > in the first place.. > > The current postgresql driver will page results from the backend if you > use FORWARD_ONLY, a non-zero fetchsize, and autocommit off. It isn't > quite there yet for scrollable resultsets, but it's close (see the > recent patches from Andy Zeneski). Note that using a scrollable > resultset can be more expensive as the backend can only provide > scrollable cursors for free in some cases -- in other cases, it has to > materialize the whole resultset. This is a cost you'll end up paying > regardless of whether you're using cursors via ResultSet, cursor > manipulation directly, or LIMIT/OFFSET tricks (at least assuming you > actually want to (eventually) process all the data from a query and not > just a subset). > > Then we just need holdable resultset support (for the autocommit case -- > there's a parameter in JDBC3 for controlling this as holdable cursors > aren't free, especially if you have very large result sets) and it'd be > all transparent. > > I don't know how Oracle handles all this, but hiding the cursor work > inside the ResultSet seems like the Right Way to do it. > > -O