Re: Counting records in a PL/pgsql cursor

From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: "Magnus Hagander" <mha(at)sollentuna(dot)net>
Cc: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Counting records in a PL/pgsql cursor
Date: 2006-11-03 13:11:35
Message-ID: b42b73150611030511x31cb7a22n92d1518695fe6b8a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/3/06, Magnus Hagander <mha(at)sollentuna(dot)net> wrote:
> > > Is there any way to count how many hits I got in a cursor
> > in PL/pgsql?
> > >
> > > I have a function that will "window" through the result of
> > a (large)
> > > query based on two parameters, but I also want to return
> > the number of
> > > hits to the client. Right now I'm looping through the entire cursor
> > > and incrementing a local variable, which I later return (along with
> > > the first <n> records in the resultset) to the client. But
> > this seems
> > > horribly inefficient... I'd just like to ask "how many rows are in
> > > this cursor", is there a way to do that without looping
> > through them all?
> >
> > You can move to the end, look at the row number, then move to
> > the beginning. It will still need to materialise the entire
> > resultset though.
>
> How do I do that? remember this is a pL/pgsql cursor. From what I can
> find at
> http://www.postgresql.org/docs/8.1/static/plpgsql-cursors.html#PLPGSQL-C
> URSOR-USING, I can only do FETCH to get the next row, or CLOSE.
>
> 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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2006-11-03 13:13:22 Re: Isolation / Visibility inside a trigger
Previous Message Jorge Godoy 2006-11-03 12:49:17 Isolation / Visibility inside a trigger