Skip to content

Commit f35ad56

Browse files
committed
GH-12940 ext/pdo_pgsql: using PQclosePrepared to free statement resources.
PQclosePrepared allows the statement's name to be reused thus allowing cache solutions to work properly ; whereas, for now, the `DEALLOCATE <statement>` query is used which free entirely the statement's resources. close GH-13316
1 parent 051c886 commit f35ad56

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ PHP NEWS
3737
- PDO_PGSQL:
3838
. Fixed GH-15986 (Double-free due to Pdo\Pgsql::setNoticeCallback()). (cmb,
3939
nielsdos)
40+
. Fixed GH-12940 (Using PQclosePrepared when available instead of
41+
the DEALLOCATE command to free statements resources). (David Carlier)
4042

4143
- Reflection:
4244
. Add missing ReflectionProperty::hasHook[s]() methods. (ilutov)

ext/pdo_pgsql/config.m4

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ if test "$PHP_PDO_PGSQL" != "no"; then
1919
or later).])],,
2020
[$PGSQL_LIBS])
2121

22+
PHP_CHECK_LIBRARY([pq], [PQclosePrepared],
23+
[AC_DEFINE([HAVE_PQCLOSEPREPARED], [1],
24+
[Define to 1 if libpq has the 'PQclosePrepared' function (PostgreSQL 17
25+
or later).])],,
26+
[$PGSQL_LIBS])
27+
2228
PHP_CHECK_PDO_INCLUDES
2329

2430
PHP_NEW_EXTENSION([pdo_pgsql],

ext/pdo_pgsql/pgsql_statement.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
7474
if (S->stmt_name) {
7575
if (S->is_prepared && server_obj_usable) {
7676
pdo_pgsql_db_handle *H = S->H;
77-
char *q = NULL;
7877
PGresult *res;
79-
78+
#ifndef HAVE_PQCLOSEPREPARED
79+
// TODO (??) libpq does not support close statement protocol < postgres 17
80+
// check if we can circumvent this.
81+
char *q = NULL;
8082
spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
8183
res = PQexec(H->server, q);
8284
efree(q);
85+
#else
86+
res = PQclosePrepared(H->server, S->stmt_name);
87+
#endif
8388
if (res) {
8489
PQclear(res);
8590
}
@@ -203,10 +208,14 @@ static int pgsql_stmt_execute(pdo_stmt_t *stmt)
203208
* deallocate it and retry ONCE (thies 2005.12.15)
204209
*/
205210
if (sqlstate && !strcmp(sqlstate, "42P05")) {
206-
char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
207211
PGresult *res;
212+
#ifndef HAVE_PQCLOSEPREPARED
213+
char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
208214
snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
209215
res = PQexec(H->server, buf);
216+
#else
217+
res = PQclosePrepared(H->server, S->stmt_name);
218+
#endif
210219
if (res) {
211220
PQclear(res);
212221
}

0 commit comments

Comments
 (0)