pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: expl0it3r@my-gate.net
To: pgsql-bugs@lists.postgresql.org
Subject: Static libpq and ecpglib clash with PThreads4W on Windows
Date: Sun, 11 Oct 2026 18:44:05 +0200
Message-ID: <a004d8e3d6d95a560d402afeada99510@my-gate.net> (raw)

Hi,

Linking the static libpq on Windows into an application that also uses a 
"real" pthreads library like PThreads4W fails with duplicate symbols:

   pthreadVC3d.lib(pthread.o) : error LNK2005: pthread_self already 
defined in pq.lib(pthread-win32.c.obj)
   pthreadVC3d.lib(pthread.o) : error LNK2005: pthread_mutex_init already 
defined in pq.lib(pthread-win32.c.obj)
   ...

libpq and ecpglib each bring a small pthread emulation on Windows 
(src/port/pthread-win32.h with src/interfaces/libpq/pthread-win32.c, and 
ecpg-pthread-win32.h with the definitions in ecpglib/misc.c), and the 
functions are named exactly like the official pthread ones. The static 
PThreads4W library is a single object file, so there's no way around it 
on the application side.

Depending on the link order, the two emulations also run into each 
other. On master, the following program fails to link against the static 
libecpg.a, libpq.a and PThreads4W:

   #include <stdio.h>
   #include <pthread.h>
   #include <libpq-fe.h>

   extern void *ECPGget_sqlca(void);

   int main(void)
   {
       PGconn *conn = PQconnectdb("host=localhost port=1 
connect_timeout=1");
       printf("%p %p\n", ECPGget_sqlca(), (void *) pthread_self().p);
       PQfinish(conn);
       return 0;
   }

   libpq.a(pthread-win32.c.obj) : error LNK2005: pthread_mutex_init 
already defined in libecpg.a(misc.c.obj)
   libpq.a(pthread-win32.c.obj) : error LNK2005: pthread_mutex_lock 
already defined in libecpg.a(misc.c.obj)
   libpq.a(pthread-win32.c.obj) : error LNK2005: pthread_mutex_unlock 
already defined in libecpg.a(misc.c.obj)

I ran into this through vcpkg, where pdal on the static Windows triplets 
pulls in libpq together with mbedtls built with pthreads. I've patched 
the vcpkg version, so the static Windows triplets no longer fail on CI 
[1].

The attached patch renames the functions with macros in the two 
emulation headers to pq_pthread_* and ecpg_pthread_*. The headers are 
included by the definitions and by every caller, and none of the 
functions are in exports.txt, so the call sites and the DLL ABI stay the 
same. The "Cleaning up threading code" thread [2] discussed essentially 
the same fix for the emulations, but it seems that it hasn't been 
implemented yet.

Tested with MSVC 2026 and meson on Windows: a full build, the ecpg suite 
(66 subtests including the thread tests), and the program above linking 
and running against the static libpq, ecpglib and PThreads4W, which 
fails on master.

[1] https://github.com/microsoft/vcpkg/pull/54377
[2] 
https://postgr.es/m/CA+hUKG+XpCcqoNe3-3oUzuGUeRoxAKqGnEAedvOhotsNCxuS+Q@mail.gmail.com

Kind regards,
Lukas

Attachments:

v1-0001-Rename-the-Windows-pthread-emulation-functions-in.patch (text/x-diff, 2.6K) download From 9a6ba833a853efbae2ec0b6e37ede4f359450132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= <eXpl0it3r@my-gate.net> Date: Sun, 11 Oct 2026 12:29:44 +0200 Subject: [PATCH v1] Rename the Windows pthread emulation functions in libpq and ecpglib libpq and ecpglib each come with a small pthread emulation on Windows, whose functions have the same names as the real ones, e.g. pthread_mutex_lock. When an application links the static libraries and also uses a real pthreads library like PThreads4W, the linker finds the same symbols twice and fails with LNK2005. Depending on the link order, libpq's and ecpglib's emulations also end up clashing with each other. Rename the functions with macros in the two emulation headers, which are included by the definitions and by every caller. None of the functions are exported from the DLLs, so the ABI stays the same. --- src/interfaces/ecpg/include/ecpg-pthread-win32.h | 9 +++++++++ src/port/pthread-win32.h | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/interfaces/ecpg/include/ecpg-pthread-win32.h b/src/interfaces/ecpg/include/ecpg-pthread-win32.h index 7b6ba46..c7649f8 100644 --- a/src/interfaces/ecpg/include/ecpg-pthread-win32.h +++ b/src/interfaces/ecpg/include/ecpg-pthread-win32.h @@ -23,6 +23,15 @@ typedef bool pthread_once_t; #define PTHREAD_MUTEX_INITIALIZER { 0 } #define PTHREAD_ONCE_INIT false +/* + * Rename the emulation functions, so they don't clash with libpq's or with a + * real pthreads library, like PThreads4W, that ends up in the same static + * link. + */ +#define pthread_mutex_init ecpg_pthread_mutex_init +#define pthread_mutex_lock ecpg_pthread_mutex_lock +#define pthread_mutex_unlock ecpg_pthread_mutex_unlock + int pthread_mutex_init(pthread_mutex_t *, void *attr); int pthread_mutex_lock(pthread_mutex_t *); int pthread_mutex_unlock(pthread_mutex_t *); diff --git a/src/port/pthread-win32.h b/src/port/pthread-win32.h index 5f33269..96648e5 100644 --- a/src/port/pthread-win32.h +++ b/src/port/pthread-win32.h @@ -4,6 +4,17 @@ #ifndef __PTHREAD_H #define __PTHREAD_H +/* + * Rename the emulation functions, so they don't clash with a real pthreads + * library on Windows, like PThreads4W, that ends up in the same static link. + */ +#define pthread_self pq_pthread_self +#define pthread_setspecific pq_pthread_setspecific +#define pthread_getspecific pq_pthread_getspecific +#define pthread_mutex_init pq_pthread_mutex_init +#define pthread_mutex_lock pq_pthread_mutex_lock +#define pthread_mutex_unlock pq_pthread_mutex_unlock + typedef ULONG pthread_key_t; typedef struct pthread_mutex_t -- 2.55.0.windows.5

Message-ID: <a004d8e3d6d95a560d402afeada99510@my-gate.net>
Permalink:  ../a004d8e3d6d95a560d402afeada99510@my-gate.net/
Also on:    postgresql.org/message-id/a004d8e3d6d95a560d402afeada99510@my-gate.net

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: expl0it3r@my-gate.net
  Subject: Re: Static libpq and ecpglib clash with PThreads4W on Windows
  In-Reply-To: <a004d8e3d6d95a560d402afeada99510@my-gate.net>
  References: <a004d8e3d6d95a560d402afeada99510@my-gate.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox