LISTEN(7)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

๐Ÿ’– 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 LISTEN as an ordinary SQL command, and then has to periodically call the function PQnotifies to 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 issue LISTEN or UNLISTEN directly. 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:

  1. First execute (and commit!) that command.
  2. Then, in a new transaction, inspect the database state as needed by the application logic.
  3. 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 ๐Ÿฆ‹


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