Skip to content

improvement(db): tag advisory lock statements by caller - #8331

Merged
waleedlatif1 merged 3 commits into
stagingfrom
improvement/tag-advisory-locks
Sep 26, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
improvement/tag-advisory-locks

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Every transaction-scoped advisory lock in apps/sim ran the same statement, SELECT pg_advisory_xact_lock(hashtextextended($1, 0)), so query insights showed all lock waits as one fingerprint with no way to tell which caller was waiting
  • New acquireAdvisoryXactLock(tx, tag, key) and tryAcquireAdvisoryXactLock(tx, tag, key) in lib/db/advisory-locks.ts issue the same call with a trailing SQLCommenter tag (/*lock='<family>'*/), which PlanetScale query tags can filter on. The tag must be a static snake_case identifier, so it can't break out of the comment
  • About 45 call sites now go through the helper, each tagged with its lock family (user_table_rows_pos, organization_membership, usage_log_event, …). Keys, lock types, timeouts, and transaction boundaries are unchanged
  • Left alone: statements whose SQL text is already unique (packages/db locks, the file-search dispatch generate_series claim) and one untyped repair script

Type of Change

  • Improvement

Testing

  • New lib/db/advisory-locks.integration.ts (real Postgres):
    • the same key blocks and releases on commit; a different key doesn't block
    • the try variant returns false while held
    • the tag reaches the server (checked through pg_stat_activity)
    • an unsafe tag is rejected
  • Each test goes red when its guarantee is broken
  • Type-check, lint, check:audits, and unit tests for every changed file pass

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 26, 2026 8:18pm UTC

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 35 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Refactors database lock calls to a shared utility.

The PR appears safe to merge; no outstanding findings remain.

Summary

The PR routes transaction-scoped advisory locks through helpers that tag statements by caller family.

  • Existing lock keys, lock types, and transaction boundaries are preserved.
  • PostgreSQL integration tests cover blocking, non-blocking acquisition, tag visibility, and tag validation.

Reviews (3) · Last reviewed commit: "improvement(db): tag the copilot run adm..."

Comment thread apps/sim/lib/db/advisory-locks.integration.ts Outdated
Comment thread apps/sim/lib/db/advisory-locks.integration.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 35 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 36 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 3945364 into staging Sep 26, 2026
32 checks passed
@waleedlatif1
waleedlatif1 deleted the improvement/tag-advisory-locks branch September 26, 2026 21:29

This branch was previously deployed

1 inactive deployment
Preview — b2331781 Deployed Sep 26, 2026 by vercel[bot]
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