Re: out of memory for query result

From: Martijn van Oosterhout <kleptog(at)svana(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:40:28
Message-ID: 20051023174023.GA11267@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Oct 22, 2005 at 06:15:59PM -0400, Allen Fair wrote:
> 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.

Well, DBI doesn't support doing the cursor bit for you. But you can use
cursors just fine. Your code is almost there:

> $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;

for(;;)
{
$sth = $dbh->prepare("fetch 1000 from csr");
$sth->execute();
last if( $sth->rows == 0 );

> while (my $hr = $sth->fetchrow_hashref) {
> # do something wonderful
> }
> $sth->finish();

}

> Is this a Perl only restriction? How about Python or Ruby?

It's not a restriction. No other language does it automatically either.

Hope this helps,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2005-10-23 17:51:31 Re: Recovery after server crash
Previous Message Douglas McNaught 2005-10-23 17:37:03 Re: out of memory for query result