codekingpro/portable-devtools
115k
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 16.3 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">3NOTIFY <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 together6 with an optional <span class="quote">“<span class="quote">payload</span>”</span> string to each client application that7 has previously executed8 <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 simple13 interprocess communication mechanism for a collection of processes14 accessing the same <span class="productname">PostgreSQL</span> database.15 A payload string can be sent along with the notification, and16 higher-level mechanisms for passing structured data can be built by using17 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 the20 notification channel21 name, the notifying session's server process <acronym class="acronym">PID</acronym>, and the22 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 will25 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 in27 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 by29 the <code class="command">NOTIFY</code> and <code class="command">LISTEN</code> commands. For30 example, a database designer could use several different channel names31 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 changes35 to a particular table, a useful programming technique is to put the36 <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 important41 ways. Firstly, if a <code class="command">NOTIFY</code> is executed inside a42 transaction, the notify events are not delivered until and unless the43 transaction is committed. This is appropriate, since if the transaction44 is aborted, all the commands within it have had no45 effect, including <code class="command">NOTIFY</code>. But it can be disconcerting if one46 is expecting the notification events to be delivered immediately. Secondly, if47 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 just49 after the transaction is completed (either committed or aborted). Again, the50 reasoning is that if a notification were delivered within a transaction that was51 later aborted, one would want the notification to be undone somehow —52 but53 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 this55 is that applications using <code class="command">NOTIFY</code> for real-time signaling56 should try to keep their transactions short.57 </p><p>58 If the same channel name is signaled multiple times with identical59 payload strings within the same transaction, only one instance of the60 notification event is delivered to listeners.61 On the other hand, notifications with distinct payload strings will62 always be delivered as distinct notifications. Similarly, notifications from63 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 same66 transaction get delivered in the order they were sent. It is also67 guaranteed that messages from different transactions are delivered in68 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 case72 it will get back a notification event, just like all the other73 listening sessions. Depending on the application logic, this could74 result in useless work, for example, reading a database table to75 find the same updates that that session just wrote out. It is76 possible to avoid such extra work by noticing whether the notifying77 session's server process <acronym class="acronym">PID</acronym> (supplied in the78 notification event message) is the same as one's own session's79 <acronym class="acronym">PID</acronym> (available from <span class="application">libpq</span>). When they80 are the same, the notification event is one's own work bouncing81 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 the86 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 not92 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 be95 sufficiently sized for almost every use case. However, no cleanup can take96 place if a session executes <code class="command">LISTEN</code> and then enters a97 transaction for a very long time. Once the queue is half full you will see98 warnings in the log file pointing you to the session that is preventing99 cleanup. In this case you should make sure that this session ends its100 current transaction so that cleanup can proceed.101 </p><p>102 The function <code class="function">pg_notification_queue_usage</code> returns the103 fraction of the queue that is currently occupied by pending notifications.104 See <a class="xref" href="functions-info.html" title="9.26. System Information Functions and Operators">Section 9.26</a> for more information.105 </p><p>106 A transaction that has executed <code class="command">NOTIFY</code> cannot be107 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 function110 <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 the112 first argument and the payload as the second. The function is much easier113 to use than the <code class="command">NOTIFY</code> command if you need to work with114 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 from117 <span class="application">psql</span>:118 119</p><pre class="programlisting">120LISTEN virtual;121NOTIFY virtual;122Asynchronous notification "virtual" received from server process with PID 8448.123NOTIFY virtual, 'This is the payload';124Asynchronous notification "virtual" with payload "This is the payload" received from server process with PID 8448.125 126LISTEN foo;127SELECT pg_notify('fo' || 'o', 'pay' || 'load');128Asynchronous 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 SQL131 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></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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> PREPARE</td></tr></table></div></body></html>