Skip to content

Add x-use-transaction mode to postgres driver for pg-proxy compatibility#1364

Closed
areazus wants to merge 1 commit intogolang-migrate:masterfrom
DataDog:feat/postgres-use-transaction
Closed

Add x-use-transaction mode to postgres driver for pg-proxy compatibility#1364
areazus wants to merge 1 commit intogolang-migrate:masterfrom
DataDog:feat/postgres-use-transaction

Conversation

@areazus
Copy link
Copy Markdown

@areazus areazus commented Mar 2, 2026

Session-level advisory locks (pg_advisory_lock) are not supported by
pg-proxies such as Orgstore's pg-proxy, which route each statement to a
potentially different backend connection. This makes it impossible to
guarantee that Lock and Unlock execute on the same connection.

Add a new x-use-transaction URL option (UseTransaction config field) that
switches the driver to transaction-scoped advisory locks
(pg_advisory_xact_lock). In this mode, a single transaction spans the
advisory lock acquisition, migration SQL, and version table update, with
the lock released automatically on commit or rollback. This is compatible
with pg-proxy deployments since all statements are issued within one
transaction on a single connection.

Key changes:

  • Add withExistingOrNewTxRollbackOnErr to manage a shared *sql.Tx,
    including proper rollback and cleanup of p.tx on error
  • Add executeExecContext, executeQueryContext, executeQueryRowContext
    wrappers that route through the shared transaction when enabled
  • Update Lock/Unlock, SetVersion, Run, Drop, and ensureVersionTable to
    use the new wrappers
  • Fix transaction leak when SET LOCAL statement_timeout fails on startup
  • Ensure p.tx is cleared after rollback so subsequent calls start fresh
  • Return nil from Unlock when the transaction is already done (rolled
    back by a failed migration), since the lock is already released
  • Update dktesting cleanup client to use API version negotiation
  • Document the feature and its limitations (CONCURRENTLY, VACUUM, etc.)

Co-Authored-By: Claude Sonnet 4.6 (1M context) noreply@anthropic.com

Session-level advisory locks (pg_advisory_lock) are not supported by
pg-proxies such as Orgstore's pg-proxy, which route each statement to a
potentially different backend connection. This makes it impossible to
guarantee that Lock and Unlock execute on the same connection.

Add a new x-use-transaction URL option (UseTransaction config field) that
switches the driver to transaction-scoped advisory locks
(pg_advisory_xact_lock). In this mode, a single transaction spans the
advisory lock acquisition, migration SQL, and version table update, with
the lock released automatically on commit or rollback. This is compatible
with pg-proxy deployments since all statements are issued within one
transaction on a single connection.

Key changes:
- Add withExistingOrNewTxRollbackOnErr to manage a shared *sql.Tx,
  including proper rollback and cleanup of p.tx on error
- Add executeExecContext, executeQueryContext, executeQueryRowContext
  wrappers that route through the shared transaction when enabled
- Update Lock/Unlock, SetVersion, Run, Drop, and ensureVersionTable to
  use the new wrappers
- Fix transaction leak when SET LOCAL statement_timeout fails on startup
- Ensure p.tx is cleared after rollback so subsequent calls start fresh
- Return nil from Unlock when the transaction is already done (rolled
  back by a failed migration), since the lock is already released
- Update dktesting cleanup client to use API version negotiation
- Document the feature and its limitations (CONCURRENTLY, VACUUM, etc.)

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@areazus areazus closed this Mar 2, 2026
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 54.645% (+0.2%) from 54.432%
when pulling 257c1fd on DataDog:feat/postgres-use-transaction
into 257fa84 on golang-migrate:master.

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.

2 participants