2 doc/src/sgml/ref/listen.sgml
3 PostgreSQL documentation
6 <refentry id="SQL-LISTEN">
8 <refentrytitle>LISTEN</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements</refmiscinfo>
14 <refname>LISTEN</refname>
15 <refpurpose>listen for a notification</refpurpose>
18 <indexterm zone="sql-listen">
19 <primary>LISTEN</primary>
24 LISTEN <replaceable class="PARAMETER">channel</replaceable>
29 <title>Description</title>
32 <command>LISTEN</command> registers the current session as a
33 listener on the notification channel named <replaceable
34 class="PARAMETER">channel</replaceable>.
35 If the current session is already registered as a listener for
36 this notification channel, nothing is done.
40 Whenever the command <command>NOTIFY <replaceable
41 class="PARAMETER">channel</replaceable></command> is invoked, either
42 by this session or another one connected to the same database, all
43 the sessions currently listening on that notification channel are
44 notified, and each will in turn notify its connected client
49 A session can be unregistered for a given notification channel with the
50 <command>UNLISTEN</command> command. A session's listen
51 registrations are automatically cleared when the session ends.
55 The method a client application must use to detect notification events depends on
56 which <productname>PostgreSQL</productname> application programming interface it
57 uses. With the <application>libpq</> library, the application issues
58 <command>LISTEN</command> as an ordinary SQL command, and then must
59 periodically call the function <function>PQnotifies</function> to find out
60 whether any notification events have been received. Other interfaces such as
61 <application>libpgtcl</> provide higher-level methods for handling notify events; indeed,
62 with <application>libpgtcl</> the application programmer should not even issue
63 <command>LISTEN</command> or <command>UNLISTEN</command> directly. See the
64 documentation for the interface you are using for more details.
68 <xref linkend="sql-notify">
69 contains a more extensive
70 discussion of the use of <command>LISTEN</command> and
71 <command>NOTIFY</command>.
76 <title>Parameters</title>
80 <term><replaceable class="PARAMETER">channel</replaceable></term>
83 Name of a notification channel (any identifier).
94 <command>LISTEN</command> takes effect at transaction commit.
95 If <command>LISTEN</command> or <command>UNLISTEN</command> is executed
96 within a transaction that later rolls back, the set of notification
97 channels being listened to is unchanged.
100 A transaction that has executed <command>LISTEN</command> cannot be
101 prepared for two-phase commit.
106 <title>Examples</title>
109 Configure and execute a listen/notify sequence from
110 <application>psql</application>:
115 Asynchronous notification "virtual" received from server process with PID 8448.
116 </programlisting></para>
120 <title>Compatibility</title>
123 There is no <command>LISTEN</command> statement in the SQL
129 <title>See Also</title>
131 <simplelist type="inline">
132 <member><xref linkend="sql-notify"></member>
133 <member><xref linkend="sql-unlisten"></member>