LISTEN(7)
π LISTEN(7) β the LISTEN page, bestie edition π
okay so this is the LISTEN(7) page from the PostgreSQL 14.24 Documentation, and honestly? it's giving "database group chat" and i'm obsessed. let's get into it π«Ά
β¨ NAME β¨
LISTEN β literally just. listen for a notification. that's the whole vibe. ππ
β¨ SYNOPSIS β¨
LISTEN channel
that's it. one line. iconic. π
β¨ DESCRIPTION β¨
so LISTEN registers the current session as a listener on the notification channel named channel. lowkey a "subscribe" button for your database session, no cap. and if the current session is already registered as a listener for this notification channel? nothing is done. it's a no-op. she already knew. π
whenever the command NOTIFY channel gets 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. it's a whole relay race and everyone ate. π₯
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 like, no ghost subscriptions haunting the place. π¦
now, the method a client application must use to detect notification events depends on which PostgreSQL application programming interface it uses. keep up bestie:
- 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. yes it's giving "checking your phone every 30 seconds" energy. π - other interfaces such as
libpgtclprovide higher-level methods for handling notify events; indeed, withlibpgtclthe application programmer should not even issueLISTENorUNLISTENdirectly. main character energy, it handles it for you.
see the documentation for the interface you are using for more details. β¨
β¨ PARAMETERS β¨
channelβ name of a notification channel (any identifier). any identifier, girl. go wild. πΈ
β¨ NOTES β¨
okay this is the part you actually need to read, don't skip it π
LISTENtakes effect at transaction commit. ifLISTENorUNLISTENis executed within a transaction that later rolls back, the set of notification channels being listened to is unchanged. so like, the vibe is: committed or it didn't happen. π- a transaction that has executed
LISTENcannot be prepared for two-phase commit. not up for debate, it's just not allowed. - β οΈ 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? and 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 little timing thing. π
so here's the rule for using LISTEN, straight up: 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. order matters bestie, it's a whole choreography. π§
the first few received notifications might refer to updates already observed in the initial database inspection, but this is usually harmless. so don't panic. π
also NOTIFY(7) contains a more extensive discussion of the use of LISTEN and NOTIFY β go read it, it's the extended cut. π
β¨ 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. slay. πβ¨
β¨ COMPATIBILITY β¨
there is no LISTEN statement in the SQL standard. so this is a PostgreSQL exclusive, iykyk. π
β¨ SEE ALSO β¨
the whole friend group fr fr π«Ά
and that's the page!! this little beauty is from PostgreSQL 14.24, dated 2026 β so go forth, listen responsibly, commit your transactions, and never forget to check PQnotifies. mwah πππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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