3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "LISTEN" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 LISTEN \- listen for a notification
40 registers the current session as a listener on the notification channel named
41 \fIchannel\fR\&. If the current session is already registered as a listener for this notification channel, nothing is done\&.
44 \fBNOTIFY \fR\fB\fIchannel\fR\fR
45 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\&.
47 A session can be unregistered for a given notification channel with the
49 command\&. A session\*(Aqs listen registrations are automatically cleared when the session ends\&.
51 The method a client application must use to detect notification events depends on which
53 application programming interface it uses\&. With the
55 library, the application issues
57 as an ordinary SQL command, and then must periodically call the function
59 to find out whether any notification events have been received\&. Other interfaces such as
61 provide higher\-level methods for handling notify events; indeed, with
63 the application programmer should not even issue
67 directly\&. See the documentation for the interface you are using for more details\&.
72 Name of a notification channel (any identifier)\&.
77 takes effect at transaction commit\&. If
81 is executed within a transaction that later rolls back, the set of notification channels being listened to is unchanged\&.
83 A transaction that has executed
85 cannot be prepared for two\-phase commit\&.
87 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\*(Aqs 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
88 \fBLISTEN\fR: 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\&.
91 contains a more extensive discussion of the use of
97 Configure and execute a listen/notify sequence from
106 Asynchronous notification "virtual" received from server process with PID 8448\&.
115 statement in the SQL standard\&.
117 \fBNOTIFY\fR(7), \fBUNLISTEN\fR(7), max_notify_queue_pages