Skip to content

fix(sql): restore advisory lock manager state on read-committed statement retry - #173217

Open
waterWang wants to merge 1 commit into
cockroachdb:masterfrom
waterWang:fix-advisory-lock-rc-retry
Open

fix(sql): restore advisory lock manager state on read-committed statement retry#173217
waterWang wants to merge 1 commit into
cockroachdb:masterfrom
waterWang:fix-advisory-lock-rc-retry

Conversation

@waterWang

Copy link
Copy Markdown

Fixes: #173169

The read-committed per-statement retry loop in dispatchReadCommittedStmtToExecutionEngine creates and rolls back its savepoint directly at the KV layer without notifying the advisory lock manager. The manager hooks are only wired into the SQL SAVEPOINT statements.

When a statement acquires a pg_advisory_xact_lock and then hits a retryable error in the same statement, KV correctly rolls back the lock but the manager's stack entry survives. If the retry does not re-acquire the same lock, pg_locks reports a granted advisory lock the session does not hold for the rest of the transaction.

This fix:

  1. Captures a snapshot of the advisory lock manager state alongside the KV savepoint creation (using the existing ExportRewindSnapshot primitive).
  2. Restores the snapshot after each read-committed statement retry rollback (using ApplyRewindSnapshot), keeping pg_locks consistent with KV.

The existing rewind primitives (ExportRewindSnapshot / ApplyRewindSnapshot in pkg/sql/advisorylock/manager.go) are reused as suggested in the issue. OnSQLRollbackToSavepoint cannot be used directly since the read-committed savepoint is not a SQL savepoint.

@blathers-crl

blathers-crl Bot commented Aug 8, 2026

Copy link
Copy Markdown

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

Thank you for contributing to CockroachDB. Please ensure you have followed the guidelines for creating a PR.

Before a member of our team reviews your PR, I have some potential action items for you:

  • Please ensure your git commit message contains a release note.
  • When CI has completed, please ensure no errors have appeared.

I have added a few people who may be able to assist in reviewing:

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl blathers-crl Bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Aug 8, 2026
@blathers-crl
blathers-crl Bot requested a review from spilchen August 8, 2026 17:09
@cockroachlabs-cla-agent

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-community Originated from the community X-blathers-triaged blathers was able to find an owner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: stale advisory lock tracking after read-committed statement retry

1 participant