Re: Cursors: getting the number of tuples; moving backwards - Mailing list pgsql-general

From am@fx.ro
Subject Re: Cursors: getting the number of tuples; moving backwards
Date
Msg-id 20021101201433.A568@coto
Whole thread Raw
In response to Re: Cursors: getting the number of tuples; moving backwards  (Martijn van Oosterhout <kleptog@svana.org>)
Responses Re: Cursors: getting the number of tuples; moving backwards  (Martijn van Oosterhout <kleptog@svana.org>)
List 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



pgsql-general by date:

Previous
From: Roj Niyogi
Date:
Subject: Re: Contribute link broken
Next
From: Jean-Luc Lachance
Date:
Subject: Re: SELECT Query