Thread: how many tuples on a cursor?

how many tuples on a cursor?

From
Andreas Kretschmer
Date:
Hello,

I want to know how many tuples there on a cursor, and i found

http://groups.google.de/groups?hl=de&lr=&threadm=3ACA7BB0.7020106%402cactus.com&rnum=7&prev=/groups%3Fhl%3Dde%26lr%3D%26q%3Dpostgres%2Bcursor%2Bcount%26btnG%3DSuche

But, this posting is very old (2001-04-03), i'm using 7.4.6.


Is there now a way to get the total number of rows in a CURSOR?


Andreas
-- 
Andreas Kretschmer    (Kontakt: siehe Header)
Heynitz:  035242/47212,      D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net===    Schollglas Unternehmensgruppe    === 


Re: how many tuples on a cursor?

From
Christoph Haller
Date:
Andreas Kretschmer wrote:
> 
> Hello,
> 
> I want to know how many tuples there on a cursor, and i found
>
http://groups.google.de/groups?hl=de&lr=&threadm=3ACA7BB0.7020106%402cactus.com&rnum=7&prev=/groups%3Fhl%3Dde%26lr%3D%26q%3Dpostgres%2Bcursor%2Bcount%26btnG%3DSuche
> 
> But, this posting is very old (2001-04-03), i'm using 7.4.6.
> 
> Is there now a way to get the total number of rows in a CURSOR?
> 
> Andreas
> --

Assuming you are using the libpq interface, 
after FETCH ALL you'll get the number of rows 
by PQntuples(). 
What else seems to work is MOVE 2147483647 
(INT_MAX) and then get the max number of rows 
by PQcmdTuples(). 
Back to the beginning by FETCH ABSOLUTE 0. 
But I have no idea how expensive this MOVE is. 

Regards, Christoph


Re: how many tuples on a cursor?

From
Tom Lane
Date:
Christoph Haller <ch@rodos.fzk.de> writes:
> What else seems to work is MOVE 2147483647 
> (INT_MAX) and then get the max number of rows 
> by PQcmdTuples(). 

I'd suggest "MOVE FORWARD ALL" rather than hard-wiring assumptions
about the maximum possible value of infinity ;-)

> But I have no idea how expensive this MOVE is. 

The rows are all calculated internally --- about all you save compared
to a FETCH is data formatting and transmission.  If you really have to
know this number in advance of fetching the data, that's pretty much
what you have to do, but it will cost you ...
        regards, tom lane


Re: how many tuples on a cursor?

From
Christoph Haller
Date:
Tom Lane wrote:
> 
> Christoph Haller <ch@rodos.fzk.de> writes:
> > What else seems to work is MOVE 2147483647
> > (INT_MAX) and then get the max number of rows
> > by PQcmdTuples().
> 
> I'd suggest "MOVE FORWARD ALL" rather than hard-wiring assumptions
> about the maximum possible value of infinity ;-)
> 

Of course. I should have read the manual on this more carefully. 

Regards, Christoph