Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 27F809FB1DC for ; Thu, 11 Jan 2007 21:44:44 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 37572-03 for ; Thu, 11 Jan 2007 21:44:31 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from elasmtp-spurfowl.atl.sa.earthlink.net (elasmtp-spurfowl.atl.sa.earthlink.net [209.86.89.66]) by postgresql.org (Postfix) with ESMTP id CFC979FA398 for ; Thu, 11 Jan 2007 21:44:36 -0400 (AST) Received: from [4.238.210.194] (helo=mindspring.com) by elasmtp-spurfowl.atl.sa.earthlink.net with asmtp (TLSv1:AES256-SHA:256) (Exim 4.34) id 1H5BT7-0002Rz-PE; Thu, 11 Jan 2007 20:44:34 -0500 Received: (from lbayuk@localhost) by mindspring.com (8.13.7/8.13.7) id l0C0fjWa000362; Thu, 11 Jan 2007 19:41:45 -0500 Date: Thu, 11 Jan 2007 19:41:45 -0500 From: L Bayuk To: Jan van der Weijde Cc: pgsql-interfaces@postgresql.org Subject: Re: How do I detect End-of-table or End-of-limit ? Message-ID: <20070112004145.GA356@mail.mindspring.com> References: <4B9C73D1EB78FE4A81475AE8A553B3C67DC52B@exch-lei1.attachmate.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4B9C73D1EB78FE4A81475AE8A553B3C67DC52B@exch-lei1.attachmate.com> User-Agent: Mutt/1.4.2.2i X-ELNK-Trace: 7a0205cf9d42df0a5e89bb4777695beb69025943178dce0bd8fe479c4c05b28b704f8b2e5ac6bcae350badd9bab72f9c350badd9bab72f9c350badd9bab72f9c X-Originating-IP: 4.238.210.194 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.217 tagged_above=0 required=5 tests=AWL, BAYES_50, DNS_FROM_RFC_ABUSE, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200701/16 X-Sequence-Number: 6394 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.