pgnotify

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package pgnotify is a sqlstore.Notifier on Postgres LISTEN/NOTIFY: an enqueue on one machine wakes idle workers on every other machine at once, instead of at their next poll.

db, _ := driverpgx.Open(dsn)
c, _ := sqlstore.New(db, sqlb.PostgresDialect{}, sqlstore.WithNotifier(pgnotify.New(db)))

Notify is a plain pg_notify statement and works through any Postgres driver. Listen needs the pgx stdlib driver (github.com/jackc/pgx/v5/stdlib, which driverpgx.Open uses), because database/sql has no way to wait for a notification; on any other driver Listen returns ErrDriver and the client keeps polling.

Listen holds one pool connection for as long as the client runs, so the pool needs one connection more than the client's workers use. That connection is discarded, never returned to the pool, when Listen ends, so no other query ever runs on a connection that is still listening.

Behind a transaction-mode connection pooler (PgBouncer in transaction or statement mode) LISTEN does not work; point the pool at Postgres itself or leave the notifier off.

Index

Constants

This section is empty.

Variables

View Source
var ErrDriver = errors.New("pgnotify: Listen needs a pool opened with the pgx stdlib driver")

ErrDriver is returned by Listen when the pool's driver is not the pgx stdlib driver, which is the only one that exposes notifications.

Functions

This section is empty.

Types

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

Notifier sends and receives wake-ups over one Postgres pool. Build it with New; it holds no state besides the pool.

func New

func New(db *sql.DB) *Notifier

New returns a Notifier over db, the same pool the sqlstore client uses (or any pool on the same Postgres database).

func (*Notifier) Listen

func (n *Notifier) Listen(ctx context.Context, channel string, wake func()) error

Listen takes a pool connection, runs LISTEN on channel, calls wake once (to cover anything sent before the subscription) and then once per notification, until ctx ends (it returns nil) or the connection fails.

func (*Notifier) Notify

func (n *Notifier) Notify(ctx context.Context, channel string) error

Notify sends an empty notification on channel. Postgres delivers it to every session listening on the channel, this one's listener included.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL