]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/libpq-notify.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / libpq-notify.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.9. Asynchronous Notification</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-fastpath.html" title="32.8. The Fast-Path Interface" /><link rel="next" href="libpq-copy.html" title="32.10. Functions Associated with the COPY Command" /></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.9. Asynchronous Notification</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-fastpath.html" title="32.8. The Fast-Path Interface">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-copy.html" title="32.10. Functions Associated with the COPY Command">Next</a></td></tr></table><hr /></div><div class="sect1" id="LIBPQ-NOTIFY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">32.9. Asynchronous Notification <a href="#LIBPQ-NOTIFY" class="id_link">#</a></h2></div></div></div><a id="id-1.7.3.16.2" class="indexterm"></a><p>
3    <span class="productname">PostgreSQL</span> offers asynchronous notification
4    via the <code class="command">LISTEN</code> and <code class="command">NOTIFY</code>
5    commands.  A client session registers its interest in a particular
6    notification channel with the <code class="command">LISTEN</code> command (and
7    can stop listening with the <code class="command">UNLISTEN</code> command).  All
8    sessions listening on a particular channel will be notified
9    asynchronously when a <code class="command">NOTIFY</code> command with that
10    channel name is executed by any session. A <span class="quote">“<span class="quote">payload</span>”</span> string can
11    be passed to communicate additional data to the listeners.
12   </p><p>
13    <span class="application">libpq</span> applications submit
14    <code class="command">LISTEN</code>, <code class="command">UNLISTEN</code>,
15    and <code class="command">NOTIFY</code> commands as
16    ordinary SQL commands.  The arrival of <code class="command">NOTIFY</code>
17    messages can subsequently be detected by calling
18    <code class="function" id="LIBPQ-PQNOTIFIES">PQnotifies</code>.<a id="id-1.7.3.16.4.7" class="indexterm"></a>
19   </p><p>
20    The function <code class="function">PQnotifies</code> returns the next notification
21    from a list of unhandled notification messages received from the server.
22    It returns a null pointer if there are no pending notifications.  Once a
23    notification is returned from <code class="function">PQnotifies</code>, it is considered
24    handled and will be removed from the list of notifications.
25
26 </p><pre class="synopsis">
27 PGnotify *PQnotifies(PGconn *conn);
28
29 typedef struct pgNotify
30 {
31     char *relname;              /* notification channel name */
32     int  be_pid;                /* process ID of notifying server process */
33     char *extra;                /* notification payload string */
34 } PGnotify;
35 </pre><p>
36
37    After processing a <code class="structname">PGnotify</code> object returned
38    by <code class="function">PQnotifies</code>, be sure to free it with
39    <a class="xref" href="libpq-misc.html#LIBPQ-PQFREEMEM"><code class="function">PQfreemem</code></a>.  It is sufficient to free the
40    <code class="structname">PGnotify</code> pointer; the
41    <code class="structfield">relname</code> and <code class="structfield">extra</code>
42    fields do not represent separate allocations.  (The names of these fields
43    are historical; in particular, channel names need not have anything to
44    do with relation names.)
45   </p><p>
46    <a class="xref" href="libpq-example.html#LIBPQ-EXAMPLE-2" title="Example 32.2. libpq Example Program 2">Example 32.2</a> gives a sample program that illustrates
47    the use of asynchronous notification.
48   </p><p>
49    <code class="function">PQnotifies</code> does not actually read data from the
50    server; it just returns messages previously absorbed by another
51    <span class="application">libpq</span> function.  In ancient releases of
52    <span class="application">libpq</span>, the only way to ensure timely receipt
53    of <code class="command">NOTIFY</code> messages was to constantly submit commands, even
54    empty ones, and then check <code class="function">PQnotifies</code> after each
55    <a class="xref" href="libpq-exec.html#LIBPQ-PQEXEC"><code class="function">PQexec</code></a>.  While this still works, it is deprecated
56    as a waste of processing power.
57   </p><p>
58    A better way to check for <code class="command">NOTIFY</code> messages when you have no
59    useful commands to execute is to call
60    <a class="xref" href="libpq-async.html#LIBPQ-PQCONSUMEINPUT"><code class="function">PQconsumeInput</code>
61      </a>, then check
62    <code class="function">PQnotifies</code>.  You can use
63    <code class="function">select()</code> to wait for data to arrive from the
64    server, thereby using no <acronym class="acronym">CPU</acronym> power unless there is
65    something to do.  (See <a class="xref" href="libpq-status.html#LIBPQ-PQSOCKET"><code class="function">PQsocket</code></a> to obtain the file
66    descriptor number to use with <code class="function">select()</code>.) Note that
67    this will work OK whether you submit commands with
68    <a class="xref" href="libpq-async.html#LIBPQ-PQSENDQUERY"><code class="function">PQsendQuery</code></a>/<a class="xref" href="libpq-async.html#LIBPQ-PQGETRESULT"><code class="function">PQgetResult</code></a> or
69    simply use <a class="xref" href="libpq-exec.html#LIBPQ-PQEXEC"><code class="function">PQexec</code></a>.  You should, however, remember
70    to check <code class="function">PQnotifies</code> after each
71    <a class="xref" href="libpq-async.html#LIBPQ-PQGETRESULT"><code class="function">PQgetResult</code></a> or <a class="xref" href="libpq-exec.html#LIBPQ-PQEXEC"><code class="function">PQexec</code></a>, to
72    see if any notifications came in during the processing of the command.
73   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-fastpath.html" title="32.8. The Fast-Path Interface">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-copy.html" title="32.10. Functions Associated with the COPY Command">Next</a></td></tr><tr><td width="40%" align="left" valign="top">32.8. The Fast-Path Interface </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.10. Functions Associated with the <code class="command">COPY</code> Command</td></tr></table></div></body></html>