NOTIFY(7)
π NOTIFY(7) β the PostgreSQL glow-up where your database literally slides into other sessions' DMs
ok so bestie, you know how the manual page header says NOTIFY(7) and it lives in the PostgreSQL 16.15 Documentation? Yeah. That's the one. Sit down, grab a lil snack π, and let me tell you about the coolest little interprocess messenger in the whole server.
β¨ SYNOPSIS β¨
NOTIFY channel [ , payload ]
that's it. that's the whole vibe. one command, one channel, optional payload. slay π
πΈ DESCRIPTION πΈ
So the NOTIFY command sends a notification event β like, together with an optional "payload" string β to every client application that has previously run LISTEN channel for that specific channel name in the current database. And here's the thing lovelies: notifications are visible to all users. no gatekeeping. periodt.
NOTIFY gives u a simple interprocess communication mechanism for a bunch of processes all touching the same PostgreSQL database. You can send a payload string along with the notification, and β this is the iconic part β you can build higher-level mechanisms for passing structured data by using tables in the database to pass extra data from notifier to listener(s). iykyk π§
The info that gets passed to the client for a notification event includes:
- the notification channel name,
- the notifying session's server process PID,
- and the payload string (which is an empty string if it hasn't been specified).
Now, who decides what channels exist? That's on the database designer, bestie. They define the channel names used in a given database and what each one means. The common pattern is: channel name == the name of some table, and the notify event basically means "I changed this table, take a look at it to see what's new" π. But β important β no such association is enforced by the NOTIFY and LISTEN commands. For example, a database designer could use several different channel names to signal different sorts of changes to a single table. Or, alternatively, the payload string could be used to differentiate various cases. ur call.
When NOTIFY is used to signal that changes happened to a particular table, a genuinely useful programming technique is to put the NOTIFY inside a statement trigger that's triggered by table updates. That way the notification happens automatically when the table changes, and the application programmer literally cannot accidentally forget to do it. no cap, that's the move π«Ά
π₯ the transaction lore (read this, it matters) π₯
NOTIFY interacts with SQL transactions in some important ways, so listen up:
- First: if a
NOTIFYis executed inside a transaction, the notify events are not delivered until and unless the transaction is committed. This is appropriate, since if the transaction is aborted, all the commands within it have had no effect β includingNOTIFY. But ngl, it can be disconcerting if you're expecting the events to be delivered immediately. π - Second: if a listening session receives a notification signal while it's within a transaction, the notification event will not be delivered to its connected client until just after the transaction is completed (either committed or aborted). Again, the reasoning: if a notification were delivered within a transaction that was later aborted, you'd want the notification undone somehow β but the server cannot "take back" a notification once it has sent it to the client. So notification events are only delivered between transactions. π
The upshot? Applications using NOTIFY for real-time signaling should try to keep their transactions short. seriously. short. like your attention span for boring docs, except this doc is not boring, obviously.
π¦ deduplication & ordering (the messy part) π¦
If the same channel name is signaled multiple times with identical payload strings within the same transaction, only one instance of the notification event is delivered to listeners. iconic efficiency.
On the other hand, notifications with distinct payload strings will always be delivered as distinct notifications. Similarly, notifications from different transactions will never get folded into one notification.
So, except for dropping later instances of duplicate notifications, NOTIFY guarantees that notifications from the same transaction get delivered in the order they were sent. It's also guaranteed that messages from different transactions are delivered in the order in which the transactions committed. chef's kiss π
π the "am I talking to myself" problem π
It's super common for a client that executes NOTIFY to also be listening on the same notification channel itself. In that case it'll get back a notification event, just like all the other listening sessions. Depending on the application logic, this could result in useless work β for example, reading a database table to find the same updates that that session just wrote out. π
You can avoid that extra work by checking whether the notifying session's server process PID (supplied in the notification event message) is the same as your own session's PID (available from libpq). When they're the same, the notification event is just your own work bouncing back at you, and you can ignore it. main character energy: recognizing yourself in the mirror πͺ
π PARAMETERS π
channelβ Name of the notification channel to be signaled (any identifier). any identifier, bestie. go wild.payloadβ The "payload" string to be communicated along with the notification. This must be specified as a simple string literal. In the default configuration it must be shorter than 8000 bytes. (If you need to send binary data or large amounts of information, it's best to put it in a database table and send the key of the record instead β much cleaner β¨.)
π NOTES π
ok so there's a queue that holds notifications that have been sent but not yet processed by all listening sessions. If this queue becomes full, transactions calling NOTIFY will fail at commit. π¬
The queue is quite large β 8GB in a standard installation β and should be sufficiently sized for almost every use case. However! No cleanup can take place if a session executes LISTEN and then enters a transaction for a very long time. Once the queue is half full you'll see warnings in the log file pointing you to the session that's preventing cleanup. In this case you should make sure that session ends its current transaction so cleanup can proceed. be a good citizen, end ur transaction π«‘
The function pg_notification_queue_usage returns the fraction of the queue that's currently occupied by pending notifications. See Section 9.26 for more info.
β οΈ Heads up: a transaction that has executed NOTIFY cannot be prepared for two-phase commit. that's just how it is, don't fight it.
π§ pg_notify π§
To send a notification you can also use the function pg_notify(text, text). It takes the channel name as the first argument and the payload as the second. The function is much easier to use than the NOTIFY command if you need to work with non-constant channel names and payloads. lowkey the superior API if ur being dynamic about it π
β EXAMPLES β
Configure and execute a listen/notify sequence from psql:
LISTEN virtual;
NOTIFY virtual;
Asynchronous notification "virtual" received from server process with PID 8448.
NOTIFY virtual, 'This is the payload';
Asynchronous notification "virtual" with payload "This is the payload" received from server process with PID 8448.
LISTEN foo;
SELECT pg_notify('fo' || 'o', 'pay' || 'load');
Asynchronous notification "foo" with payload "payload" received from server process with PID 14728.
look at that. it's giving communication. π¦
π COMPATIBILITY π
There is no NOTIFY statement in the SQL standard. we're built different. π
π SEE ALSO π
ok that's the whole page, bestie, go forth and notify responsibly π
β with love, from the PostgreSQL 16.15 docs, dated 2026, page NOTIFY(7), which is genuinely the most fun you can have with interprocess communication πΈβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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