LISTEN(7)
π
LISTEN(7) β the PostgreSQL LISTEN page, bestie edition β¨
okay so this is the LISTEN page from the PostgreSQL 13.23 Documentation, and honestly? it's giving "quietly powerful." it's about listening for notifications, and no cap, it's one of the most underrated little commands in the whole system. let's get into it π
πΈ NAME πΈ
LISTEN β listen for a notification π¦
β¨ SYNOPSIS β¨
LISTEN channel
that's it. that's the whole vibe. one command, one channel, and you're tuned in π
π DESCRIPTION π
so LISTEN registers the current session as a listener on the notification channel named channel. and if the current session is already registered as a listener for this notification channel? literally nothing happens. no error, no drama, nothing is done. it's just like "yeah babe i already know" π«Ά
whenever the command NOTIFY channel is invoked β whether it's by this session or another one 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 giving group chat energy, fr fr π£
a session can be unregistered for a given notification channel with the UNLISTEN command. and listen (pun intended), a session's listen registrations are automatically cleared when the session ends β so you don't have to clean up after yourself, it's iconic like that β¨
now the part where it gets a little technical, stay with me π
the method a client application must use to detect notification events depends on which PostgreSQL application programming interface it uses:
- with the
libpqlibrary, the application issuesLISTENas an ordinary SQL command, and then must periodically call the functionPQnotifiesto find out whether any notification events have been received. - other interfaces such as
libpgtclprovide higher-level methods for handling notify events. in fact, withlibpgtclthe application programmer should not even issueLISTENorUNLISTENdirectly. like, don't do it, bestie.
see the documentation for the interface you're using for more details β the manual's words, not mine π
π PARAMETERS π
channelβ name of a notification channel (any identifier). that's the whole thing. any identifier you want, it's yours π
β NOTES β
okay this section is important, so pay attention, no scrolling past π
LISTENtakes effect at transaction commit. so ifLISTENorUNLISTENis executed within a transaction that later rolls back, the set of notification channels being listened to is unchanged. like it never happened π- a transaction that has executed
LISTENcannot be prepared for two-phase commit. important. write it down. - 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. sneaky, right?
this leads to the following rule for using LISTEN, and it's the main character of this whole section π:
- 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. phew π
NOTIFY(7) contains a more extensive discussion of the use of LISTEN and NOTIFY β so if you want the full lore, go read that one too π¦
π 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. clean. gorgeous. ate and left no crumbs π β¨
π¦ COMPATIBILITY π¦
there is no LISTEN statement in the SQL standard. nope. not a thing there. this is a PostgreSQL original, iykyk π
π₯ SEE ALSO π₯
and that's the whole page, bestie π«Ά go forth and listen (the command, not the emotional labour). this was PostgreSQL 13.23, dated 2025, and honestly? she's timeless. mwah πππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql13 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms