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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
<!--
doc/src/sgml/ref/listen.sgml
PostgreSQL documentation
-->
<refentry id="sql-listen">
<indexterm zone="sql-listen">
<primary>LISTEN</primary>
</indexterm>
<refmeta>
<refentrytitle>LISTEN</refentrytitle>
<manvolnum>7</manvolnum>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
<refnamediv>
<refname>LISTEN</refname>
<refpurpose>listen for a notification</refpurpose>
</refnamediv>
<refsynopsisdiv>
<synopsis>
LISTEN <replaceable class="parameter">channel</replaceable>
</synopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
<command>LISTEN</command> registers the current session as a
listener on the notification channel named <replaceable
class="parameter">channel</replaceable>.
If the current session is already registered as a listener for
this notification channel, nothing is done.
</para>
<para>
Whenever the command <command>NOTIFY <replaceable
class="parameter">channel</replaceable></command> 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.
</para>
<para>
A session can be unregistered for a given notification channel with the
<command>UNLISTEN</command> command. A session's listen
registrations are automatically cleared when the session ends.
</para>
<para>
The method a client application must use to detect notification events depends on
which <productname>PostgreSQL</productname> application programming interface it
uses. With the <application>libpq</application> library, the application issues
<command>LISTEN</command> as an ordinary SQL command, and then must
periodically call the function <function>PQnotifies</function> to find out
whether any notification events have been received. Other interfaces such as
<application>libpgtcl</application> provide higher-level methods for handling notify events; indeed,
with <application>libpgtcl</application> the application programmer should not even issue
<command>LISTEN</command> or <command>UNLISTEN</command> directly. See the
documentation for the interface you are using for more details.
</para>
</refsect1>
<refsect1>
<title>Parameters</title>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">channel</replaceable></term>
<listitem>
<para>
Name of a notification channel (any identifier).
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1>
<title>Notes</title>
<para>
<command>LISTEN</command> takes effect at transaction commit.
If <command>LISTEN</command> or <command>UNLISTEN</command> is executed
within a transaction that later rolls back, the set of notification
channels being listened to is unchanged.
</para>
<para>
A transaction that has executed <command>LISTEN</command> cannot be
prepared for two-phase commit.
</para>
<para>
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 <command>LISTEN</command>: 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.
</para>
<para>
<xref linkend="sql-notify"/>
contains a more extensive
discussion of the use of <command>LISTEN</command> and
<command>NOTIFY</command>.
</para>
</refsect1>
<refsect1>
<title>Examples</title>
<para>
Configure and execute a listen/notify sequence from
<application>psql</application>:
<programlisting>
LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448.
</programlisting></para>
</refsect1>
<refsect1>
<title>Compatibility</title>
<para>
There is no <command>LISTEN</command> statement in the SQL
standard.
</para>
</refsect1>
<refsect1>
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="sql-notify"/></member>
<member><xref linkend="sql-unlisten"/></member>
<member><xref linkend="guc-max-notify-queue-pages"/></member>
</simplelist>
</refsect1>
</refentry>
|