Edit report at https://bugs.php.net/bug.php?id=65015&edit=1

 ID:                 65015
 User updated by:    adam at vektah dot net
 Reported by:        adam at vektah dot net
-Summary:            pg_send_query
+Summary:            pg_send_query does not flush send buffer
 Status:             Open
 Type:               Bug
 Package:            PostgreSQL related
 Operating System:   Linux
 PHP Version:        5.4.16
 Block user comment: N
 Private report:     N

 New Comment:

Updated summary.


Previous Comments:
------------------------------------------------------------------------
[2013-06-12 03:05:50] 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



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65015&edit=1

Reply via email to