Re: out of memory for query result - Mailing list pgsql-general

From Douglas McNaught
Subject Re: out of memory for query result
Date
Msg-id m28xwkqgn4.fsf@Douglas-McNaughts-Powerbook.local
Whole thread Raw
In response to Re: out of memory for query result  (Allen Fair <allen@cyberdesk.com>)
List pgsql-general
Allen Fair <allen@cyberdesk.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


pgsql-general by date:

Previous
From: Chris
Date:
Subject: Recovery after server crash
Next
From: Martijn van Oosterhout
Date:
Subject: Re: out of memory for query result