php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52750 pg_get_result does not block if an error occurred
Submitted: 2010-08-31 07:59 UTC Modified: 2012-03-31 06:19 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: levi at alliancesoftware dot com dot au Assigned:
Status: Not a bug Package: PostgreSQL related
PHP Version: 5.3.3 OS: Linux (FC10)
Private report: No CVE-ID: None
 [2010-08-31 07:59 UTC] levi at alliancesoftware dot com dot au
Description:
------------
Continuation of http://bugs.php.net/bug.php?id=36469 -- it won't let me reopen the bug (too old?) but it still exists. (It seems to be worse on modern hardware since there are multiple CPUs and the php process never has to yield to the postgres process)

The manual says that pg_get_result() will block until results are ready, but if there was any sort of error (syntax error or query error, eg table does not exist) then pg_get_result() will return immediately.

This causes problems since the next command you try to run may fail because the previous command hasn't actually finished. 

Test script:
---------------
#!/usr/local/src/php5/php-5.3.3/sapi/cli/php
<?php

if (!isset($_SERVER['argv'][1])) {
    echo "Missing sleep time\n";
    exit(1);
}
$sleepTime = $_SERVER['argv'][1];

$connStr = sprintf("host=%s dbname=%s user=%s password=%s", 'localhost', 'testlevi', 'levi', 'levi');
($dbconn = pg_connect($connStr)) or die("Could not connect");

$i = 0;

do {
    pg_send_query($dbconn, 'i am a syntax error;');
    pg_get_result($dbconn);
    usleep($sleepTime);
    $i++;
} while (!pg_connection_busy($dbconn));

echo "got a busy signal after $i commands";

?>


(There are 2 alternate scripts in the previous bug report that effectively show the same thing)

Expected result:
----------------
Should loop forever

Actual result:
--------------
./test.php 0
got a busy signal after 1 commands

./test.php 10
got a busy signal after 3033 commands

./test.php 1000
(loops forever)



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-31 06:19 UTC] yohgaki@php.net
-Status: Open +Status: Not a bug
 [2012-03-31 06:19 UTC] yohgaki@php.net
This isn't a bug.

The reason why pg_connection_busy() returns false is PostgreSQL actually finished 
query during usleep(). If query is not executing, pg_connection_busy() returns 
false.

Therefore, this is expected behavior.
 [2012-03-31 11:43 UTC] levi at alliancesoftware dot com dot au
The manual page for pg_connection_busy() states: "If pg_get_result() is used on 
the connection, it will be blocked."


How can the query finish during the usleep() if pg_get_result() blocked while 
the query was still executing? It should 
have been finished already.


If this is in fact "expected behavior" then the manual is wrong and should be 
changed to reflect what actually happens.


Demonstrating the same bug with no usleep():


#!/usr/local/src/php5/php-5.4.0/sapi/cli/php
<?php

$connStr = sprintf("host=%s dbname=%s user=%s password=%s", 'localhost', 
'testlevi', 'levi', 'levi');
($dbconn = pg_connect($connStr)) or die("Could not connect");

pg_send_query($dbconn, 'i am a syntax error;');
pg_get_result($dbconn);
if (pg_connection_busy($dbconn)) {
    echo "This should be impossible\n";
}

?>


100% of the time this prints "This should be impossible". PHP 5.4, PGSQL 8.3
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC