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

กลยุทธ์การล็อกระดับแถว

สำรวจกลยุทธ์ขั้นสูงสำหรับจัดการล็อกระดับแถว เพื่อเพิ่มประสิทธิภาพการเขียนข้อมูลพร้อมกัน

บทเรียน 3 จาก 412 ขั้นตอน

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Row-Level Locking?

When multiple users or processes try to change the same data at the same time, databases need a way to prevent conflicts and ensure data integrity. This is where row-level locking comes in.

A row-level lock allows a transaction to claim exclusive or shared access to specific rows, preventing other transactions from making conflicting changes until the lock is released. It's crucial for high-concurrency applications.

Implicit Row Locks

PostgreSQL automatically applies row-level locks during Data Manipulation Language (DML) operations like INSERT, UPDATE, and DELETE.

  • INSERT: Places an exclusive lock on the newly inserted row.
  • UPDATE: Places an exclusive lock on the row being modified.
  • DELETE: Places an exclusive lock on the row being deleted.

These implicit locks ensure that only one transaction can modify a specific row at a time.

Explicit Locks: FOR UPDATE

Sometimes you need to lock rows before modifying them, especially when your application logic involves reading data, making decisions, and then updating. This is where SELECT ... FOR UPDATE is invaluable.

It acquires an exclusive lock on the selected rows, preventing other transactions from updating or deleting them until your transaction commits or rolls back.

FOR UPDATE in Action

Try this example. If you run SELECT ... FOR UPDATE in one database session, then try to UPDATE the same row from another session, the second session will wait.

Session 1:

BEGIN;
SELECT * FROM products WHERE product_id = 1 FOR UPDATE;
-- Do some work...
-- UPDATE products SET stock = stock - 1 WHERE product_id = 1;
-- ROLLBACK; OR COMMIT;

FOR UPDATE: What Happens

The previous code snippet shows how FOR UPDATE works. If you ran the SELECT in Session 1, then immediately tried to run this UPDATE in a different Session 2, Session 2 would wait until Session 1 either COMMITs or ROLLBACKs.

Session 2 (will wait):

UPDATE products SET price = 10.99 WHERE product_id = 1;

Explicit Locks: FOR SHARE

What if you want to prevent updates, but allow other transactions to read the data or even acquire their own shared lock?

SELECT ... FOR SHARE acquires a shared lock. This means:

  • Other transactions can read the rows.
  • Other transactions can acquire their own FOR SHARE locks.
  • Other transactions cannot acquire FOR UPDATE locks or modify the rows.

FOR SHARE in Action

If Session 1 holds a FOR SHARE lock, Session 2 can also acquire a FOR SHARE lock, but a FOR UPDATE or DML operation on the same row will wait.

Session 1:

BEGIN;
SELECT * FROM orders WHERE order_id = 101 FOR SHARE;
-- Do some calculations...
-- COMMIT; OR ROLLBACK;

More Granular Locks: FOR NO KEY UPDATE

SELECT ... FOR NO KEY UPDATE is similar to FOR UPDATE but is weaker. It acquires an exclusive lock that doesn't block FOR KEY SHARE locks.

It's useful when you're updating non-key columns and don't need to prevent concurrent foreign key operations, which are typically very short-lived.

Shared Read Locks: FOR KEY SHARE

SELECT ... FOR KEY SHARE is the weakest explicit row-level lock. It allows other transactions to acquire FOR SHARE, FOR NO KEY UPDATE, and even other FOR KEY SHARE locks.

It primarily prevents other transactions from deleting the locked rows or acquiring an exclusive lock that would modify key columns. It's often used by foreign key constraints.

Locking Order Strategy

A critical strategy to prevent deadlocks (where two transactions wait for each other indefinitely) is to always acquire locks on multiple rows in a consistent order.

For example, if you need to lock rows with product_id = 5 and product_id = 10, always lock 5 first, then 10 across all transactions. This prevents a scenario where one transaction locks 5 then tries for 10, while another locks 10 then tries for 5.

Quick Check: Row Locks

Consider two concurrent transactions. Transaction A runs SELECT * FROM users WHERE user_id = 1 FOR UPDATE;. What happens if Transaction B immediately tries to run UPDATE users SET email = 'new@example.com' WHERE user_id = 1;?

Recap: Row-Level Locks

We've explored how PostgreSQL manages concurrency with row-level locks:

  • Implicit locks protect DML operations.
  • FOR UPDATE provides exclusive row locks for modifications.
  • FOR SHARE provides shared locks, allowing reads but blocking updates.
  • FOR NO KEY UPDATE and FOR KEY SHARE offer more granular control.
  • Consistently ordering lock acquisition is a key strategy to prevent deadlocks.

Mastering these strategies ensures your application handles concurrent writes efficiently and reliably!

เริ่มต้นได้ฟรี

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

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

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

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

บทเรียน “กลยุทธ์การล็อกระดับแถว” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “กลยุทธ์การล็อกระดับแถว”

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

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

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

บทเรียน “กลยุทธ์การล็อกระดับแถว” ใช้เวลานานแค่ไหน

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

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

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

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

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