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 ¶
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 ¶
New returns a Notifier over db, the same pool the sqlstore client uses (or any pool on the same Postgres database).