]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/libpq-threading.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / libpq-threading.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>32.21. Behavior in Threaded Programs</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="libpq-oauth.html" title="32.20. OAuth Support" /><link rel="next" href="libpq-build.html" title="32.22. Building libpq Programs" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">32.21. Behavior in Threaded Programs</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-oauth.html" title="32.20. OAuth Support">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 32. libpq — C Library">Up</a></td><th width="60%" align="center">Chapter 32. <span class="application">libpq</span> — C Library</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="libpq-build.html" title="32.22. Building libpq Programs">Next</a></td></tr></table><hr /></div><div class="sect1" id="LIBPQ-THREADING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">32.21. Behavior in Threaded Programs <a href="#LIBPQ-THREADING" class="id_link">#</a></h2></div></div></div><a id="id-1.7.3.28.2" class="indexterm"></a><p>
3    As of version 17, <span class="application">libpq</span> is always reentrant and thread-safe.
4    However, one restriction is that no two threads attempt to manipulate
5    the same <code class="structname">PGconn</code> object at the same time. In particular,
6    you cannot issue concurrent commands from different threads through
7    the same connection object. (If you need to run concurrent commands,
8    use multiple connections.)
9   </p><p>
10    <code class="structname">PGresult</code> objects are normally read-only after creation,
11    and so can be passed around freely between threads.  However, if you use
12    any of the <code class="structname">PGresult</code>-modifying functions described in
13    <a class="xref" href="libpq-misc.html" title="32.12. Miscellaneous Functions">Section 32.12</a> or <a class="xref" href="libpq-events.html" title="32.14. Event System">Section 32.14</a>, it's up
14    to you to avoid concurrent operations on the same <code class="structname">PGresult</code>,
15    too.
16   </p><p>
17    In earlier versions, <span class="application">libpq</span> could be compiled
18    with or without thread support, depending on compiler options. This
19    function allows the querying of <span class="application">libpq</span>'s
20    thread-safe status:
21   </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQISTHREADSAFE"><span class="term"><code class="function">PQisthreadsafe</code><a id="id-1.7.3.28.6.1.1.2" class="indexterm"></a></span> <a href="#LIBPQ-PQISTHREADSAFE" class="id_link">#</a></dt><dd><p>
22       Returns the thread safety status of the
23       <span class="application">libpq</span> library.
24 </p><pre class="synopsis">
25 int PQisthreadsafe();
26 </pre><p>
27      </p><p>
28       Returns 1 if the <span class="application">libpq</span> is thread-safe
29       and 0 if it is not. Always returns 1 on version 17 and above.
30      </p></dd></dl></div><p>
31    The deprecated functions <a class="xref" href="libpq-cancel.html#LIBPQ-PQREQUESTCANCEL"><code class="function">PQrequestCancel</code></a> and
32    <a class="xref" href="libpq-exec.html#LIBPQ-PQOIDSTATUS"><code class="function">PQoidStatus</code></a> are not thread-safe and should not be
33    used in multithread programs.  <a class="xref" href="libpq-cancel.html#LIBPQ-PQREQUESTCANCEL"><code class="function">PQrequestCancel</code></a>
34    can be replaced by <a class="xref" href="libpq-cancel.html#LIBPQ-PQCANCELBLOCKING"><code class="function">PQcancelBlocking</code></a>.
35    <a class="xref" href="libpq-exec.html#LIBPQ-PQOIDSTATUS"><code class="function">PQoidStatus</code></a> can be replaced by
36    <a class="xref" href="libpq-exec.html#LIBPQ-PQOIDVALUE"><code class="function">PQoidValue</code></a>.
37   </p><p>
38    If you are using Kerberos inside your application (in addition to inside
39    <span class="application">libpq</span>), you will need to do locking around
40    Kerberos calls because Kerberos functions are not thread-safe.  See
41    function <code class="function">PQregisterThreadLock</code> in the
42    <span class="application">libpq</span> source code for a way to do cooperative
43    locking between <span class="application">libpq</span> and your application.
44   </p><p>
45    Similarly, if you are using <span class="productname">Curl</span> inside your application,
46    <span class="emphasis"><em>and</em></span> you do not already
47    <a class="ulink" href="https://curl.se/libcurl/c/curl_global_init.html" target="_top">initialize
48    libcurl globally</a> before starting new threads, you will need to
49    cooperatively lock (again via <code class="function">PQregisterThreadLock</code>)
50    around any code that may initialize libcurl. This restriction is lifted for
51    more recent versions of <span class="productname">Curl</span> that are built to support thread-safe
52    initialization; those builds can be identified by the advertisement of a
53    <code class="literal">threadsafe</code> feature in their version metadata.
54   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-oauth.html" title="32.20. OAuth Support">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html" title="Chapter 32. libpq — C Library">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-build.html" title="32.22. Building libpq Programs">Next</a></td></tr><tr><td width="40%" align="left" valign="top">32.20. OAuth Support </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 32.22. Building <span class="application">libpq</span> Programs</td></tr></table></div></body></html>