# Pub/Sub and keyspace events

> Channels, patterns, sharded Pub/Sub and keyspace notifications.

---

LLMS index: [llms.txt](/llms.txt)

---

`pgredis` implements Redis Pub/Sub channels (`SUBSCRIBE`, `UNSUBSCRIBE`, `PUBLISH`), pattern subscriptions (`PSUBSCRIBE`, `PUNSUBSCRIBE`), standalone sharded Pub/Sub (`SSUBSCRIBE`, `SUNSUBSCRIBE`, `SPUBLISH`), introspection (`PUBSUB CHANNELS`, `PUBSUB NUMSUB`, `PUBSUB NUMPAT`, `PUBSUB SHARDCHANNELS`, `PUBSUB SHARDNUMSUB`), and keyspace notifications (`notify-keyspace-events`).

## Subscriptions and transactional delivery {#lifecycle}

- **Commit-ordered registration:** The gateway confirms `SUBSCRIBE`, `PSUBSCRIBE`, or `SSUBSCRIBE` only after PostgreSQL commits the subscription registration in `redis._pubsub_session`. Any `PUBLISH` or `SPUBLISH` committed afterwards reaches the subscriber.
- **Transactional publishing:** Because `PUBLISH` inserts into `redis._pubsub_event` inside the publisher's PostgreSQL transaction, rolling back a SQL transaction or aborting `EXEC` discards any messages published inside that transaction. Publishing the same payload twice in committed transactions delivers two distinct messages.
- **Sharded Pub/Sub scope:** `SSUBSCRIBE`, `SUNSUBSCRIBE`, and `SPUBLISH` operate within the standalone PostgreSQL database; they do not route messages across a Redis Cluster.
- **SQL session API:** SQL callers use [`redis.pubsub_open`, `redis.pubsub_change`, `redis.pubsub_poll`, and `redis.pubsub_cancel`](/docs/redis/reference/sql/#sessions). Commit the subscription transaction before publishing from another connection. `pubsub_poll` deletes delivered messages in its own transaction, and a rollback restores them.

## Keyspace notifications {#events}

`CONFIG SET notify-keyspace-events <flags>` enables keyspace (`__keyspace@<db>__:<key>`) and keyevent (`__keyevent@<db>__:<event>`) notifications across all 16 logical Redis databases in the current PostgreSQL database (requires `redis._admin()` capability):

- Changes to `notify-keyspace-events` and emitted notifications participate in SQL transactions and `MULTI`/`EXEC` commit or rollback.
- Notifications reflect committed database mutations; they do not prove that a subscriber socket finished reading the frame.
- When a key with a TTL expires, read queries hide the key immediately at `expires_at <= clock_timestamp()`, while the `expired` keyspace notification is emitted when either a foreground command or the background maintenance worker physically deletes the expired row. See [Expiry and session lifecycle](/docs/redis/architecture/lifecycle/#expiry).

## Backpressure and session limits {#backpressure}

Pub/Sub sessions and queued messages in `redis._pubsub_session` and `redis._pubsub_event` are bounded by admission limits:

{.wide}
| Resource | Default limit |
| --- | ---: |
| Session lease TTL | `120` seconds |
| Queued message payload per client | `1 MiB` |
| Queued message frames per client | `1,024` |
| Total retained Pub/Sub payload across sessions | `64 MiB` |
| Active Pub/Sub sessions | `4,096` |
| Total subscription registrations | `16,384` |

Additionally, `CONFIG SET client-output-buffer-limit` enforces `pubsub` class hard and soft buffer limits (default `32 MiB` hard, `8 MiB` soft for `60` seconds; see [Configuration](/docs/redis/reference/configuration/#client-output)). When a slow or stalled subscriber exceeds its queue or output buffer limit, `pg_redis` marks the route failed, logs a `pg_redis Pub/Sub route failure decision` entry in the PostgreSQL server log, and the gateway closes the client connection. Disconnected clients must reconnect and re-subscribe; Pub/Sub subscriptions and unread queues are transient session state and are not restored across server restarts or `pg_dump` backups.

---

Backlinks:

- [Reference](/docs/redis/reference/)
