Skip to content

Mixup with prepared statement handles #183

@pepl

Description

@pepl

With the upgrade to the latest release, we've been running in troubles with server side prepared statements and async usage: Statement handles got mixed up in the same processes and errors looked like this one

DBD::Pg::st execute failed: ERROR:  prepared statement "dbdpg_p239066_1" already exists

or mismatch of bind variables count.

Two work-arounds have been
(1) Disable pg_server_prepare
(2) Do not use prepare_cached (in a setup where async and non-async queries are mixed)

The issue seems to be related to prepare_number not being incremented with 48bf171 in the async case which this patch does address:

host:dbdpg✔ [master]$ git diff dbdimp.c
diff --git a/dbdimp.c b/dbdimp.c
index cd6e093..4d42032 100644
--- a/dbdimp.c
+++ b/dbdimp.c
@@ -2552,9 +2552,11 @@ static int pg_st_prepare_statement (pTHX_ SV * sth, imp_sth_t * imp_sth)
         TRACE_PQSENDPREPARE;
         status = PQsendPrepare(imp_dbh->conn, imp_sth->prepare_name, statement, params,
                                imp_sth->PQoids);
-        if (status)
+        if (status) {
             imp_sth->async_status = STH_ASYNC_PREPARE;
-        else {
+            imp_sth->prepared_by_us = DBDPG_TRUE;
+            imp_dbh->prepare_number++;
+        } else {
             status = PGRES_FATAL_ERROR;
             _fatal_sqlstate(aTHX_ imp_dbh);
         }

As I am neither an expert in dbdimp.c nor could I reproduce this with updating 08async.t (maybe related to our local PgBouncer usage?), I am opening this a bug ticket instead of PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions