アプリケーション連携のためのアドバイザリロック
PostgreSQLのアドバイザリロックを、テーブル行をロックせずにジョブの調整やクリティカルセクションの保護を行う、軽量でプログラマーが制御できるミューテックスとして利用する方法を学びます。
「アプリケーション連携のためのアドバイザリロック」はCoddyKit上の無料PostgreSQL Performance & Query Optimizationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPostgreSQL Performance & Query Optimization学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What Are Advisory Locks?
Advisory locks are locks whose meaning is defined entirely by your application. PostgreSQL does not associate them with any row or table — your code decides what a given lock key represents.
Why Use Them?
They are perfect for coordination that does not map to a single row:
- Ensuring only one worker runs a nightly job
- Serializing access to an external API
- Preventing two processes from importing the same file
Lock Keys
An advisory lock is identified by a 64-bit integer, or by two 32-bit integers. You pick the numbers; a common pattern is to hash a string into an integer key.
SELECT hashtext('nightly-report');Session-Level Locks
pg_advisory_lock holds the lock until you release it or the session ends. It blocks if another session already holds the same key.
SELECT pg_advisory_lock(42);
-- do exclusive work
SELECT pg_advisory_unlock(42);Try-Lock Without Blocking
Often you do not want to wait. pg_try_advisory_lock returns true if it got the lock and false immediately if someone else holds it.
SELECT pg_try_advisory_lock(42);The Single-Worker Pattern
A cron-triggered job uses try-lock to ensure only one instance runs. If it returns false, this instance simply exits.
-- if this returns false, another worker is running
SELECT pg_try_advisory_lock(hashtext('nightly-report'));Transaction-Level Locks
The _xact_ variants release automatically at COMMIT or ROLLBACK, so you cannot forget to unlock. Great for short critical sections.
BEGIN;
SELECT pg_advisory_xact_lock(42);
-- protected work here
COMMIT; -- lock auto-releasedInspecting Held Locks
Advisory locks show up in pg_locks with locktype 'advisory'. Use it to debug who is holding what.
SELECT pid, classid, objid, granted
FROM pg_locks
WHERE locktype = 'advisory';Advisory vs Row Locks
Key differences from SELECT ... FOR UPDATE:
- Advisory locks are not tied to any row
- They survive across statements without holding row versions
- Their meaning is purely a convention your app agrees on
Pitfalls to Avoid
Watch out for:
- Forgetting to unlock session-level locks (prefer xact variants)
- Key collisions between unrelated features — namespace your keys
- Connection poolers reusing sessions and leaking locks
Unlocking Session Locks Cleanly
Session-level locks can be acquired multiple times and must be unlocked the same number of times. To clear everything held by the current session at once, use the unlock-all helper.
SELECT pg_advisory_unlock_all();Quick Check
Test your advisory lock knowledge.
Recap
You learned advisory locks:
- Application-defined locks not tied to rows or tables
- Identified by 64-bit or paired 32-bit keys
- Session-level vs auto-releasing transaction-level variants
pg_try_advisory_lockpowers single-worker patterns- Namespace keys and prefer xact locks to avoid leaks
よくある質問
「アプリケーション連携のためのアドバイザリロック」レッスンは無料ですか?
はい。「アプリケーション連携のためのアドバイザリロック」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、PostgreSQL Performance & Query Optimizationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 PostgreSQL Performance & Query Optimizationコースには全4レッスンが含まれています。
「アプリケーション連携のためのアドバイザリロック」で何を学びますか?
PostgreSQLのアドバイザリロックを、テーブル行をロックせずにジョブの調整やクリティカルセクションの保護を行う、軽量でプログラマーが制御できるミューテックスとして利用する方法を学びます。 ブラウザで直接実行するハンズオンコードでPostgreSQL Performance & Query Optimizationを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
PostgreSQL Performance & Query Optimizationを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのPostgreSQL Performance & Query Optimizationは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「アプリケーション連携のためのアドバイザリロック」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このPostgreSQL Performance & Query Optimizationレッスンでコードを書いて実行できますか?
はい。すべてのPostgreSQL Performance & Query Optimizationレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ロックとデッドロックを理解する
- ロック競合の特定と解消
- 行レベルロック戦略
- アプリケーション連携のためのアドバイザリロック