Re: out of memory for query result

From: Douglas McNaught <doug(at)mcnaught(dot)org>
To: Allen Fair <allen(at)cyberdesk(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: out of memory for query result
Date: 2005-10-23 17:37:03
Message-ID: m28xwkqgn4.fsf@Douglas-McNaughts-Powerbook.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Allen Fair <allen(at)cyberdesk(dot)com> writes:

> From my googling, it seems the Perl DBD driver for Postgres does
> *not* support the cursor (see below). I hope someone can refute this!
>
> I am otherwise looking for code to implement Postgres cursors in
> Perl. I can not find the "DECLARE CURSOR" defined in the Perl DBI
> documentation either. Thanks Martijn for your reply, it helped me dig
> deeper.
>
> The following code does not work, but I'll keep trying! (I just added
> the declare phrase.)
> $dbh = DBI->connect("DBI:Pg:dbname=$dbName;host=$host",
> $dbUser, $dbPassword,
> { RaiseError => 0, AutoCommit => 0, PrintError => 1 });
> $sth = $dbh->prepare("declare csr cursor for $sqlstatement");
> $sth->execute(@statement_parms) or die $DBI::errstr;

I think you need to DECLARE the cursor first, then for your loop do:

<loop>
FETCH 100 FROM csr;
<loop calling fetchrow_hashref() 100 times or until it returns undef>
<process the row>
</loop>
</loop>

You can execute FETCH once for each row, but it'll be faster to batch
it up as above.

Read up on DECLARE and FETCH in the SQL docs. I don't know of any
reason why you can't use them from Perl; it's just not done
automatically behind the scenes.

-Doug

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2005-10-23 17:40:28 Re: out of memory for query result
Previous Message Chris 2005-10-23 17:31:33 Recovery after server crash