Re: Cursors: getting the number of tuples; moving backwards

am@fx.ro

From: am@fx.ro
To: pgsql-general@postgresql.org
Date: 2002-11-01T18:14:33Z
Lists: pgsql-hackers, pgsql-general
On Fri, Nov 01, 2002 at 07:23:29PM +1100, Martijn van Oosterhout wrote:
> > The solution i've found was something like:
> > 
> >     int nr_tuples;
> > 
> >     res = PQexec(conn, "MOVE ALL in CURS");    
> >     sscanf(PQcmdStatus(res),"MOVE %i",&nr_tuples);
> >     PQclear(res);
> 
> That would work. But why do you need to know the total beforehand? You could
> just do a FETCH ALL and then use PQntuples to get the number. 

If the table has, let's say, 10000 rows, it's unlikely that the user
will ever browse all of them ( my program permits the user to set some
filters ; the interface is ncurses-based).  Fetching everything 
would be unnecessary. 

So, for speed reasons, i prefer to fetch maximum 500 rows.
But i want to display in the screen's corner the total number
of rows .  

> > I'm wondering: is there any better way to get that number?
> > 
> > ( just an idea:  maybe it would be useful to make PQcmdTuples
> >   work for MOVE commands ... ? )
> 
> Interesting idea. I'm not sure whether MOVE actually executes the query or
> not.

I guess it doesn't execute the whole query. MOVE ALL is *much*
faster than FETCH ALL + PQcmdTuples

> > --2-- I found out that if i reach the end of the cursor, and want
> > to move backwards, i have to increase the MOVE command's argument by 1:
> 
> No idea, the cursor has probably moved off the end to indicate the query is
> done. So you need the extra one to move it back. That's just a guess though.
 
Yeah, this could be the explanation.


Thanks for your answer

Adrian Maier