]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
libpq: Add PQgetThreadLock() to mirror PQregisterThreadLock()
authorJacob Champion <jchampion@postgresql.org>
Thu, 5 Mar 2026 18:04:48 +0000 (10:04 -0800)
committerJacob Champion <jchampion@postgresql.org>
Thu, 5 Mar 2026 18:04:48 +0000 (10:04 -0800)
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 <zsolt.parragi@percona.com>
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

doc/src/sgml/libpq.sgml
src/interfaces/libpq/exports.txt
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/libpq-fe.h

index e08d46782cc837542fe46b4b780d061a5c2d1407..57349ef8f8403ddda088bca8556fa1774031da6f 100644 (file)
@@ -10635,7 +10635,11 @@ int PQisthreadsafe();
    Kerberos calls because Kerberos functions are not thread-safe.  See
    function <function>PQregisterThreadLock</function> in the
    <application>libpq</application> source code for a way to do cooperative
-   locking between <application>libpq</application> and your application.
+   locking between <application>libpq</application> and your application. (Note
+   that it is only safe to call <function>PQregisterThreadLock</function> when
+   there are no open connections.) Clients may retrieve the current locking
+   callback with <function>PQgetThreadLock</function>; if no custom callback
+   has been registered, a default is used.
   </para>
 
   <para>
index dbbae642d769acfde14b41139415fbd6f87f8f25..1e3d5bd5867f58d7a9f743398b0d851fa63e37ac 100644 (file)
@@ -210,3 +210,4 @@ PQgetAuthDataHook         207
 PQdefaultAuthDataHook     208
 PQfullProtocolVersion     209
 appendPQExpBufferVA       210
+PQgetThreadLock           211
index b42a0cb4c781fd448b6f2339411880bcdf830a36..db9b4c8edbfe9dc51c065c9663d0d81af5bf3631 100644 (file)
@@ -8414,3 +8414,10 @@ PQregisterThreadLock(pgthreadlock_t newhandler)
 
        return prev;
 }
+
+pgthreadlock_t
+PQgetThreadLock(void)
+{
+       Assert(pg_g_threadlock);
+       return pg_g_threadlock;
+}
index 905f2f33ab8d6b808a5a8c794a4005b4c8f32ec6..1b46032b6f9553f86c8c5209d13e9d9c10bcce2d 100644 (file)
@@ -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);