NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+1 view

Have you heard of Advisory Locks in PostgreSQL?

Practice

AI answer

Yes. Advisory locks in PostgreSQL are application-level locks for which the application chooses the key and defines the meaning of the resource being locked; the database does not automatically associate them with rows or tables. There are transaction-level variants (pg_advisory_xact_lock) and session-level variants (pg_advisory_lock). Transaction-level locks are released when the transaction ends, while session-level locks must be explicitly released or the connection must be closed. They can be used, for example, to prevent the same task from running in parallel. It is important that all participants use the same key space and that the lock is held on the same connection; long-running tasks also require timeouts and failure handling.