Advisory Locks zur Koordination von Anwendungen
Lernen Sie, wie PostgreSQL Advisory Locks Ihrer Anwendung einen schlanken, programmierergesteuerten Mutex zur Koordination von Jobs und zum Schutz kritischer Abschnitte bieten, ohne Tabellenzeilen zu sperren.
Advisory Locks zur Koordination von Anwendungen ist eine kostenlose PostgreSQL Performance & Query Optimization-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des PostgreSQL Performance & Query Optimization-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der PostgreSQL Performance & Query Optimization-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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
Häufig gestellte Fragen
Ist die Lektion „Advisory Locks zur Koordination von Anwendungen“ kostenlos?
Ja — der vollständige Text von „Advisory Locks zur Koordination von Anwendungen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des PostgreSQL Performance & Query Optimization-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der PostgreSQL Performance & Query Optimization-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Advisory Locks zur Koordination von Anwendungen“?
Lernen Sie, wie PostgreSQL Advisory Locks Ihrer Anwendung einen schlanken, programmierergesteuerten Mutex zur Koordination von Jobs und zum Schutz kritischer Abschnitte bieten, ohne Tabellenzeilen zu… Du übst PostgreSQL Performance & Query Optimization mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um PostgreSQL Performance & Query Optimization zu starten?
Keine Vorkenntnisse erforderlich. PostgreSQL Performance & Query Optimization auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Advisory Locks zur Koordination von Anwendungen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser PostgreSQL Performance & Query Optimization-Lektion Code schreiben und ausführen?
Ja. Jede PostgreSQL Performance & Query Optimization-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Sperren und Deadlocks verstehen
- Sperrkonkurrenz erkennen und beheben
- Strategien für Sperren auf Zeilenebene
- Advisory Locks zur Koordination von Anwendungen