Skip to content

Commit 44afde3

Browse files
committed
GH-15750 Pdo\Pgsql with ATTR_PREFETCH = 0: handle handle's internal queries
not only the statements, but the driver, love to make internal queries. We make sure no unfinished query still runs when having to pass an internal one.
1 parent 0d3829e commit 44afde3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

ext/pdo_pgsql/pgsql_driver.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "pgsql_driver_arginfo.h"
3737

3838
static bool pgsql_handle_in_transaction(pdo_dbh_t *dbh);
39+
void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode);
3940

4041
static char * _pdo_pgsql_trim_message(const char *message, int persistent)
4142
{
@@ -103,6 +104,13 @@ int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *
103104
}
104105
/* }}} */
105106

107+
static zend_always_inline void pgsql_finish_running_stmt(pdo_pgsql_db_handle *H)
108+
{
109+
if (H->running_stmt && H->running_stmt->is_running_unbuffered) {
110+
pgsql_stmt_finish(H->running_stmt, 0);
111+
}
112+
}
113+
106114
static void _pdo_pgsql_notice(void *context, const char *message) /* {{{ */
107115
{
108116
pdo_dbh_t * dbh = (pdo_dbh_t *)context;
@@ -348,6 +356,7 @@ static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
348356

349357
bool in_trans = pgsql_handle_in_transaction(dbh);
350358

359+
pgsql_finish_running_stmt(H);
351360
if (!(res = PQexec(H->server, ZSTR_VAL(sql)))) {
352361
/* fatal error */
353362
pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
@@ -415,6 +424,7 @@ static zend_string *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const zend_string *
415424
PGresult *res;
416425
ExecStatusType status;
417426

427+
pgsql_finish_running_stmt(H);
418428
if (name == NULL) {
419429
res = PQexec(H->server, "SELECT LASTVAL()");
420430
} else {
@@ -578,6 +588,7 @@ static bool pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
578588
PGresult *res;
579589
bool ret = true;
580590

591+
pgsql_finish_running_stmt(H);
581592
res = PQexec(H->server, cmd);
582593

583594
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
@@ -686,6 +697,7 @@ void pgsqlCopyFromArray_internal(INTERNAL_FUNCTION_PARAMETERS)
686697
while ((pgsql_result = PQgetResult(H->server))) {
687698
PQclear(pgsql_result);
688699
}
700+
pgsql_finish_running_stmt(H); /* Maybe in the future combine with the getResult loop in a pgsql_consume_running_stmt */
689701
pgsql_result = PQexec(H->server, query);
690702

691703
efree(query);
@@ -810,6 +822,7 @@ void pgsqlCopyFromFile_internal(INTERNAL_FUNCTION_PARAMETERS)
810822
while ((pgsql_result = PQgetResult(H->server))) {
811823
PQclear(pgsql_result);
812824
}
825+
pgsql_finish_running_stmt(H); /* Maybe in the future combine with the getResult loop in a pgsql_consume_running_stmt */
813826
pgsql_result = PQexec(H->server, query);
814827

815828
efree(query);
@@ -906,6 +919,7 @@ void pgsqlCopyToFile_internal(INTERNAL_FUNCTION_PARAMETERS)
906919
while ((pgsql_result = PQgetResult(H->server))) {
907920
PQclear(pgsql_result);
908921
}
922+
pgsql_finish_running_stmt(H); /* Maybe in the future combine with the getResult loop in a pgsql_consume_running_stmt */
909923

910924
/* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
911925
if (pg_fields) {
@@ -997,6 +1011,7 @@ void pgsqlCopyToArray_internal(INTERNAL_FUNCTION_PARAMETERS)
9971011
while ((pgsql_result = PQgetResult(H->server))) {
9981012
PQclear(pgsql_result);
9991013
}
1014+
pgsql_finish_running_stmt(H); /* Maybe in the future combine with the getResult loop in a pgsql_consume_running_stmt */
10001015

10011016
/* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
10021017
if (pg_fields) {

ext/pdo_pgsql/pgsql_statement.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262

6363

6464

65-
static void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)
65+
void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)
6666
{
6767
pdo_pgsql_db_handle *H = S->H;
6868

0 commit comments

Comments
 (0)