From 074645e5b414e2f095b5703e5ce2a45006c13d81 Mon Sep 17 00:00:00 2001 From: Jacob Champion Date: Thu, 19 Feb 2026 12:39:16 -0800 Subject: [PATCH v3 2/6] libpq: Add PQgetThreadLock() to mirror PQregisterThreadLock() Allow libpq clients to retrieve the current pg_g_threadlock pointer with PQgetThreadLock(). Single-threaded applications could already do this in a convoluted way: pgthreadlock_t tlock; tlock = PQregisterThreadLock(NULL); PQregisterThreadLock(tlock); /* re-register the callback */ /* use tlock */ But a generic library can't do that without potentially breaking concurrent libpq connections. The motivation for doing this now is the libpq-oauth plugin, which currently relies on direct injection of pg_g_threadlock, and should ideally not. --- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/libpq-fe.h | 10 ++++++++-- src/interfaces/libpq/fe-connect.c | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index dbbae642d76..1e3d5bd5867 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -210,3 +210,4 @@ PQgetAuthDataHook 207 PQdefaultAuthDataHook 208 PQfullProtocolVersion 209 appendPQExpBufferVA 210 +PQgetThreadLock 211 diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 905f2f33ab8..29b16efbb1d 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -63,6 +63,10 @@ extern "C" /* Indicates presence of the PQAUTHDATA_PROMPT_OAUTH_DEVICE authdata hook */ #define LIBPQ_HAS_PROMPT_OAUTH_DEVICE 1 +/* Features added in PostgreSQL v19: */ +/* Indicates presence of PQgetThreadLock */ +#define LIBPQ_HAS_GET_THREAD_LOCK + /* * Option flags for PQcopyResult */ @@ -462,12 +466,14 @@ extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, * Used to set callback that prevents concurrent access to * non-thread safe functions that libpq needs. * The default implementation uses a libpq internal mutex. - * Only required for multithreaded apps that use kerberos - * both within their app and for postgresql connections. + * Only required for multithreaded apps that use Kerberos or + * older (non-threadsafe) versions of Curl both within their + * app and for postgresql connections. */ typedef void (*pgthreadlock_t) (int acquire); extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler); +extern pgthreadlock_t PQgetThreadLock(void); /* === in fe-trace.c === */ extern void PQtrace(PGconn *conn, FILE *debug_port); diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index a0d2f749811..0c5f0d47b20 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -8381,3 +8381,10 @@ PQregisterThreadLock(pgthreadlock_t newhandler) return prev; } + +pgthreadlock_t +PQgetThreadLock(void) +{ + Assert(pg_g_threadlock); + return pg_g_threadlock; +} -- 2.34.1