NOTIFY(7)
๐ NOTIFY(7) โ the "hey bestie, something changed" manual ๐
okay so. this is the page for NOTIFY(7), one of the PostgreSQL manual pages. it lives in the PostgreSQL 18.6 Documentation set, and honestly? it's giving interprocess communication but make it cute. let's get into it ๐
โจ NAME โจ
NOTIFY โ generate a notification ๐
โจ SYNOPSIS โจ
NOTIFY channel [ , payload ]
that's it. that's the whole vibe. no cap. ๐ฆ
โจ DESCRIPTION โจ
so the NOTIFY command sends a notification event to every client application that already ran LISTEN channel for that channel name in the current database โ and it can carry an optional "payload" string with it. Notifications are visible to all users. everyone eats, nobody's left out ๐
NOTIFY is basically 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 if you want to pass structured data, you can build higher-level mechanisms on top by using tables in the database to pass the extra data from notifier to listener(s). iconic behavior tbh ๐
the info that actually reaches the client for a notification event is:
- the notification channel name ๐ธ
- the notifying session's server process PID ๐ฅ
- the payload string โ which is an empty string if you didn't specify one ๐
it's up to the database designer to define which channel names get used in a given database and what each one means. commonly, the channel name is the same as the name of some table in the database, and the notify event basically means "I changed this table, take a look at it to see what's new". But โ and this is important โ no such association is enforced by the NOTIFY and LISTEN commands. no cap, zero enforcement. For example, a database designer could use several different channel names to signal different sorts of changes to a single table. Alternatively, the payload string could be used to differentiate various cases. your database, your rules โจ
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 fires on table updates. that way, notification happens automatically when the table is changed, and the application programmer literally cannot accidentally forget to do it. main character energy for your trigger ๐ซถ
NOTIFY interacts with SQL transactions in some important ways, and you need to know these:
- firstly, 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 it can be disconcerting if you're expecting the notification events to be delivered immediately. ๐ญ - secondly, if a listening session receives a notification signal while it is 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 is that if a notification were delivered within a transaction that was later aborted, you'd want the notification to be 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 of all that? applications using NOTIFY for real-time signaling should try to keep their transactions short. short transactions = happy notifications. periodt ๐
now, the dedup rules, because they're lowkey important:
- 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. ๐
- 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. ๐ฆ
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 is also guaranteed that messages from different transactions are delivered in the order in which the transactions committed. the ordering is immaculate, we love to see it ๐
it's super common for a client that executes NOTIFY to also be listening on the same notification channel itself. In that case it will 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. ๐ It is possible to avoid such extra work by noticing whether the notifying session's server process PID (supplied in the notification event message) is the same as one's own session's PID (available from libpq). When they are the same, the notification event is just your own work bouncing back, and it can be ignored. not me ghosting my own notifications ๐
โจ 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 binary data or large amounts of information need to be communicated, it's best to put it in a database table and send the key of the record.) ๐
โจ NOTES โจ
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 this session ends its current transaction so that cleanup can proceed. be a good citizen, end your transaction ๐ซถ
the function pg_notification_queue_usage returns the fraction of the queue that is currently occupied by pending notifications. See Section 9.27 for more information. ๐
โ ๏ธ heads up: a transaction that has executed NOTIFY cannot be prepared for two-phase commit. that's just how it is, no exceptions.
๐ง pg_notify ๐ง
to send a notification you can also use the function pg_notify(text, text). The function 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. like, way easier. chef's kiss ๐ฉโ๐ณ๐
โจ 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 those PIDs. 8448. 14728. the range?? we stan a working notification system ๐
โจ COMPATIBILITY โจ
There is no NOTIFY statement in the SQL standard. ๐คทโโ๏ธ it's a PostgreSQL original, iykyk.
โจ SEE ALSO โจ
LISTEN(7), UNLISTEN(7), max_notify_queue_pages
and that's the whole page bestie!! this was PostgreSQL 18.6, dated 2026, and the page itself is NOTIFY(7). go forth and notify responsibly, keep those transactions short, and don't let your queue hit half full. mwah ๐โจ๐
๐ฆ 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