DBD::Pg & cursors & total results

From: Patrick Verdon <patrick(at)kan(dot)co(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: DBD::Pg & cursors & total results
Date: 1998-10-04 00:39:31
Message-ID: 3616C3C3.8894596C@kan.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I'm using PostgreSQL with Apache and mod_perl to keep
persistent connections. Thus far I have been doing
standard selects using DBD::Pg on tables sometimes
returning 1000s of records. This becomes a problem with
the larger queries, as the memory needed by Perl to
receive the result set bloats the Web server processes
horrendously (exceeding 20+ MB per process).

To address this memory issue I've started using cursors to
select the 10 or 20 records that the user will see
at one time. This alleviates the memory issue to some
extent, however this approach creates a new problem: I
can't seem to (easily) determine the total results that
a select statement will yield when using a cursor. The
only way I've managed to get hold of the total results
is by doing the following, which seems absolutely
ludicrous (!):

$dbh = DBI->connect( ...)
$dbh->{AutoCommit} = 0;
$dbh->do("declare c cursor for select * from reports order by id");
$dbh->do("move 10 in c");

--> $dbh->do("select * into table temp from reports where id > 0");
--> my $num_rows = $dbh->do("delete from temp where id > 0");
--> $dbh->do("drop table temp");

my $sth = $dbh->prepare("fetch 20 in c");
$sth->execute();
while ( my $r = $sth->fetchrow_hashref ) { .. }
$dbh->commit();
$sth->finish();
$dbh->disconnect();

Is there any other less expensive way to find out what
$num_rows is going to be for a select statement? Am I
missing something obvious?

I'd be grateful for any help / thoughts.

Cheers.

Patrick

--

#===============================#
\ KAN Design & Publishing Ltd /
/ T: +44 (0)1223 511134 \
\ F: +44 (0)1223 571968 /
/ E: mailto:patrick(at)kan(dot)co(dot)uk \
\ W: http://www.kan.co.uk /
#===============================#

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 1998-10-04 01:57:30 Re: [HACKERS] RE: [GENERAL] Long update query ? (also Re: [GENERAL] CNF vs. DNF)
Previous Message Agape 1998-10-03 18:35:35 [GENERAL] Question programming in C, PGresult *