Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

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



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.



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group