php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65015 pg_send_query does not flush send buffer
Submitted: 2013-06-12 03:05 UTC Modified: 2013-06-26 06:15 UTC
Votes:5
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:3 (60.0%)
Same OS:5 (100.0%)
From: adam at vektah dot net Assigned: yohgaki (profile)
Status: Closed Package: PostgreSQL related
PHP Version: 5.4.16 OS: Linux
Private report: No CVE-ID: None
 [2013-06-12 03:05 UTC] adam at vektah dot net
Description:
------------
When sending asynchronous postgres queries using pg_send_query() a notice will 
be raised for large queries:
PHP Notice:  pg_send_query(): Cannot set connection to blocking mode in 
longquery.php on line 6

This only seems to happen on databases NOT hosted on the local machine.

pg_send_query() roughly does:
 PQ_SETNONBLOCKING(1)
 PQsendQuery()
 PQ_SETNONBLOCKING(0)

PQ_SETNONBLOCKING will do this towards the end:
 if (pqFlush(conn))
  return -1;

and fail raising the notice if the connection still has data left in the send 
buffer.

http://www.postgresql.org/docs/8.3/static/libpq-async.html mentions that: 
After sending any command or data on a non-blocking connection, call PQflush. If 
it returns 1, wait for the socket to be write-ready and call it again; repeat 
until it returns 0. Once PQflush returns 0, wait for the socket to be read-ready 
and then read the response as described above.

The attached patch adds a loop that waits for the buffer to empty before calling 
PQ_SETNONBLOCKING(0), and raises a notice on any errors.

PHP Versions tested:
 - 5.4.9  (ubuntu package)
 - 5.4.13 (source)
 - 5.4.16 (source)

Postgres configurations tested:
 - 9.1.9 client with 2.2.4 server
 - 9.2.4 client and server

Operating systems tested:
 - Ubuntu 13.04
 - Centos 6

Test script:
---------------
$len = 100000;  // This may need to be increased, depending on db server.
$sql = "select 1" . str_repeat(' ', $len - 8);
$con = pg_connect('host=db-host.example.com dbname=postgres user=postgres password=password');

pg_send_query($con, $sql);
pg_get_result($con);


Expected result:
----------------
The query should run, no notices should be rasied and the connection should be 
put back into blocking mode again.

Actual result:
--------------
PHP Notice:  pg_send_query(): Cannot set connection to blocking mode in 
longquery.php on line 6
PHP Stack trace:
PHP   1. {main}()        longquery.php:0
PHP   2. pg_send_query() longquery.php:6


Patches

pg_send_query_flush_buffer.patch (last revision 2013-06-12 03:07 UTC by adam at vektah dot net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-06-25 06:32 UTC] adam at vektah dot net
-Summary: pg_send_query +Summary: pg_send_query does not flush send buffer
 [2013-06-25 06:32 UTC] adam at vektah dot net
Updated summary.
 [2013-06-26 06:15 UTC] yohgaki@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: yohgaki
 [2013-06-26 06:15 UTC] yohgaki@php.net
Thanks for reporting. I'll commit this fix.
 [2013-06-26 07:29 UTC] yohgaki@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6c8cef3ca49a11228d4dcd188170b943e03c3972
Log: Fixed bug #65015 (pg_send_query does not flush send buffer) patch submitted by: adam at vektah dot net
 [2013-06-26 07:29 UTC] yohgaki@php.net
-Status: Assigned +Status: Closed
 [2013-06-26 07:31 UTC] yohgaki@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6c8cef3ca49a11228d4dcd188170b943e03c3972
Log: Fixed bug #65015 (pg_send_query does not flush send buffer) patch submitted by: adam at vektah dot net
 [2013-06-26 14:51 UTC] andrey@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6c8cef3ca49a11228d4dcd188170b943e03c3972
Log: Fixed bug #65015 (pg_send_query does not flush send buffer) patch submitted by: adam at vektah dot net
 [2014-10-07 23:18 UTC] stas@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=6c8cef3ca49a11228d4dcd188170b943e03c3972
Log: Fixed bug #65015 (pg_send_query does not flush send buffer) patch submitted by: adam at vektah dot net
 [2014-10-07 23:29 UTC] stas@php.net
Automatic comment on behalf of yohgaki
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=6c8cef3ca49a11228d4dcd188170b943e03c3972
Log: Fixed bug #65015 (pg_send_query does not flush send buffer) patch submitted by: adam at vektah dot net
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Aug 15 18:01:26 2020 UTC