From: Jacob Champion Date: Thu, 5 Mar 2026 18:04:48 +0000 (-0800) Subject: libpq: Add PQgetThreadLock() to mirror PQregisterThreadLock() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8d76858353e40f888c809eb14a974cf13bf23ab;p=thirdparty%2Fpostgresql.git 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. Reviewed-by: Zsolt Parragi Discussion: https://postgr.es/m/CAOYmi%2BmEU_q9sr1PMmE-4rLwFN%3DOjyndDwFZvpsMU3RNJLrM9g%40mail.gmail.com Discussion: https://postgr.es/m/CAOYmi%2B%3DMHD%2BWKD4rsTn0v8220mYfyLGhEc5EfhmtqrAb7SmC5g%40mail.gmail.com --- diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index e08d46782cc..57349ef8f84 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -10635,7 +10635,11 @@ int PQisthreadsafe(); Kerberos calls because Kerberos functions are not thread-safe. See function PQregisterThreadLock in the libpq source code for a way to do cooperative - locking between libpq and your application. + locking between libpq and your application. (Note + that it is only safe to call PQregisterThreadLock when + there are no open connections.) Clients may retrieve the current locking + callback with PQgetThreadLock; if no custom callback + has been registered, a default is used. 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/fe-connect.c b/src/interfaces/libpq/fe-connect.c index b42a0cb4c78..db9b4c8edbf 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -8414,3 +8414,10 @@ PQregisterThreadLock(pgthreadlock_t newhandler) return prev; } + +pgthreadlock_t +PQgetThreadLock(void) +{ + Assert(pg_g_threadlock); + return pg_g_threadlock; +} diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 905f2f33ab8..1b46032b6f9 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 1 + /* * 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);