2 32.21. Behavior in Threaded Programs #
4 As of version 17, libpq is always reentrant and thread-safe. However,
5 one restriction is that no two threads attempt to manipulate the same
6 PGconn object at the same time. In particular, you cannot issue
7 concurrent commands from different threads through the same connection
8 object. (If you need to run concurrent commands, use multiple
11 PGresult objects are normally read-only after creation, and so can be
12 passed around freely between threads. However, if you use any of the
13 PGresult-modifying functions described in Section 32.12 or
14 Section 32.14, it's up to you to avoid concurrent operations on the
17 In earlier versions, libpq could be compiled with or without thread
18 support, depending on compiler options. This function allows the
19 querying of libpq's thread-safe status:
22 Returns the thread safety status of the libpq library.
26 Returns 1 if the libpq is thread-safe and 0 if it is not. Always
27 returns 1 on version 17 and above.
29 The deprecated functions PQrequestCancel and PQoidStatus are not
30 thread-safe and should not be used in multithread programs.
31 PQrequestCancel can be replaced by PQcancelBlocking. PQoidStatus can be
32 replaced by PQoidValue.
34 If you are using Kerberos inside your application (in addition to
35 inside libpq), you will need to do locking around Kerberos calls
36 because Kerberos functions are not thread-safe. See function
37 PQregisterThreadLock in the libpq source code for a way to do
38 cooperative locking between libpq and your application.
40 Similarly, if you are using Curl inside your application, and you do
41 not already initialize libcurl globally before starting new threads,
42 you will need to cooperatively lock (again via PQregisterThreadLock)
43 around any code that may initialize libcurl. This restriction is lifted
44 for more recent versions of Curl that are built to support thread-safe
45 initialization; those builds can be identified by the advertisement of
46 a threadsafe feature in their version metadata.