X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 14C02539D4 for ; Thu, 14 Apr 2005 21:36:23 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 88910-06 for ; Fri, 15 Apr 2005 00:36:14 +0000 (GMT) Received: from unicorn.rentec.com (rtc10-252.rentec.com [216.223.240.9]) by svr1.postgresql.org (Postfix) with ESMTP id 03544539CF for ; Thu, 14 Apr 2005 21:36:12 -0300 (ADT) Received: from wren.rentec.com (wren.rentec.com [192.5.35.106]) by unicorn.rentec.com (8.13.1/8.12.1) with ESMTP id j3F0aCcw002408 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Thu, 14 Apr 2005 20:36:13 -0400 (EDT) X-Rentec: external Received: from [172.16.160.108] (stange-dhcp2.rentec.com [172.16.160.108]) by wren.rentec.com (8.13.1/8.12.1) with ESMTP id j3F0aA8w000925; Thu, 14 Apr 2005 20:36:10 -0400 (EDT) Message-ID: <425F0280.50005@rentec.com> Date: Thu, 14 Apr 2005 19:53:36 -0400 From: Alan Stange User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Kristina Magwood Cc: pgsql-jdbc@postgresql.org Subject: Re: process large tables References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Logged: Logged by unicorn.rentec.com as j3F0aCcw002408 at Thu Apr 14 20:36:13 2005 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200504/90 X-Sequence-Number: 12697 Kristina Magwood wrote: > > Hi, > I am trying to process a large table. Unfortunately, using select * > from table gives me a ResultSet that is too large. > The java runs out of memory even if I boost the vm memory. > Is there any way I can programmatically (in java) retrieve say 10,000 > records at a time without knowing anything specific about the table? > Then, when I am done with those records, retrieve the next 10,000, etc? > > Thank you in advance for any help you can spare. > Kristina The following should work but was written mostly from memory, so it might not compile. Make sure you're only pulling the columns you need. There are three important bits (the details of which might have changed from pg7 to pg8): 1) the auto commit must be turned off. 2) the fetch direction must be FETCH_FORWARD 3) the query can be only a *single* statement. So no trailing ";" is allowed, as "select * from foo;" is actually three statements (assuming I understand the discussion that happened some time back). You can tell it's working if you see cursors being created on the server, or by using truss/strace type tools on the client...and if your memory footprint is smaller. This was all discussed on the list a few months back, so you might look through the mailing list for more details. Good luck. -- Alan String sql = null; boolean commitState = true; try { Connection conn = getConnectionSomehow(); Statement st = conn.createStatement(); commitState = conn.getAutoCommit(); conn.setAutoCommit(false); // keep the commit state st.setFetchDirection(ResultSet.FETCH_FORWARD); st.setFetchSize(nRows); // number of rows to pull in one chunk. ResultSet rs = st.executeQuery(sql = "select * from someTable"); while (rs.next()) processRow(); } catch (Exception e) { System.err.println("error in processing statement: " + sql); e.printStackTrace(); } finally { // restore the commit state if the connection is going back to a pool... if (conn != null) try {conn.setAutoCommit(commitState); } catch (Exception e) {} if (st != null) try {st.close(); } catch (Exception e) {} if (conn != null) try {conn.close(); } catch (Exception e) {} }