From 7c718a5bb413ca082c34a48b83eef8780ff6b386 Mon Sep 17 00:00:00 2001 From: Kirill Reshke Date: Thu, 26 Nov 2026 11:50:19 +0500 Subject: [PATCH v2 1/2] Allow libpq to execute a prepared statement in a named portal Add PQsendQueryPreparedPortal() with an explicit destination for Bind, Describe and Execute. PQsendQueryPrepared() selects the unnamed portal as before. Existing callers keep their original function signatures. --- doc/src/sgml/libpq.sgml | 36 +++++++++++++++++++++++++++++-- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-exec.c | 37 +++++++++++++++++++++++++++----- src/interfaces/libpq/libpq-fe.h | 12 +++++++++++ 4 files changed, 79 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 3caaea91137..a7be0c89f4f 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -5437,6 +5437,36 @@ int PQsendQueryPrepared(PGconn *conn, + + PQsendQueryPreparedPortalPQsendQueryPreparedPortal + + + + Submits a request to bind a prepared statement to a portal and execute it. + +int PQsendQueryPreparedPortal(PGconn *conn, + const char *stmtName, + const char *portalName, + int nParams, + const char * const *paramValues, + const int *paramLengths, + const int *paramFormats, + int resultFormat); + + This has the same behavior and return value as + , with an additional + portalName argument. Supply an empty string to + use the unnamed portal. A null pointer is an error. + + + + A named portal remains available until the transaction ends or the + portal is closed (see ). + Binding to an existing named portal produces a server error. + + + + PQsendDescribePreparedPQsendDescribePrepared @@ -5527,6 +5557,7 @@ int PQsendClosePortal(PGconn *conn, const char *portalName); , , , + , , , , @@ -5898,8 +5929,9 @@ int PQflush(PGconn *conn); After entering pipeline mode, the application dispatches requests using - or its prepared-query sibling - . + or one of its prepared-query variants, + and + . These requests are queued on the client-side until flushed to the server; this occurs when is used to establish a synchronization point in the pipeline, diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 1e3d5bd5867..d6843b711a1 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -211,3 +211,4 @@ PQdefaultAuthDataHook 208 PQfullProtocolVersion 209 appendPQExpBufferVA 210 PQgetThreadLock 211 +PQsendQueryPreparedPortal 212 diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 294690648bd..da021791e59 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -69,6 +69,7 @@ static bool PQsendQueryStart(PGconn *conn, bool newQuery); static int PQsendQueryGuts(PGconn *conn, const char *command, const char *stmtName, + const char *portalName, int nParams, const Oid *paramTypes, const char *const *paramValues, @@ -1534,6 +1535,7 @@ PQsendQueryParams(PGconn *conn, return PQsendQueryGuts(conn, command, "", /* use unnamed statement */ + "", /* use unnamed portal */ nParams, paramTypes, paramValues, @@ -1654,6 +1656,24 @@ PQsendQueryPrepared(PGconn *conn, const int *paramLengths, const int *paramFormats, int resultFormat) +{ + return PQsendQueryPreparedPortal(conn, stmtName, "", nParams, + paramValues, paramLengths, paramFormats, + resultFormat); +} + +/* + * Bind a prepared statement to the specified portal and execute it. + */ +int +PQsendQueryPreparedPortal(PGconn *conn, + const char *stmtName, + const char *portalName, + int nParams, + const char *const *paramValues, + const int *paramLengths, + const int *paramFormats, + int resultFormat) { if (!PQsendQueryStart(conn, true)) return 0; @@ -1664,6 +1684,11 @@ PQsendQueryPrepared(PGconn *conn, libpq_append_conn_error(conn, "statement name is a null pointer"); return 0; } + if (!portalName) + { + libpq_append_conn_error(conn, "portal name is a null pointer"); + return 0; + } if (nParams < 0 || nParams > PQ_QUERY_PARAM_MAX_LIMIT) { libpq_append_conn_error(conn, "number of parameters must be between 0 and %d", @@ -1674,6 +1699,7 @@ PQsendQueryPrepared(PGconn *conn, return PQsendQueryGuts(conn, NULL, /* no command to parse */ stmtName, + portalName, nParams, NULL, /* no param types */ paramValues, @@ -1774,6 +1800,7 @@ static int PQsendQueryGuts(PGconn *conn, const char *command, const char *stmtName, + const char *portalName, int nParams, const Oid *paramTypes, const char *const *paramValues, @@ -1790,8 +1817,8 @@ PQsendQueryGuts(PGconn *conn, /* * We will send Parse (if needed), Bind, Describe Portal, Execute, Sync - * (if not in pipeline mode), using specified statement name and the - * unnamed portal. + * (if not in pipeline mode), using the specified statement and portal + * names. */ if (command) @@ -1822,7 +1849,7 @@ PQsendQueryGuts(PGconn *conn, /* Construct the Bind message */ if (pqPutMsgStart(PqMsg_Bind, conn) < 0 || - pqPuts("", conn) < 0 || + pqPuts(portalName, conn) < 0 || pqPuts(stmtName, conn) < 0) goto sendFailed; @@ -1889,13 +1916,13 @@ PQsendQueryGuts(PGconn *conn, /* construct the Describe Portal message */ if (pqPutMsgStart(PqMsg_Describe, conn) < 0 || pqPutc('P', conn) < 0 || - pqPuts("", conn) < 0 || + pqPuts(portalName, conn) < 0 || pqPutMsgEnd(conn) < 0) goto sendFailed; /* construct the Execute message */ if (pqPutMsgStart(PqMsg_Execute, conn) < 0 || - pqPuts("", conn) < 0 || + pqPuts(portalName, conn) < 0 || pqPutInt(0, 4, conn) < 0 || pqPutMsgEnd(conn) < 0) goto sendFailed; diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index f51fd620b0a..584c65ca1e3 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -69,6 +69,10 @@ extern "C" /* Indicates presence of the PQAUTHDATA_OAUTH_BEARER_TOKEN_V2 authdata hook */ #define LIBPQ_HAS_OAUTH_BEARER_TOKEN_V2 1 +/* Features added in PostgreSQL v20: */ +/* Indicates presence of PQsendQueryPreparedPortal */ +#define LIBPQ_HAS_SEND_QUERY_PREPARED_PORTAL 1 + /* * Option flags for PQcopyResult */ @@ -542,6 +546,14 @@ extern int PQsendQueryPrepared(PGconn *conn, const int *paramLengths, const int *paramFormats, int resultFormat); +extern int PQsendQueryPreparedPortal(PGconn *conn, + const char *stmtName, + const char *portalName, + int nParams, + const char *const *paramValues, + const int *paramLengths, + const int *paramFormats, + int resultFormat); extern int PQsetSingleRowMode(PGconn *conn); extern int PQsetChunkedRowsMode(PGconn *conn, int chunkSize); extern PGresult *PQgetResult(PGconn *conn); base-commit: b8bf28800559a9ad71360fb2384d4b9981d24fa8 -- 2.50.1 (Apple Git-155)