Skip to content

Add pgx listener support to database/sql - #1366

Draft
bgentry wants to merge 1 commit into
masterfrom
bg/dbsql-pgx-listener
Draft

Add pgx listener support to database/sql#1366
bgentry wants to merge 1 commit into
masterfrom
bg/dbsql-pgx-listener

Conversation

@bgentry

@bgentry bgentry commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Adds riverdatabasesql.NewWithPgxListener(dbPool, listenerPool). The *sql.DB remains the exclusive executor for River queries and transactions, while a private riverpgxv5 driver uses the separately supplied Pgx pool only to create listeners.

This lets applications built around database/sql, Bun, GORM, or another ORM run one coherently typed River client with Postgres LISTEN/NOTIFY support. Cross-process actions such as cancelling a running job no longer have to wait for the poll interval, while JobCompleteTx continues to operate on an ordinary *sql.Tx. The existing New constructor remains explicitly poll-only, and the new constructor rejects a nil listener pool.

The caller owns both pools and must configure them for the same database and schema. A listener pool dedicated to one River client can generally use MinConns: 0 and MaxConns: 1. Listener connections are hijacked and therefore no longer count against the pool maximum or close with the pool; stopping the River client closes them. PgBouncer must use session pooling or be bypassed for these connections.

This provides a design-aligned solution for #1364 without adding mixed transactional-driver support.

River Pro companion: riverqueue/riverpro#361.

The `database/sql` driver currently operates in poll-only mode because it
cannot acquire the dedicated connection required by Postgres `LISTEN`.
Cross-process actions like cancelling a running job therefore wait for a
poll even when the underlying SQL driver is Pgx.

Add `NewWithPgxListener`, which keeps all query and transaction execution
on the supplied `*sql.DB` while delegating listener creation to a Pgx
driver backed by a separately supplied pool. Preserve `New` as the
poll-only constructor and reject a nil listener pool so configuration
errors cannot silently disable notifications.

Document the database, schema, connection ownership, and pool sizing
requirements. Exercise the hybrid driver through the shared listener
suite, including notification-driven cancellation and transactional job
completion through an ordinary `*sql.Tx`.
@bgentry
bgentry force-pushed the bg/dbsql-pgx-listener branch from 380f3ad to 657ee90 Compare August 27, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant