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