Add x-use-transaction mode to postgres driver for pg-proxy compatibility#1364
Closed
areazus wants to merge 1 commit intogolang-migrate:masterfrom
Closed
Add x-use-transaction mode to postgres driver for pg-proxy compatibility#1364areazus wants to merge 1 commit intogolang-migrate:masterfrom
areazus wants to merge 1 commit intogolang-migrate:masterfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
including proper rollback and cleanup of p.tx on error
wrappers that route through the shared transaction when enabled
use the new wrappers
back by a failed migration), since the lock is already released
Co-Authored-By: Claude Sonnet 4.6 (1M context) noreply@anthropic.com