NOTIFY(7)
π NOTIFY(7) β the PostgreSQL 14.24 glow-up
okay bestie so picture this: you're deep in the PostgreSQL 14.24 docs and you find the cutest little interprocess-communication baddie. that's NOTIFY. she's giving main character energy, she's lowkey the group chat of your database, and we're gonna learn her together. no cap. π«Ά
β¨ NAME β¨
NOTIFY β generate a notification. that's it. that's the vibe. π
π SYNOPSIS π
NOTIFY channel [ , payload ]
πΈ DESCRIPTION πΈ
okay so NOTIFY sends a notification event β plus an optional "payload" string if you feel like it β to every client app that already ran LISTEN channel for that channel name in the current database. and the notifications? visible to all users. everybody sees it. it's public. iconic behavior honestly. β¨
NOTIFY is literally a simple interprocess communication mechanism for a bunch of processes all accessing the same PostgreSQL database. you can send a payload string along with the notification, and if you want structured data, you can build higher-level mechanisms on top by using tables in the database to pass extra data from the notifier to the listener(s). very clever. very demure.
so what actually gets passed to the client for a notification event? three things, bestie:
- 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 π«
now here's the thing: it's totally up to the database designer to define which channel names get used in a given database and what each one means. nobody's gonna stop you. 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 ngl, no such association is enforced by NOTIFY and LISTEN β they do not care. 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. your db, your rules. π
when NOTIFY is used to signal that changes happened to a particular table, there's a genuinely useful programming technique: put the NOTIFY in a statement trigger that's triggered by table updates. that way notification happens automatically when the table changes, and the application programmer literally cannot accidentally forget to do it. no more "oops I forgor" bugs. we love a self-care system. π§
π₯ the transaction lore (still DESCRIPTION, but it deserves its own moment) π₯
NOTIFY interacts with SQL transactions in some important ways, and you need to know this, fr fr:
- 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. π - 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 is: if a notification were delivered within a transaction that later got aborted, you'd want the notification to be undone somehow β but the server literally 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. short transactions = happy vibes. long transactions = chaos. periodt.
π¦ duplicates & ordering (also DESCRIPTION, she's a long one) π¦
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. efficiency slay. β¨
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 don't worry about your faves getting squished together.
and the ordering guarantees? 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. the universe is ordered and it's beautiful. π
π«Ά the "am I talking to myself" situation π«Ά
it is super common for a client that executes NOTIFY to 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. which isβ¦ a lot, frankly. 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. π
good news: you can avoid that extra work by noticing 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. self-awareness is free. πͺ
π PARAMETERS π
channelβ name of the notification channel to be signaled (any identifier). pick your fave, it just has to be an identifier.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, the move is to put it in a database table and send the key of the record. work smarter, not harder. π§ )
β NOTES β
there is a queue holding 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. she's thicc. she's fine.
BUT. 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 cleanup can proceed. basically somebody's holding the door open and everybody's waiting. πͺ
the function pg_notification_queue_usage returns the fraction of the queue currently occupied by pending notifications. see Section 9.26 for more information. π
π¨ a transaction that has executed NOTIFY cannot be prepared for two-phase commit. that's a hard no, bestie.
π§ 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. dynamic girlies, this one's for you. π
π EXAMPLES π
configure and execute a listen/notify sequence from psql β here's the whole cute little run:
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 her go. πβ¨
πΊ COMPATIBILITY πΊ
there is no NOTIFY statement in the SQL standard. she's built different. she's not like other statements. π
π SEE ALSO π
LISTEN(7), UNLISTEN(7) β the rest of the trio, go read them next, iykyk π
mwah π that's the page! brought to you with love from PostgreSQL 14.24, dated 2026, and yes queen, the page is literally called NOTIFY(7) β now go send some notifications bestie π¦β¨π
π¦ 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