Getting number of matched records from query

Lists: pgsql-hackers
From: Michael Owens <mike(at)mikesclutter(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Getting number of matched records from query
Date: 2003-11-02 04:54:05
Message-ID: 200311012254.06132.mike@mikesclutter.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I should perhaps dig a little more on this one, but per the C API docs I don't
think what I am asking for is available. This somewhat relates to count().

Is there a reasonable way that information regarding the number of matched
records could be passed back to the client --- even in the case of a LIMIT?
PQntuples() only returns the number of tuples in the result set (restricted by
the limit clause). Is there a way to get all matched records in the query
(before they were limited)? Perhaps this number could be sent back through
PQcmdTuples(), in which case this would be the meaning of its return value
for the SELECT queries.

This is useful in a limit/offset situation such as a web based search where
you want to divide the results up over several pages. In order to display any
kind of navigation, you need to know the total number of records up front so
you can display to the client the number of pages it would take to step
through all of the results, N records at a time.

If this were available, then it would obviate the need to count(*) the query
before executing it, which could save quite a bit of time.

Isn't this information (the number of matched records) available to the
backend --- even in the case of LIMIT? All matched records still have to be
materialized right? Therefore the record count should be available and could
therefore be available to the client. Is this correct?

I would be willing to work on implementing this, but I would just like to know
if this is even possible first.


From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: Michael Owens <mike(at)mikesclutter(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Getting number of matched records from query
Date: 2003-11-02 05:58:00
Message-ID: 20031101214918.D58021@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Sat, 1 Nov 2003, Michael Owens wrote:

> Isn't this information (the number of matched records) available to the
> backend --- even in the case of LIMIT? All matched records still have to be
> materialized right? Therefore the record count should be available and could
> therefore be available to the client. Is this correct?

No. For many plans, the entire record set does not have to be fully
materialized in the presence of LIMIT. In general, the limit node should
have the effect of making such plans more likely to be chosen.