Thread: Getting number of matched records from query

Getting number of matched records from query

From
Michael Owens
Date:
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.




Re: Getting number of matched records from query

From
Stephan Szabo
Date:
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.