Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 37CFA632982; Mon, 19 Apr 2010 20:29:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 09879-05; Mon, 19 Apr 2010 23:28:51 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f216.google.com (mail-bw0-f216.google.com [209.85.218.216]) by mail.postgresql.org (Postfix) with ESMTP id EF1696325C4; Mon, 19 Apr 2010 20:28:50 -0300 (ADT) Received: by bwz8 with SMTP id 8so4994630bwz.3 for ; Mon, 19 Apr 2010 16:28:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:received:message-id :subject:from:to:cc:content-type; bh=UmJbYbwcLDv5ok0S3X52oBwdRdwpbbzX3lUBQnxpZA0=; b=neAbOvlTeg/mBdhU5TWqxQywgEObR0We/5b0x5zpDDPWeZDQbCd28dCYcpaNWJaRUJ nZOcD5jO/bQPje6T97NYVpzjW1xYr5P9kyqeW6TsJQMH9QzkfY3+ctFkbYDHCA6wTMe7 mg9dX4CvG3/A4+IDDQTQjVUNkQ5VgflQkq1Lk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:cc:content-type; b=mTHLL2YuQv9A/Hwznv1C0uo907eiUT12PNc2+3dUdo11Fad0vJbKwkyniPQveKOeEa QgTtvc8+Uuh+mC2jvhSdZiXXIsKQ5l0P5BaEba2soeSKt5nmVIQjNTcClUxHhA5tNiy+ VRHR6LK5Exh1LH6Xb0JFFGpA54agXsZ/fl6S8= MIME-Version: 1.0 Received: by 10.204.66.8 with HTTP; Mon, 19 Apr 2010 16:28:49 -0700 (PDT) Date: Mon, 19 Apr 2010 18:28:49 -0500 Received: by 10.204.163.129 with SMTP id a1mr5280447bky.144.1271719729072; Mon, 19 Apr 2010 16:28:49 -0700 (PDT) Message-ID: Subject: Getting rid of a cursor from JDBC .... Re: [PERFORM] Re: HELP: How to tame the 8.3.x JDBC driver with a biq guery result set From: Dave Crooke To: Scott Carey Cc: pgsql-performance , pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=0003255586eab489aa04849f5245 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, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201004/29 X-Sequence-Number: 20440 --0003255586eab489aa04849f5245 Content-Type: text/plain; charset=UTF-8 Scott - I tried to post a SOLVED followup to the JDBC list but it was rejected :-! I now have the opposite problem of getting rid of the cursor :-) ResultSet.close() does not work. I am trying to do a DROP TABLE from the other Connection, to whack the table I just finished the ETL on, but it just hangs indefintiely, and pg_locks shows the shared read lock still sitting there. I am trying a Statement.close() and Connection.close() now, but I fear I may have to do something slightly ugly, as I have Apache DBCP sitting in between me and the actual PG JDBC driver. I am hoping the slightly ugly thing is only closing the underlying connection, and does not have to be */etc/init.d/postgresql8.3 restart* :-) Is there a backdoor way to forcibly get rid of a lock you don't need any more? Cheers Dave On Mon, Apr 19, 2010 at 1:05 PM, Scott Carey wrote: > On Apr 15, 2010, at 1:01 PM, Dave Crooke wrote: > > On Thu, Apr 15, 2010 at 2:42 PM, Dave Crooke wrote: > > Hey folks > > > > I am trying to do a full table scan on a large table from Java, using a > straightforward "select * from foo". I've run into these problems: > > > > 1. By default, the PG JDBC driver attempts to suck the entire result set > into RAM, resulting in java.lang.OutOfMemoryError ... this is not cool, in > fact I consider it a serious bug (even MySQL gets this right ;-) I am only > testing with a 9GB result set, but production needs to scale to 200GB or > more, so throwing hardware at is is not feasible. > > > > For scrolling large result sets you have to do the following to prevent it > from loading the whole thing into memory: > > > Use forward-only, read-only result scrolling and set the fetch size. Some > of these may be the default depending on what the connection pool is doing, > but if set otherwise it may cause the whole result set to load into memory. > I regularly read several GB result sets with ~10K fetch size batches. > > Something like: > Statement st = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, > java.sql.ResultSet.CONCUR_READ_ONLY) > st.setFetchSize(FETCH_SIZE); > That's what I''m using, albeit without any args to createStatement, and it now works. > > > > > 2. I tried using the official taming method, namely > java.sql.Statement.setFetchSize(1000) and this makes it blow up entirely > with an error I have no context for, as follows (the number C_10 varies, > e.g. C_12 last time) ... > > > > org.postgresql.util.PSQLException: ERROR: portal "C_10" does not exist > > at > org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592) > > at > org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327) > > at > org.postgresql.core.v3.QueryExecutorImpl.fetch(QueryExecutorImpl.java:1527) > > at > org.postgresql.jdbc2.AbstractJdbc2ResultSet.next(AbstractJdbc2ResultSet.java:1843) > > > > This is definitely a bug :-) > > > > > > I have no idea what that is. > It was because I was also writing to the same Connection ... when you call Connection.commit() with the PG JDBC driver, it also kills all your open cursors. I think this is a side effect of the PG internal design where it does MVCC within a table (rows have multiple versions with min and max transaction ids) ... even a query in PG has a notional virtual transaction ID, whereas in e.g. Oracle, a query has a start time and visibility horizon, and as long as you have enough undo tablespace, it has an existence which is totally independent of any transactions going on around it even on the same JDBC connection. --0003255586eab489aa04849f5245 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Scott - I tried to post a SOLVED followup to the JDBC list but it was rejec= ted :-!

I now have the opposite problem of getting rid of the cursor= :-) ResultSet.close() does not work. I am trying to do a DROP TABLE from t= he other Connection, to whack the table I just finished the ETL on, but it = just hangs indefintiely, and pg_locks shows the shared read lock still sitt= ing there.

I am trying a Statement.close() and Connection.close() now, but I fear = I may have to do something slightly ugly, as I have Apache DBCP sitting in = between me and the actual PG JDBC driver.

I am hoping the slightly u= gly thing is only closing the underlying connection, and does not have to b= e /etc/init.d/postgresql8.3 restart :-) Is there a backdoor way to f= orcibly get rid of a lock you don't need any more?

Cheers
Dave

On Mon, Apr 19, 2010 a= t 1:05 PM, Scott Carey <scott@richrelevance.com> wrote:
On Apr 15, 2010, at 1:01 PM, Dave Crooke wrote:
> On Thu, Apr 15, 2010 at 2:42 PM, Dave Crooke <dcrooke@gmail.com> wrote:
> Hey folks
>
> I am trying to do a full table scan on a large table from Java, using = a straightforward "select * from foo". I've run into these pr= oblems:
>
> 1. By default, the PG JDBC driver attempts to suck the entire result s= et into RAM, resulting in java.lang.OutOfMemoryError ... this is not cool, = in fact I consider it a serious bug (even MySQL gets this right ;-) I am on= ly testing with a 9GB result set, but production needs to scale to 200GB or= more, so throwing hardware at is is not feasible.
>

For scrolling large result sets you have to do the following to preve= nt it from loading the whole thing into memory:


Use forward-only, read-only result scrolling and set the fetch size. =C2=A0= Some of these may be the default depending on what the connection pool is d= oing, but if set otherwise it may cause the whole result set to load into m= emory. =C2=A0I regularly read several GB result sets with ~10K fetch size b= atches.

Something like:
Statement st =3D =C2=A0conn.createStatement(java.sql.ResultSet.TYPE_FORWARD= _ONLY, java.sql.ResultSet.CONCUR_READ_ONLY)
st.setFetchSize(FETCH_SIZE);

That's what I'= ;'m using, albeit without any args to createStatement, and it now works= .
=C2=A0



> 2. I tried using the official taming method, namely java.sql.Statement= .setFetchSize(1000) and this makes it blow up entirely with an error I have= no context for, as follows (the number C_10 varies, e.g. C_12 last time) .= ..
>
> org.postgresql.util.PSQLException: ERROR: portal "C_10" does= not exist
> =C2=A0 =C2=A0 at org.postgresql.core.v3.QueryExecutorImpl.receiveError= Response(QueryExecutorImpl.java:1592)
> =C2=A0 =C2=A0 at org.postgresql.core.v3.QueryExecutorImpl.processResul= ts(QueryExecutorImpl.java:1327)
> =C2=A0 =C2=A0 at org.postgresql.core.v3.QueryExecutorImpl.fetch(QueryE= xecutorImpl.java:1527)
> =C2=A0 =C2=A0 at org.postgresql.jdbc2.AbstractJdbc2ResultSet.next(Abst= ractJdbc2ResultSet.java:1843)
>
> This is definitely a bug :-)
>
>

I have no idea what that is.

It was because = I was also writing to the same Connection ... when you call Connection.comm= it() with the PG JDBC driver, it also kills all your open cursors.

I think this is a side effect of the PG internal design where it does M= VCC within a table (rows have multiple versions with min and max transactio= n ids) ... even a query in PG has a notional virtual transaction ID, wherea= s in e.g. Oracle, a query has a start time and visibility horizon, and as l= ong as you have enough undo tablespace, it has an existence which is totall= y independent of any transactions going on around it even on the same JDBC = connection.


=C2=A0

--0003255586eab489aa04849f5245--