Skip site navigation (1) Skip section navigation (2)

PHP and COPY query

From: Volkan YAZICI <volkan(dot)yazici(at)gmail(dot)com>
To: pgsql-php(at)postgresql(dot)org
Subject: PHP and COPY query
Date: 2005-04-18 19:47:04
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-php
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 = pg_connect("user=knt dbname=template1")
            or die("Connection failed!\nRelated error message: ".

    $res = 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) != PGSQL_COPY_IN )
        die("An unexpected result occured!\n");

/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 <libpq-fe.h>
#include <stdlib.h>
#include <libpq-fe.h>

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

    conn = PQconnectdb("dbname=template1");
    if ( PQstatus(conn) != CONNECTION_OK ) {
        fprintf(stderr, "Connection failed!\n");
        fprintf(stderr, "Related error message: %s",
    res = PQexec(conn, "COPY ornektablo FROM stdin");
    if ( PQresultStatus(res) != PGRES_COPY_IN ) {
        fprintf(stderr, "An unexpected result occured!\n");

    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.

pgsql-php by date

Next:From: Joshua D. DrakeDate: 2005-04-19 02:16:01
Subject: Re: PostgreSQL and SSL and PHP
Previous:From: Frank L. ParksDate: 2005-04-13 22:07:19
Subject: PostgreSQL and SSL and PHP

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group