]> begriffs open source - ai-pg/blob - full-docs/html/sql-notify.html
Include links to all subsection html pages, with shorter paths too
[ai-pg] / full-docs / html / sql-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>NOTIFY</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="sql-move.html" title="MOVE" /><link rel="next" href="sql-prepare.html" title="PREPARE" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">NOTIFY</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-move.html" title="MOVE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</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="sql-prepare.html" title="PREPARE">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-NOTIFY"><div class="titlepage"></div><a id="id-1.9.3.158.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">NOTIFY</span></h2><p>NOTIFY — generate a notification</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 NOTIFY <em class="replaceable"><code>channel</code></em> [ , <em class="replaceable"><code>payload</code></em> ]
4 </pre></div><div class="refsect1" id="id-1.9.3.158.5"><h2>Description</h2><p>
5    The <code class="command">NOTIFY</code> command sends a notification event together
6    with an optional <span class="quote">“<span class="quote">payload</span>”</span> string to each client application that
7    has previously executed
8    <code class="command">LISTEN <em class="replaceable"><code>channel</code></em></code>
9    for the specified channel name in the current database.
10    Notifications are visible to all users.
11   </p><p>
12    <code class="command">NOTIFY</code> provides a simple
13    interprocess communication mechanism for a collection of processes
14    accessing the same <span class="productname">PostgreSQL</span> database.
15    A payload string can be sent along with the notification, and
16    higher-level mechanisms for passing structured data can be built by using
17    tables in the database to pass additional data from notifier to listener(s).
18   </p><p>
19    The information passed to the client for a notification event includes the
20    notification channel
21    name, the notifying session's server process <acronym class="acronym">PID</acronym>, and the
22    payload string, which is an empty string if it has not been specified.
23   </p><p>
24    It is up to the database designer to define the channel names that will
25    be used in a given database and what each one means.
26    Commonly, the channel name is the same as the name of some table in
27    the database, and the notify event essentially means, <span class="quote">“<span class="quote">I changed this table,
28    take a look at it to see what's new</span>”</span>.  But no such association is enforced by
29    the <code class="command">NOTIFY</code> and <code class="command">LISTEN</code> commands.  For
30    example, a database designer could use several different channel names
31    to signal different sorts of changes to a single table.  Alternatively,
32    the payload string could be used to differentiate various cases.
33   </p><p>
34    When <code class="command">NOTIFY</code> is used to signal the occurrence of changes
35    to a particular table, a useful programming technique is to put the
36    <code class="command">NOTIFY</code> in a statement trigger that is triggered by table updates.
37    In this way, notification happens automatically when the table is changed,
38    and the application programmer cannot accidentally forget to do it.
39   </p><p>
40    <code class="command">NOTIFY</code> interacts with SQL transactions in some important
41    ways.  Firstly, if a <code class="command">NOTIFY</code> is executed inside a
42    transaction, the notify events are not delivered until and unless the
43    transaction is committed.  This is appropriate, since if the transaction
44    is aborted, all the commands within it have had no
45    effect, including <code class="command">NOTIFY</code>.  But it can be disconcerting if one
46    is expecting the notification events to be delivered immediately.  Secondly, if
47    a listening session receives a notification signal while it is within a transaction,
48    the notification event will not be delivered to its connected client until just
49    after the transaction is completed (either committed or aborted).  Again, the
50    reasoning is that if a notification were delivered within a transaction that was
51    later aborted, one would want the notification to be undone somehow —
52    but
53    the server cannot <span class="quote">“<span class="quote">take back</span>”</span> a notification once it has sent it to the client.
54    So notification events are only delivered between transactions.  The upshot of this
55    is that applications using <code class="command">NOTIFY</code> for real-time signaling
56    should try to keep their transactions short.
57   </p><p>
58    If the same channel name is signaled multiple times with identical
59    payload strings within the same transaction, only one instance of the
60    notification event is delivered to listeners.
61    On the other hand, notifications with distinct payload strings will
62    always be delivered as distinct notifications. Similarly, notifications from
63    different transactions will never get folded into one notification.
64    Except for dropping later instances of duplicate notifications,
65    <code class="command">NOTIFY</code> guarantees that notifications from the same
66    transaction get delivered in the order they were sent.  It is also
67    guaranteed that messages from different transactions are delivered in
68    the order in which the transactions committed.
69   </p><p>
70    It is common for a client that executes <code class="command">NOTIFY</code>
71    to be listening on the same notification channel itself.  In that case
72    it will get back a notification event, just like all the other
73    listening sessions.  Depending on the application logic, this could
74    result in useless work, for example, reading a database table to
75    find the same updates that that session just wrote out.  It is
76    possible to avoid such extra work by noticing whether the notifying
77    session's server process <acronym class="acronym">PID</acronym> (supplied in the
78    notification event message) is the same as one's own session's
79    <acronym class="acronym">PID</acronym> (available from <span class="application">libpq</span>).  When they
80    are the same, the notification event is one's own work bouncing
81    back, and can be ignored.
82   </p></div><div class="refsect1" id="id-1.9.3.158.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>channel</code></em></span></dt><dd><p>
83       Name of the notification channel to be signaled (any identifier).
84      </p></dd><dt><span class="term"><em class="replaceable"><code>payload</code></em></span></dt><dd><p>
85       The <span class="quote">“<span class="quote">payload</span>”</span> string to be communicated along with the
86       notification.  This must be specified as a simple string literal.
87       In the default configuration it must be shorter than 8000 bytes.
88       (If binary data or large amounts of information need to be communicated,
89       it's best to put it in a database table and send the key of the record.)
90      </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.158.7"><h2>Notes</h2><p>
91    There is a queue that holds notifications that have been sent but not
92    yet processed by all listening sessions.  If this queue becomes full,
93    transactions calling <code class="command">NOTIFY</code> will fail at commit.
94    The queue is quite large (8GB in a standard installation) and should be
95    sufficiently sized for almost every use case. However, no cleanup can take
96    place if a session executes <code class="command">LISTEN</code> and then enters a
97    transaction for a very long time. Once the queue is half full you will see
98    warnings in the log file pointing you to the session that is preventing
99    cleanup. In this case you should make sure that this session ends its
100    current transaction so that cleanup can proceed.
101   </p><p>
102    The function <code class="function">pg_notification_queue_usage</code> returns the
103    fraction of the queue that is currently occupied by pending notifications.
104    See <a class="xref" href="functions-info.html" title="9.27. System Information Functions and Operators">Section 9.27</a> for more information.
105   </p><p>
106    A transaction that has executed <code class="command">NOTIFY</code> cannot be
107    prepared for two-phase commit.
108   </p><div class="refsect2" id="id-1.9.3.158.7.5"><h3>pg_notify</h3><a id="id-1.9.3.158.7.5.2" class="indexterm"></a><p>
109     To send a notification you can also use the function
110     <code class="literal"><code class="function">pg_notify</code>(<code class="type">text</code>,
111     <code class="type">text</code>)</code>. The function takes the channel name as the
112     first argument and the payload as the second. The function is much easier
113     to use than the <code class="command">NOTIFY</code> command if you need to work with
114     non-constant channel names and payloads.
115    </p></div></div><div class="refsect1" id="id-1.9.3.158.8"><h2>Examples</h2><p>
116    Configure and execute a listen/notify sequence from
117    <span class="application">psql</span>:
118
119 </p><pre class="programlisting">
120 LISTEN virtual;
121 NOTIFY virtual;
122 Asynchronous notification "virtual" received from server process with PID 8448.
123 NOTIFY virtual, 'This is the payload';
124 Asynchronous notification "virtual" with payload "This is the payload" received from server process with PID 8448.
125
126 LISTEN foo;
127 SELECT pg_notify('fo' || 'o', 'pay' || 'load');
128 Asynchronous notification "foo" with payload "payload" received from server process with PID 14728.
129 </pre></div><div class="refsect1" id="id-1.9.3.158.9"><h2>Compatibility</h2><p>
130    There is no <code class="command">NOTIFY</code> statement in the SQL
131    standard.
132   </p></div><div class="refsect1" id="id-1.9.3.158.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-listen.html" title="LISTEN"><span class="refentrytitle">LISTEN</span></a>, <a class="xref" href="sql-unlisten.html" title="UNLISTEN"><span class="refentrytitle">UNLISTEN</span></a>, <a class="xref" href="runtime-config-resource.html#GUC-MAX-NOTIFY-QUEUE-PAGES">max_notify_queue_pages</a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-move.html" title="MOVE">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-prepare.html" title="PREPARE">Next</a></td></tr><tr><td width="40%" align="left" valign="top">MOVE </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"> PREPARE</td></tr></table></div></body></html>