PostgreSQL LISTEN and NOTIFY
Answer: The NOTIFY command sends a notification event together with an optional "payload" string to each client application that has previously executed LISTEN channel for the specified channel name in the current database. (S1)
-- session A
LISTEN jobs;
-- session B
NOTIFY jobs, 'job 42 ready';
SELECT pg_notify('jobs', 'job 43 ready'); -- function form
Session A then receives an asynchronous notification with the payload and the sender's PID.
Behavior
- LISTEN takes effect at commit; if the transaction rolls back, listening is unchanged.
- A session's listen registrations are automatically cleared when the session ends. (S2)
- A transaction that ran LISTEN or NOTIFY cannot be prepared for two-phase commit.
pg_notification_queue_usage()shows how full the pending queue is.
Caveats
When NOTIFY is used to signal the occurrence of changes to a particular table, a useful programming technique is to put the NOTIFY in a statement trigger that is triggered by table updates. (S1)
Claims
- id: C1 claim: pg_notification_queue_usage returns the fraction of the queue occupied. source: S1 evidence: "The function pg_notification_queue_usage returns the fraction of the queue"
- id: C2 claim: A transaction that executed NOTIFY cannot be prepared for two-phase commit. source: S1 evidence: "A transaction that has executed NOTIFY cannot be prepared for two-phase commit."
- id: C3 claim: LISTEN takes effect at transaction commit. source: S2 evidence: "LISTEN takes effect at transaction commit."
- id: C4 claim: A session's listen registrations are cleared when the session ends. source: S2 evidence: "A session's listen registrations are automatically cleared when the session ends."
Sources
- [S1] PostgreSQL: Documentation: 18: NOTIFY. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-notify.html . License: PostgreSQL License. Accessed 2026-11-04.
- [S2] PostgreSQL: Documentation: 18: LISTEN. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-listen.html . License: PostgreSQL License. Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: NOTIFY, PostgreSQL License; PostgreSQL Global Development Group, PostgreSQL: Documentation: 18: LISTEN, PostgreSQL License.
Cite this
iinventedlight. 2026. "PostgreSQL LISTEN and NOTIFY." iinventedlight Library, iil:note:2xr2w01h, version of 2026-11-04. https://iinventedlight.com/library/notes/postgres-listen-notify/. License: CC-BY-4.0.
@misc{iil_note_2xr2w01h,
author = {{iinventedlight}},
title = {{PostgreSQL LISTEN and NOTIFY}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/postgres-listen-notify/},
note = {Permanent ID iil:note:2xr2w01h. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/postgres-listen-notify.md.