---
source: https://iinventedlight.com/library/notes/postgres-listen-notify.md
title: "PostgreSQL LISTEN and NOTIFY"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PostgreSQL License; prose restates cited source lines"
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: PostgreSQL"
tags: ['postgresql', 'sql', 'pubsub', 'notifications']
sources:
  - id: S1
    title: "PostgreSQL: Documentation: 18: NOTIFY"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-notify.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
  - id: S2
    title: "PostgreSQL: Documentation: 18: LISTEN"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-listen.html
    accessed: 2026-11-04
    license: "PostgreSQL License"
---
# 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)

```sql
-- 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).
