Pub/Sub and keyspace events
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
- Commit-ordered registration: The gateway confirms
SUBSCRIBE,PSUBSCRIBE, orSSUBSCRIBEonly after PostgreSQL commits the subscription registration inredis._pubsub_session. AnyPUBLISHorSPUBLISHcommitted afterwards reaches the subscriber. - Transactional publishing: Because
PUBLISHinserts intoredis._pubsub_eventinside the publisher’s PostgreSQL transaction, rolling back a SQL transaction or abortingEXECdiscards any messages published inside that transaction. Publishing the same payload twice in committed transactions delivers two distinct messages. - Sharded Pub/Sub scope:
SSUBSCRIBE,SUNSUBSCRIBE, andSPUBLISHoperate 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, andredis.pubsub_cancel. Commit the subscription transaction before publishing from another connection.pubsub_polldeletes delivered messages in its own transaction, and a rollback restores them.
Keyspace notifications
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-eventsand emitted notifications participate in SQL transactions andMULTI/EXECcommit 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 theexpiredkeyspace notification is emitted when either a foreground command or the background maintenance worker physically deletes the expired row. See Expiry and session lifecycle.
Backpressure and session limits
Pub/Sub sessions and queued messages in redis._pubsub_session and redis._pubsub_event are bounded by admission limits:
| 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). 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.
