Edit report at https://bugs.php.net/bug.php?id=52750&edit=1
ID: 52750
Comment by: levi at alliancesoftware dot com dot au
Reported by: levi at alliancesoftware dot com dot au
Summary: pg_get_result does not block if an error occurred
Status: Not a bug
Type: Bug
Package: PostgreSQL related
Operating System: Linux (FC10)
PHP Version: 5.3.3
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2012-03-31 06:19:18] [email protected]
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.
------------------------------------------------------------------------
[2010-08-31 07:59:43] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=52750&edit=1