pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Volkan YAZICI <volkan.yazici@gmail.com>
To: pgsql-php@postgresql.org
Subject: PHP and COPY query
Date: Mon, 18 Apr 2005 22:47:04 +0300
Message-ID: <7104a737050418124778333f3b@mail.gmail.com> (raw)

Hi all,

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

/tmp$ cat example-copy.php
<?php
    
    $conn = pg_connect("user=knt dbname=template1")
            or die("Connection failed!\nRelated error message: ".
            pg_last_error($conn));

    $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");

    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 <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",
                PQerrorMessage(conn));
        PQfinish(conn);
        exit(1);
    }
    
    res = PQexec(conn, "COPY ornektablo FROM stdin");
    if ( PQresultStatus(res) != 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.



Message-ID: <7104a737050418124778333f3b@mail.gmail.com>
Permalink:  ../7104a737050418124778333f3b@mail.gmail.com/
Also on:    postgresql.org/message-id/7104a737050418124778333f3b@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: volkan.yazici@gmail.com
  Subject: Re: PHP and COPY query
  In-Reply-To: <7104a737050418124778333f3b@mail.gmail.com>
  References: <7104a737050418124778333f3b@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox