PHP and COPY query

PHP and COPY query

am 18.04.2005 21:47:04 von Volkan YAZICI

Hi all,

Could anybody help me to figure out the reason of the endless loop
after pg_query() line:

/tmp$ cat example-copy.php

$conn =3D pg_connect("user=3Dknt dbname=3Dtemplate1")
or die("Connection failed!\nRelated error message: ".
pg_last_error($conn));

$res =3D pg_query($conn, "COPY ornektablo FROM stdin");

/*
* If a pg_end_copy() call won't make in here, it will stuck to this
* line of the code with an endless loop (and a 100% CPU usage in
* the proccess side).
*/

if ( pg_result_status($res) !=3D PGSQL_COPY_IN )
die("An unexpected result occured!\n");

pg_close($conn);
?>
/tmp$ php example-copy.php
# Waiting and waiting `n waiting...
# (CPU Usage: 99%-100%)

The same program written using libpq works without any problem:

/tmp$ cat example-copy.c
#include
#include
#include

int main(void)
{
PGconn *conn;
PGresult *res;

conn =3D PQconnectdb("dbname=3Dtemplate1");
if ( PQstatus(conn) !=3D CONNECTION_OK ) {
fprintf(stderr, "Connection failed!\n");
fprintf(stderr, "Related error message: %s",
PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}

res =3D PQexec(conn, "COPY ornektablo FROM stdin");
if ( PQresultStatus(res) !=3D PGRES_COPY_IN ) {
fprintf(stderr, "An unexpected result occured!\n");
PQclear(res);
PQfinish(conn);
exit(1);
}

PQfinish(conn);
return 0;
}
/tmp$ gcc -Wall -lpq example-copy.c && ./a.out
/tmp$ _
# Program ends normally.

It seems like a PHP bug related with the handling of stdin between
PostgreSQL and PHP. (I executed above PHP script from the command
line, but the result is same when you'd use a web server too.) When I
look at the source code of pg_query() function, it just makes a simple
call to PQexec() after controlling taken input. Also, that encouraged
me to think about stdin handling once more.

Any comments are welcome.
Regards.

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

http://archives.postgresql.org