Skip to content

Pub/Sub and keyspace events

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

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, 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. 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

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.

Backpressure and session limits

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

ResourceDefault limit
Session lease TTL120 seconds
Queued message payload per client1 MiB
Queued message frames per client1,024
Total retained Pub/Sub payload across sessions64 MiB
Active Pub/Sub sessions4,096
Total subscription registrations16,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.

Scan to open this page
https://mimus.pgsty.com/docs/redis/reference/pubsub/