Skip to content

Commit e8b987d

Browse files
committed
Fix GH-9344: pgsql pipeline mode proposal.
Fix freeze pg_query/pg_query_params/pg_prepare/pg_execute
1 parent c3d42bc commit e8b987d

File tree

2 files changed

+83
-40
lines changed

2 files changed

+83
-40
lines changed

ext/pgsql/pgsql.c

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,17 +1020,23 @@ PHP_FUNCTION(pg_query)
10201020

10211021
pgsql = link->conn;
10221022

1023-
if (PQsetnonblocking(pgsql, 0)) {
1024-
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1025-
RETURN_FALSE;
1026-
}
1027-
while ((pgsql_result = PQgetResult(pgsql))) {
1028-
PQclear(pgsql_result);
1029-
leftover = 1;
1030-
}
1031-
if (leftover) {
1032-
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1023+
#ifdef LIBPQ_HAS_PIPELINING
1024+
if (PQpipelineStatus(pgsql) == PQ_PIPELINE_OFF) {
1025+
#endif
1026+
if (PQsetnonblocking(pgsql, 0)) {
1027+
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1028+
RETURN_FALSE;
1029+
}
1030+
while ((pgsql_result = PQgetResult(pgsql))) {
1031+
PQclear(pgsql_result);
1032+
leftover = 1;
1033+
}
1034+
if (leftover) {
1035+
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1036+
}
1037+
#ifdef LIBPQ_HAS_PIPELINING
10331038
}
1039+
#endif
10341040
pgsql_result = PQexec(pgsql, query);
10351041
if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
10361042
PQclear(pgsql_result);
@@ -1114,17 +1120,23 @@ PHP_FUNCTION(pg_query_params)
11141120

11151121
pgsql = link->conn;
11161122

1117-
if (PQsetnonblocking(pgsql, 0)) {
1118-
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1119-
RETURN_FALSE;
1120-
}
1121-
while ((pgsql_result = PQgetResult(pgsql))) {
1122-
PQclear(pgsql_result);
1123-
leftover = 1;
1124-
}
1125-
if (leftover) {
1126-
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1123+
#ifdef LIBPQ_HAS_PIPELINING
1124+
if (PQpipelineStatus(pgsql) == PQ_PIPELINE_OFF) {
1125+
#endif
1126+
if (PQsetnonblocking(pgsql, 0)) {
1127+
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1128+
RETURN_FALSE;
1129+
}
1130+
while ((pgsql_result = PQgetResult(pgsql))) {
1131+
PQclear(pgsql_result);
1132+
leftover = 1;
1133+
}
1134+
if (leftover) {
1135+
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1136+
}
1137+
#ifdef LIBPQ_HAS_PIPELINING
11271138
}
1139+
#endif
11281140

11291141
num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
11301142
if (num_params > 0) {
@@ -1219,17 +1231,23 @@ PHP_FUNCTION(pg_prepare)
12191231

12201232
pgsql = link->conn;
12211233

1222-
if (PQsetnonblocking(pgsql, 0)) {
1223-
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1224-
RETURN_FALSE;
1225-
}
1226-
while ((pgsql_result = PQgetResult(pgsql))) {
1227-
PQclear(pgsql_result);
1228-
leftover = 1;
1229-
}
1230-
if (leftover) {
1231-
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1234+
#ifdef LIBPQ_HAS_PIPELINING
1235+
if (PQpipelineStatus(pgsql) == PQ_PIPELINE_OFF) {
1236+
#endif
1237+
if (PQsetnonblocking(pgsql, 0)) {
1238+
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1239+
RETURN_FALSE;
1240+
}
1241+
while ((pgsql_result = PQgetResult(pgsql))) {
1242+
PQclear(pgsql_result);
1243+
leftover = 1;
1244+
}
1245+
if (leftover) {
1246+
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1247+
}
1248+
#ifdef LIBPQ_HAS_PIPELINING
12321249
}
1250+
#endif
12331251
pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
12341252
if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
12351253
PQclear(pgsql_result);
@@ -1300,17 +1318,23 @@ PHP_FUNCTION(pg_execute)
13001318

13011319
pgsql = link->conn;
13021320

1303-
if (PQsetnonblocking(pgsql, 0)) {
1304-
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1305-
RETURN_FALSE;
1306-
}
1307-
while ((pgsql_result = PQgetResult(pgsql))) {
1308-
PQclear(pgsql_result);
1309-
leftover = 1;
1310-
}
1311-
if (leftover) {
1312-
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1321+
#ifdef LIBPQ_HAS_PIPELINING
1322+
if (PQpipelineStatus(pgsql) == PQ_PIPELINE_OFF) {
1323+
#endif
1324+
if (PQsetnonblocking(pgsql, 0)) {
1325+
php_error_docref(NULL, E_NOTICE,"Cannot set connection to blocking mode");
1326+
RETURN_FALSE;
1327+
}
1328+
while ((pgsql_result = PQgetResult(pgsql))) {
1329+
PQclear(pgsql_result);
1330+
leftover = 1;
1331+
}
1332+
if (leftover) {
1333+
php_error_docref(NULL, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
1334+
}
1335+
#ifdef LIBPQ_HAS_PIPELINING
13131336
}
1337+
#endif
13141338

13151339
num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
13161340
if (num_params > 0) {

ext/pgsql/tests/pg_pipeline_sync.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,25 @@ if (pg_get_result($db) !== false) {
248248

249249
pg_close($db);
250250

251+
if (!$db = pg_connect($conn_str)) {
252+
die("pg_connect() error");
253+
}
254+
255+
if (!pg_enter_pipeline_mode($db)) {
256+
die('pg_enter_pipeline_mode');
257+
}
258+
259+
if (!pg_send_query_params($db, "select $1 as index, now() + ($1||' day')::interval as time", array(199))) {
260+
die('pg_send_query_params failed');
261+
}
262+
263+
@pg_query_params($db, "select $1 as index, now() + ($1||' day')::interval as time", array(200));
264+
if (strpos(pg_last_error($db), 'synchronous command execution functions are not allowed in pipeline mode') === false) {
265+
die('pg_query_params failed');
266+
}
267+
268+
pg_close($db);
269+
251270
echo "OK";
252271

253272
?>

0 commit comments

Comments
 (0)