summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/sql-listen.html
blob: 1348cc9e6abbc6adda09ddad740464f01c312106 (plain)
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
<?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>LISTEN</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-insert.html" title="INSERT" /><link rel="next" href="sql-load.html" title="LOAD" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">LISTEN</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-insert.html" title="INSERT">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 15.7 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-load.html" title="LOAD">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-LISTEN"><div class="titlepage"></div><a id="id-1.9.3.153.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">LISTEN</span></h2><p>LISTEN — listen for a notification</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
LISTEN <em class="replaceable"><code>channel</code></em>
</pre></div><div class="refsect1" id="id-1.9.3.153.5"><h2>Description</h2><p>
   <code class="command">LISTEN</code> registers the current session as a
   listener on the notification channel named <em class="replaceable"><code>channel</code></em>.
   If the current session is already registered as a listener for
   this notification channel, nothing is done.
  </p><p>
   Whenever the command <code class="command">NOTIFY <em class="replaceable"><code>channel</code></em></code> is invoked, either
   by this session or another one connected to the same database, all
   the sessions currently listening on that notification channel are
   notified, and each will in turn notify its connected client
   application.
  </p><p>
   A session can be unregistered for a given notification channel with the
   <code class="command">UNLISTEN</code> command.  A session's listen
   registrations are automatically cleared when the session ends.
  </p><p>
   The method a client application must use to detect notification events depends on
   which <span class="productname">PostgreSQL</span> application programming interface it
   uses.  With the <span class="application">libpq</span> library, the application issues
   <code class="command">LISTEN</code> as an ordinary SQL command, and then must
   periodically call the function <code class="function">PQnotifies</code> to find out
   whether any notification events have been received.  Other interfaces such as
   <span class="application">libpgtcl</span> provide higher-level methods for handling notify events; indeed,
   with <span class="application">libpgtcl</span> the application programmer should not even issue
   <code class="command">LISTEN</code> or <code class="command">UNLISTEN</code> directly.  See the
   documentation for the interface you are using for more details.
  </p></div><div class="refsect1" id="id-1.9.3.153.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>
      Name of a notification channel (any identifier).
     </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.153.7"><h2>Notes</h2><p>
   <code class="command">LISTEN</code> takes effect at transaction commit.
   If <code class="command">LISTEN</code> or <code class="command">UNLISTEN</code> is executed
   within a transaction that later rolls back, the set of notification
   channels being listened to is unchanged.
  </p><p>
   A transaction that has executed <code class="command">LISTEN</code> cannot be
   prepared for two-phase commit.
  </p><p>
   There is a race condition when first setting up a listening session:
   if concurrently-committing transactions are sending notify events,
   exactly which of those will the newly listening session receive?
   The answer is that the session will receive all events committed after
   an instant during the transaction's commit step.  But that is slightly
   later than any database state that the transaction could have observed
   in queries.  This leads to the following rule for
   using <code class="command">LISTEN</code>: first execute (and commit!) that
   command, then in a new transaction inspect the database state as needed
   by the application logic, then rely on notifications to find out about
   subsequent changes to the database state.  The first few received
   notifications might refer to updates already observed in the initial
   database inspection, but this is usually harmless.
  </p><p>
   <a class="xref" href="sql-notify.html" title="NOTIFY"><span class="refentrytitle">NOTIFY</span></a>
   contains a more extensive
   discussion of the use of <code class="command">LISTEN</code> and
   <code class="command">NOTIFY</code>.
  </p></div><div class="refsect1" id="id-1.9.3.153.8"><h2>Examples</h2><p>
   Configure and execute a listen/notify sequence from
   <span class="application">psql</span>:

</p><pre class="programlisting">
LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448.
</pre></div><div class="refsect1" id="id-1.9.3.153.9"><h2>Compatibility</h2><p>
   There is no <code class="command">LISTEN</code> statement in the SQL
   standard.
  </p></div><div class="refsect1" id="id-1.9.3.153.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-notify.html" title="NOTIFY"><span class="refentrytitle">NOTIFY</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-insert.html" title="INSERT">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-load.html" title="LOAD">Next</a></td></tr><tr><td width="40%" align="left" valign="top">INSERT </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.7 Documentation">Home</a></td><td width="40%" align="right" valign="top"> LOAD</td></tr></table></div></body></html>