Re: How do I detect End-of-table or End-of-limit ?

From: L Bayuk <lbayuk(at)pobox(dot)com>
To: Jan van der Weijde <Jan(dot)van(dot)der(dot)Weijde(at)attachmate(dot)com>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: How do I detect End-of-table or End-of-limit ?
Date: 2007-01-12 00:41:45
Message-ID: 20070112004145.GA356@mail.mindspring.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

On Thu, Jan 11, 2007 at 11:30:48AM +0100, Jan van der Weijde wrote:
> Hello all,
>
> I am using the libpq interface method PQexec() to execute SELECT
> statements with a LIMIT clause. I declare a cursor with hold and use
> PQecec() to execute FETCH NEXT on that cursor.
> At a certain moment PQntuples() returns 0 after a fetch, but I cannot
> check for the actual reason: is the physical end of the table (or
> selection) reached or is the end of my limit reached.
> In the former case I'm done. But in the latter case I can continue
> selecting records by using SELECT with a LIMIT and OFFSET clause.
> ...

I'm not sure if this applies to you, but what I do is if I want N records,
I select with limit N+1. If I get N or fewer records back, it reached end
of table. If I get N+1 back, I process the first N of them and discard the
last, and I know there are more records. Next query starts at the discarded
one. So there is 1 record overlap on the queries. And unless the data
changes between queries, you will never get 0 records back.

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message Jan van der Weijde 2007-01-12 13:35:08 Re: How do I detect End-of-table or End-of-limit ?
Previous Message Alvaro Herrera 2007-01-12 00:36:41 Re: How do I detect End-of-table or End-of-limit ?