dbTalk Databases Forums  

[BUGS] BUG #1756: PQexec eats huge amounts of memory

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] BUG #1756: PQexec eats huge amounts of memory in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Denis Vlasenko
 
Posts: n/a

Default [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-06-2005 , 08:18 AM







The following bug has been logged online:

Bug reference: 1756
Logged by: Denis Vlasenko
Email address: vda (AT) ilport (DOT) com.ua
PostgreSQL version: 8.0.1
Operating system: Linux
Description: PQexec eats huge amounts of memory
Details:

Verbatim from http://bugs.php.net/bug.php?id=33587:

Description:
------------
Seen on php-4.3.4RC2. Since I was just testing how good
PG fares compared to Oracle, and I am not feeling any
real pain from this (IOW: not my itch to scratch),
I do not research this in depth, apart from submitting
bug report. Sorry.

Symptom: even the simplest query
$result = pg_query($db, "SELECT * FROM big_table");
eats enormous amounts of memory on server
(proportional to table size).

I think this is a problem with PostgreSQL client libs.
php's source is included for easy reference.

PHP_FUNCTION(pg_query)
{

....
pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query));
if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) !=
CONNECTION_OK) {
PQclear(pgsql_result);
PQreset(pgsql);
pgsql_result = PQexec(pgsql, Z_STRVAL_PP(query));
}

if (pgsql_result) {
status = PQresultStatus(pgsql_result);
} else {
status = (ExecStatusType) PQstatus(pgsql);
}

switch (status) {
case PGRES_EMPTY_QUERY:
case PGRES_BAD_RESPONSE:
case PGRES_NONFATAL_ERROR:
case PGRES_FATAL_ERROR:
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Query failed: %s.", PQerrorMessage(pgsql));
PQclear(pgsql_result);
RETURN_FALSE;
break;
case PGRES_COMMAND_OK: /* successful command that did
not return rows */
default:
if (pgsql_result) {
pg_result = (pgsql_result_handle *)
emalloc(sizeof(pgsql_result_handle));
pg_result->conn = pgsql;
pg_result->result = pgsql_result;
pg_result->row = 0;
ZEND_REGISTER_RESOURCE(return_value,
pg_result, le_result);
} else {
PQclear(pgsql_result);
RETURN_FALSE;
}
break;
}
}

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Reply With Quote
  #2  
Old   
Harald Armin Massa
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-06-2005 , 08:53 AM






------=_Part_3238_26198124.1120657950846
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Denis,

$result =3D pg_query($db, "SELECT * FROM big_table");
Quote:
=20
you are reading a big result (as I suspect from big_table) into memory. It=
=20
is perfectly normal that this uses large amounts of memory.

[it would be rather suspicious if loading a big file / big resultset would=
=20
not use big amounts of memory]

Harald


--=20
GHUM Harald Massa
persuasion python postgresql
Harald Armin Massa
Reinsburgstra=DFe 202b
70197 Stuttgart
0173/9409607

------=_Part_3238_26198124.1120657950846
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Denis,<br>
<br>
<div><blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(=
204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">$result =3D =
pg_query($db, &quot;SELECT * FROM big_table&quot;<br></blockquote></div><=
br>

you are reading a big result (as I suspect from big_table) into memory.
It is perfectly normal that this uses large amounts of memory.<br>
<br>
[it would be rather suspicious if loading a big file / big resultset would =
not use big amounts of memory]<br>
<br>
Harald<br>
<br>
<br>-- <br>GHUM Harald Massa<br>persuasion python postgresql<br>Harald Armi=
n Massa<br>Reinsburgstra=DFe 202b<br>70197 Stuttgart<br>0173/9409607

------=_Part_3238_26198124.1120657950846--


Reply With Quote
  #3  
Old   
Neil Conway
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 12:56 AM



Denis Vlasenko wrote:
Quote:
Symptom: even the simplest query
$result = pg_query($db, "SELECT * FROM big_table");
eats enormous amounts of memory on server
(proportional to table size).
Right, which is exactly what you would expect. The entire result set is
sent to the client and stored in local memory; if you only want to
process part of the result set at a time, use a cursor.

(And I'm a little suspicious that the performance of "SELECT * FROM
big_table" will contribute to a meaningful comparison between database
systems.)

-Neil

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
  #4  
Old   
Neil Conway
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 02:33 AM



Denis Vlasenko wrote:
Quote:
The same php script but done against Oracle does not have this
behaviour.
Perhaps; presumably Oracle is essentially creating a cursor for you
behind the scenes. libpq does not attempt to do this automatically; if
you need a cursor, you can create one by hand.

-Neil

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match


Reply With Quote
  #5  
Old   
Denis Vlasenko
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 10:02 AM



On Wednesday 06 July 2005 16:52, Harald Armin Massa wrote:
Quote:
Denis,

$result = pg_query($db, "SELECT * FROM big_table");

you are reading a big result (as I suspect from big_table) into memory. It
is perfectly normal that this uses large amounts of memory.
No, I am not reading it into memory. I am executing query _on the server_,
fetching result row-by-row and discarding rows as prey are processed
(i.e. without accumulating all rows in _client's memory_) in the part
of php script which you snipped off.

Similar construct with Oracle, with 10x larger table,
does not use Apache (php) memory significantly.

php's pg_query() calls PQuery(), a Postgresql client library function,
which is likely implemented so that it fetches all rows and stores them
in client's RAM before completion.

Oracle OCI8 does not work this way, it keeps result set
on db server (in a form of a cursor or something like that).

Quote:
[it would be rather suspicious if loading a big file / big resultset would
not use big amounts of memory]
--
vda


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


Reply With Quote
  #6  
Old   
Denis Vlasenko
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 10:02 AM



On Thursday 07 July 2005 08:54, Neil Conway wrote:
Quote:
Denis Vlasenko wrote:
Symptom: even the simplest query
$result = pg_query($db, "SELECT * FROM big_table");
eats enormous amounts of memory on server
(proportional to table size).

Right, which is exactly what you would expect. The entire result set is
sent to the client and stored in local memory; if you only want to
process part of the result set at a time, use a cursor.
The same php script but done against Oracle does not have this
behaviour.

Quote:
(And I'm a little suspicious that the performance of "SELECT * FROM
big_table" will contribute to a meaningful comparison between database
systems.)
I wanted to show colleagues which are Oracle admins that peak
data fetch rate of PostgreSQL is way better than Oracle one.

While it turned out to be true (Oracle+WinNT = 2kb TCP output buffer,
~1Mb/s over 100Mbit; PostgreSQL+Linux = 8kb buffer, ~2.6Mb/s),
I was ridiculed instead when my php script failed miserably,
crashing Apache with OOM condition, while alanogous script for Oracle
ran to completion just fine.
--
vda


---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match


Reply With Quote
  #7  
Old   
Alvaro Herrera
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 10:11 AM



On Thu, Jul 07, 2005 at 09:51:54AM +0300, Denis Vlasenko wrote:

Quote:
I wanted to show colleagues which are Oracle admins that peak
data fetch rate of PostgreSQL is way better than Oracle one.

While it turned out to be true (Oracle+WinNT = 2kb TCP output buffer,
~1Mb/s over 100Mbit; PostgreSQL+Linux = 8kb buffer, ~2.6Mb/s),
I was ridiculed instead when my php script failed miserably,
crashing Apache with OOM condition, while alanogous script for Oracle
ran to completion just fine.
You should have tested the script before showing off :-) You may want
to convert it to manually use a cursor, at least the Postgres version.
That would alleviate the memory problem.

--
Alvaro Herrera (<alvherre[a]alvh.no-ip.org>)
"Some men are heterosexual, and some are bisexual, and some
men don't think about sex at all... they become lawyers" (Woody Allen)

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq


Reply With Quote
  #8  
Old   
John R Pierce
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 10:19 AM



Neil Conway wrote:
Quote:
Denis Vlasenko wrote:

The same php script but done against Oracle does not have this
behaviour.


Perhaps; presumably Oracle is essentially creating a cursor for you
behind the scenes. libpq does not attempt to do this automatically; if
you need a cursor, you can create one by hand.
I do not understand how a cursor could be autocreated by a query like

$result = pg_query($db, "SELECT * FROM big_table");

php will expect $result to contain the entire table (yuck!).



---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


Reply With Quote
  #9  
Old   
Alvaro Herrera
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-07-2005 , 12:46 PM



On Thu, Jul 07, 2005 at 08:17:23AM -0700, John R Pierce wrote:
Quote:
Neil Conway wrote:
Denis Vlasenko wrote:

The same php script but done against Oracle does not have this
behaviour.


Perhaps; presumably Oracle is essentially creating a cursor for you
behind the scenes. libpq does not attempt to do this automatically; if
you need a cursor, you can create one by hand.

I do not understand how a cursor could be autocreated by a query like

$result = pg_query($db, "SELECT * FROM big_table");

php will expect $result to contain the entire table (yuck!).
Really? I thought what really happened is you had to get the results
one at a time using the pg_fetch family of functions. If that is true,
then it's possible to make the driver fake having the whole table by
using a cursor. (Even if PHP doesn't do it, it's possible for OCI to do
it behind the scenes.)

--
Alvaro Herrera (<alvherre[a]alvh.no-ip.org>)
Officer Krupke, what are we to do?
Gee, officer Krupke, Krup you! (West Side Story, "Gee, Officer Krupke")

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match


Reply With Quote
  #10  
Old   
Volkan YAZICI
 
Posts: n/a

Default Re: [BUGS] BUG #1756: PQexec eats huge amounts of memory - 07-08-2005 , 01:36 AM



Hi,

A similar topic has been discussed before on pgsql-sql mailing list:
Subject: SELECT very slow Thomas Kellerer
URL: http://archives.postgresql.org/pgsql...6/msg00118.php

Regards.

On 7/6/05, Denis Vlasenko <vda (AT) ilport (DOT) com.ua> wrote:
Quote:
Bug reference: 1756
Logged by: Denis Vlasenko
Email address: vda (AT) ilport (DOT) com.ua
PostgreSQL version: 8.0.1
Operating system: Linux
Description: PQexec eats huge amounts of memory
---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.