NOTIFY(7)

πŸ“¦ version 13.23, in opensuse-leap 16.0

πŸ’Œ NOTIFY(7) β€” the manual page, but make it make sense

okay bestie, so this is the NOTIFY page from the PostgreSQL 13.23 docs. yes, the whole thing. grab a strawberry πŸ“ and let's go.


✨ NAME ✨

NOTIFY β€” generate a notification πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

NOTIFY channel [ , payload ]

πŸ¦‹ DESCRIPTION πŸ¦‹

so like. the NOTIFY command sends a notification event β€” plus an optional payload string if you feel like it β€” to every client application that previously ran LISTEN channel for that specific channel name, in the current database. and the notifications? visible to all users. no gatekeeping. πŸ’–

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 higher-level mechanisms for passing structured data, you can build those by using tables in the database to pass additional 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
  • the payload string β€” which is an empty string if you didn't specify one

it's up to the database designer to define the channel names used in a given database and what each one means. commonly, the channel name matches 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 no such association is enforced by the NOTIFY and LISTEN commands, just so we're clear. 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. flexible queen energy πŸ‘‘

when NOTIFY is used to signal that changes happened to a particular table, a genuinely useful programming technique is to put the NOTIFY in a statement trigger that's triggered by table updates. that way notification happens automatically when the table is changed, and the application programmer literally cannot accidentally forget to do it. ate. πŸ”₯

NOTIFY interacts with SQL transactions in some important ways, so pay attention bestie:

  1. if a NOTIFY is executed inside a transaction, the notify events are not delivered until and unless the transaction is committed. this is appropriate, because if the transaction is aborted, all the commands within it had no effect β€” including NOTIFY. but it can be disconcerting if you're expecting notifications to arrive immediately. 😭
  2. if a listening session receives a notification signal while it's within a transaction, the notification event won't 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 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. periodt.

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's also guaranteed that messages from different transactions are delivered in the order in which the transactions committed. no cap. 🫢

it's 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. 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 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 your own work bouncing back, and you can ignore it. main character energy, honestly.


🌸 PARAMETERS 🌸

  • channel β€” name of the notification channel to be signaled (any 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 communicate 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. trust. πŸ’œ)

⭐ 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 cleanup can proceed. be a good citizen, bestie. 🧚

the function pg_notification_queue_usage returns the fraction of the queue that is currently occupied by pending notifications. see Section 9.26 for more information. πŸ‘‘

⚠️ heads up: a transaction that has executed NOTIFY cannot be prepared for two-phase commit.

πŸŽ€ 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. slay. πŸ’…


πŸ“ 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. iconic. 🌈


πŸ’€ COMPATIBILITY πŸ’€

there is no NOTIFY statement in the SQL standard. it's giving PostgreSQL exclusive. πŸ™ˆ


πŸ’– SEE ALSO πŸ’–

LISTEN(7), UNLISTEN(7)


and that's the whole page, bestie β€” no skipped sections, no vibe casualties. this was PostgreSQL 13.23, dated 2025, and the page is NOTIFY(7). go forth and notify responsibly. mwah. πŸ’‹βœ¨

πŸ“¦ where this page comes from