PostgreSQL Performance & Query Optimization · บทเรียน

ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน

เรียนรู้ว่าล็อกคำแนะนำของ PostgreSQL มอบ mutex น้ำหนักเบาที่โปรแกรมควบคุมได้ให้แอปพลิเคชัน เพื่อประสานงานงานและปกป้องส่วนสำคัญโดยไม่ต้องล็อกแถวในตารางได้อย่างไร

บทเรียน 4 จาก 413 ขั้นตอน

ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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-released

Inspecting 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_lock powers single-worker patterns
  • Namespace keys and prefer xact locks to avoid leaks
เริ่มต้นได้ฟรี

เรียนรู้ SQL ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
22
บทเรียน
88

คำถามที่พบบ่อย

บทเรียน “ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน”

เรียนรู้ว่าล็อกคำแนะนำของ PostgreSQL มอบ mutex น้ำหนักเบาที่โปรแกรมควบคุมได้ให้แอปพลิเคชัน เพื่อประสานงานงานและปกป้องส่วนสำคัญโดยไม่ต้องล็อกแถวในตารางได้อย่างไร คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม

ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจล็อกและเดดล็อก
  2. การระบุและแก้ไขการแย่งใช้ล็อก
  3. กลยุทธ์การล็อกระดับแถว
  4. ล็อกคำแนะนำสำหรับการประสานงานระหว่างแอปพลิเคชัน
← กลับไปที่ PostgreSQL Performance & Query Optimization