Re: Counting records in a PL/pgsql cursor - Mailing list pgsql-general

From Magnus Hagander
Subject Re: Counting records in a PL/pgsql cursor
Date
Msg-id 6BCB9D8A16AC4241919521715F4D8BCEA358A0@algol.sollentuna.se
Whole thread Raw
In response to Re: Counting records in a PL/pgsql cursor  (Richard Troy <rtroy@ScienceTools.com>)
Responses Re: Counting records in a PL/pgsql cursor
List pgsql-general
> > > I can deal with materializing the resultset, but I want
> to get away
> > > from the loop-a-thousand-times-doing-plus-one...
> >
> > i dont think its possible.  note that you can make a
> refcursor inside
> > your plpgsql function and pass it to an sql function which
> can do sql
> > cursor operations on it -- i think :-)..haven't tried it yet.
> >
> > merlin
>
> ...If you know your application well enough, you might get
> away with doing a select count() with the same where clause
> just before entering the cursor. It _could_ of course be
> wrong, though! OTOH, it would be much faster. If the only
> down-side is occasionally giving users an incorrect count,
> then perhaps call it a "row estimate", and let them marvel at
> how accurate the estimate is most of hte time!

Nope, the query is way too expensive to run it twice. (GIN scan over
well over half a million rows. It's faster to do the
get-as-cursor-then-loop-and-increment, I've measured that)

//Magnus

pgsql-general by date:

Previous
From: "Merlin Moncure"
Date:
Subject: Re: Counting records in a PL/pgsql cursor
Next
From: Richard Huxton
Date:
Subject: Re: I know the bad way...what is the good way?