HeadlinesBriefing favicon HeadlinesBriefing.com

Multigres Scales Postgres LISTEN/NOTIFY

Hacker News •
×

Postgres's LISTEN/NOTIFY mechanism, a publish/subscribe system, suffers from significant performance degradation as the number of listening clients increases. Multigres addresses this by decoupling client connections from persistent Postgres sessions, allowing for connection pooling without sacrificing notification speed.

The core innovation involves a dedicated, long-lived listener connection within the Multigres pooler. This connection, acquired lazily and reserved indefinitely, handles LISTEN/UNLISTEN commands on behalf of all connected clients. Channel subscriptions are managed via a reference count, ensuring only one actual backend subscription per distinct channel. To manage simultaneous listening and notification reception, the listener connection employs a split read/write model.

When a NOTIFY command is issued, Multigres routes it to a specific backend connection. Postgres then delivers the notification to all sessions listening on that channel, including Multigres's shared listener. This notification is then fanned out by Multigres through a gateway to individual client connections. To maintain compatibility, Multigres also handles transactional LISTEN/UNLISTEN commands by buffering them and applying net changes at transaction commit or rollback.