Re: Looping through cursor row batches

From: "Henry Combrinck" <henry(at)zen(dot)co(dot)za>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Looping through cursor row batches
Date: 2008-10-07 06:50:18
Message-ID: 20081007085018.16231glavun5xkfc@zenmail.co.za
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>> Anyone know the most efficient way of FETCHing a batch of rows, and
>> looping >> through them in a function? FETCHing a record at a
>> time will work, but I >> was wondering whether this could be done.
>
> You're outsmarting yourself.

:-) One can only try.

> plpgsql already does the equivalent of
> this under the hood, there is no need for you to try to make it happen
> at user level. Just use a plain ol' FOR rec IN SELECT and forget the
> explicit cursor.

I'm aware of the implicit cursor use in functions, but recall that (pg8.3.3)

(#1)
FOR rec IN SELECT col from dblink_fetch('cursor'..) DO

is running out of memory (see discussion
http://archives.postgresql.org/pgsql-general/2008-06/msg00031.php) due
to an exception block inside the loop (which is possibly leaking
memory - I tried to reduce it to a concise failing case, still trying).

I'm pre-emptively expecting (pessimistically, I know) an OOM error again with:

(#2)
FOR rec IN SELECT col FROM really_huge_table DO
exception block...
END LOOP;

Anyway, I've found that fetching a batch of (say) 100,000 (instead of
10,000) at a time I reduce the likelihood of running out of memory (a
process which does exactly this has been running for the past day or
two; time will tell). I was pondering whether it's possible to do
what I mentioned in my original post - ie, an explicit cursor as the
source object in a FOR loop so I can have a bit more control over how
many rows are fetched each time (instead of 1 at a time).

So, setting aside my self-outsmartiness, is there a way to achieve this?

Regards
Henry

Browse pgsql-general by date

  From Date Subject
Next Message Peter Childs 2008-10-07 07:04:45 Re: how to remove the duplicate records from a table
Previous Message Yi Zhao 2008-10-07 06:36:59 how to remove the duplicate records from a table