LISTEN(7)
๐ LISTEN(7) โ PostgreSQL 18.6 Documentation ๐
okay bestie, sit down, hydrate, because we're learning about LISTEN โ the PostgreSQL command that's literally just your session saying "hey, keep me in the loop" ๐โจ It's giving group chat notification energy, fr fr.
๐ธ NAME ๐ธ
LISTEN โ listen for a notification ๐ฆ
โจ SYNOPSIS โจ
LISTEN channel
๐ DESCRIPTION ๐
LISTEN registers the current session as a listener on the notification channel named channel. That's it, that's the whole vibe. And if the current session is already registered as a listener for that channel? Nothing happens. Zero. Nada. The command is like "yeah bestie I got it already" ๐
Whenever the command NOTIFY channel gets invoked โ whether it's by this same session or by a totally different session connected to the same database โ all the sessions currently listening on that notification channel get notified, and each one will in turn notify its connected client application. It's a whole chain of gossip and honestly iconic ๐
A session can be unregistered for a given notification channel with the UNLISTEN command. And a session's listen registrations are automatically cleared when the session ends โ so no cleanup needed, she cleans up after herself ๐
Now, how a client application actually detects these notification events depends on which PostgreSQL application programming interface it's using. Lowkey important, so listen up:
- With the libpq library, the application issues
LISTENas an ordinary SQL command, and then has to periodically call the functionPQnotifiesto find out whether any notification events have been received. Yes, you have to check. It's not gonna come to you ๐ญ - Other interfaces, like libpgtcl, provide higher-level methods for handling notify events. And actually, with
libpgtcl, the application programmer should not even issueLISTENorUNLISTENdirectly. Not even a little bit.
See the documentation for the interface you're using for more details, bestie ๐ซถ
๐ PARAMETERS ๐
channelโ Name of a notification channel (any identifier). Yep, any identifier. Go feral, pick a cute one ๐
๐ฅ NOTES ๐ฅ
heads up, this part is where the real ones live. read it.
LISTEN takes effect at transaction commit. So if LISTEN or UNLISTEN gets executed within a transaction that later rolls back, the set of notification channels being listened to is unchanged. Rolled back means it never happened, periodt ๐
A transaction that has executed LISTEN cannot be prepared for two-phase commit. Like, cannot. Don't try it, it's not that kind of party ๐ซ
Now, here's the messy part โ there is a race condition when first setting up a listening session. Like, imagine: if concurrently-committing transactions are sending notify events, exactly which of those will the newly listening session receive? The answer, bestie, is that the session will receive all events committed after an instant during the transaction's commit step. But that instant is slightly later than any database state that the transaction could have observed in queries. Sneaky, right? ๐ญ
This leads to the following rule for using LISTEN, and I need you to tattoo it on your brain:
- 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 you already observed in the initial database inspection โ but this is usually harmless, so don't spiral about it ๐ง
NOTIFY(7) contains a more extensive discussion of the use of LISTEN and NOTIFY. Go read it, it's the sequel ๐
โญ EXAMPLES โญ
Configure and execute a listen/notify sequence from psql:
LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448.
Look at that. She listened, she got notified, PID 8448 said hi. Chef's kiss ๐จ๐ณ๐
๐ COMPATIBILITY ๐
There is no LISTEN statement in the SQL standard. PostgreSQL said "we're doing our own thing" and honestly? respect ๐ซก
๐ฆ SEE ALSO ๐ฆ
NOTIFY(7)UNLISTEN(7)max_notify_queue_pages
and that's the whole page, bestie!! This was PostgreSQL 18.6, dated 2026 โ no cap, you now understand LISTEN better than like 90% of people who use it daily. go forth and subscribe to your channels ๐โจ๐๐ฆ๐
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms