pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Guido Fiala <guido.fiala@dka-gmbh.de>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: JDBC and processing large numbers of rows
Date: Wed, 12 May 2004 23:36:41 +1200
Message-ID: <40A20C49.6090802@opencloud.com> (raw)
In-Reply-To: <200405120837.42865.guido.fiala@dka-gmbh.de>
References: <20040511103520.1ADC6D1B1EE@svr1.postgresql.org>
	<03f401c437b6$1bb615b0$3201a8c0@rasta>
	<40A17610.7040600@earthlink.net>
	<200405120837.42865.guido.fiala@dka-gmbh.de>

Guido Fiala wrote:
> Reading all this i'd like to know if all this isn't just a tradeof between 
> _where_ the memory is consumed?
> 
> If your JDBC-client holds all in memory - it gets an OutOfMem-Exception.

Yes. The current driver attempts to hold the entire resultset in heap if 
not using cursors. In theory, the driver could try to spill large 
resultsets to disk (but see below).

> If your backend uses Cursors - it caches the whole resultset and probably 
> starts swapping and gets slow (needs the memory of all users).

As I understand it (break out the salt!), the backend's cursor behaviour 
depends on both the query and the cursor type. For a NO SCROLL cursor, 
the backend is doing no more real work than for normal query retrieval 
(it's just changing *when* the rows are retrieved). For a SCROLL cursor, 
the backend may or may not need additional storage depending on the 
complexity of the query. When the backend does need to store the 
resultset, it will spill large resultsets to disk rather than keep them 
in-memory (and possibly this storage is more efficient than just storing 
the raw tuples, as it could just reference the original tuples in the 
main table itself -- not sure if this is how it actually works though). 
For a WITH HOLD cursor, in addition to the SCROLL/NOSCROLL behaviour the 
backend will preserve a copy of the resultset when it can no longer 
safely derive the results directly from the database (i.e. when the 
enclosing transaction commits).

The DECLARE documentation discusses some of this.

> If you use Limit and Offset the database has to do more to find the 
> data-snippet and in worst case (last few records) still needs temporary the 
> whole resultset? (not sure here)

I'd guess that in the non-zero OFFSET case the backend skips and 
discards the initial rows (why would it need to store them?), i.e. it's 
similar in cost to doing a DECLARE/MOVE FORWARD/FETCH FORWARD sequence. 
But you end up rerunning the start of the query every time you want some 
more data..

> Is that just a "choose your poison" ? At least in the first case the memory of 
> the Client _gets_ used too and not all load to the backend, on the other side 
> - most the the user does not really read all the data at all, so it puts 
> unnecessary load on all the hardware.

I lean towards using cursors. The backend can handle some cases more 
efficiently than a client can, simply because it knows more about how to 
generate the data and where it might already be stored. Also it seems a 
bit redundant to do the same work to deal with large datasets (spill to 
disk, etc) on both the client and the server.

-O



view thread (17+ messages)  latest in thread

Message-ID: <40A20C49.6090802@opencloud.com>
Permalink:  ../40A20C49.6090802@opencloud.com/
Also on:    postgresql.org/message-id/40A20C49.6090802@opencloud.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: oliver@opencloud.com, guido.fiala@dka-gmbh.de
  Subject: Re: JDBC and processing large numbers of rows
  In-Reply-To: <40A20C49.6090802@opencloud.com>
  References: <20040511103520.1ADC6D1B1EE@svr1.postgresql.org> <03f401c437b6$1bb615b0$3201a8c0@rasta> <40A17610.7040600@earthlink.net> <200405120837.42865.guido.fiala@dka-gmbh.de> <40A20C49.6090802@opencloud.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox