pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Sebastiaan van Erk <sebster@sebster.com>
To: pgsql-jdbc@postgresql.org
Subject: Limit vs setMaxRows issue
Date: Wed, 21 Jun 2006 11:11:51 +0200
Message-ID: <44990D57.9070506@sebster.com> (raw)

Hi,

When using the ps.setMaxRows() call on PreparedStatement, the jdbc 
driver sets the row limit via the "raw" postgres protocol. However, in 
the words of Tom Lane, "the row limit in the protocol only says how many 
rows to deliver in the first batch. The presumption is that you'll 
eventually grab the rest, and so the query is planned on that basis."

What this means that when we do the following query:

select action_id from actions order by action_id

with a ps.setMaxRows(100), it takes about 1.8 seconds for the query to 
complete. However, if we do the following query:

select action_id from actions order by action_id limit 100

without any ps.setMaxRows() the query only takes 0.156 seconds. This is 
more than a factor of 10 faster.

I'm not 100% sure of what JDBC says about setMaxRows (it's kind of 
ambiguous in the java doc, as usual), but as far as I can tell, if you 
call setMaxRows on the prepared statement there is no way in to ever 
retrieve more than that number of rows. If this is indeed the case, it 
seems to me that currently there is a mismatch between the JDBC api and 
the postgresql api, and JDBC should somehow tell postgres that this is a 
hard limit and it should not plan for a second batch.

Therefore, my question is: is this a bug? It is not feasable for me to 
add LIMIT clauses to all the SQL queries in my code, so if this IS a 
bug, I hope it can be fixed. If it is NOT a bug, is there an alternative 
workaround that does not involve changing all of my sql statements?

Thanks in advance,
Sebastiaan





view thread (23+ messages)  latest in thread

Message-ID: <44990D57.9070506@sebster.com>
Permalink:  ../44990D57.9070506@sebster.com/
Also on:    postgresql.org/message-id/44990D57.9070506@sebster.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: sebster@sebster.com
  Subject: Re: Limit vs setMaxRows issue
  In-Reply-To: <44990D57.9070506@sebster.com>
  References: <44990D57.9070506@sebster.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