Skip to content

Commit

Permalink
GH-12940 ext/pdo_pgsql: using PQclosePrepared to free statement resou…
Browse files Browse the repository at this point in the history
…rces.

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.
  • Loading branch information
devnexen committed Apr 1, 2024
1 parent 0dc5998 commit 70e152d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions ext/pdo_pgsql/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ if test "$PHP_PDO_PGSQL" != "no"; then

AC_CHECK_LIB(pq, PQlibVersion,, AC_MSG_ERROR([Unable to build the PDO PostgreSQL driver: at least libpq 9.1 is required]))

PHP_CHECK_FUNC(PQclosePrepared, pq)

LIBS=$old_LIBS
LDFLAGS=$old_LDFLAGS

Expand Down
15 changes: 12 additions & 3 deletions ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
if (S->stmt_name) {
if (S->is_prepared && server_obj_usable) {
pdo_pgsql_db_handle *H = S->H;
char *q = NULL;
PGresult *res;

#ifndef HAVE_PQCLOSEPREPARED
// TODO (??) libpq does not support close statement protocol < postgres 17
// check if we can circumvent this.
char *q = NULL;
spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
res = PQexec(H->server, q);
efree(q);
#else
res = PQclosePrepared(H->server, S->stmt_name);
#endif
if (res) {
PQclear(res);
}
Expand Down Expand Up @@ -203,10 +208,14 @@ static int pgsql_stmt_execute(pdo_stmt_t *stmt)
* deallocate it and retry ONCE (thies 2005.12.15)
*/
if (sqlstate && !strcmp(sqlstate, "42P05")) {
char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
PGresult *res;
#ifndef HAVE_PQCLOSEPREPARED
char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
res = PQexec(H->server, buf);
#else
res = PQclosePrepared(H->server, S->stmt_name);
#endif
if (res) {
PQclear(res);
}
Expand Down

0 comments on commit 70e152d

Please sign in to comment.