31 Mart 2023 Cuma

PostgreSQL Locks

Giriş
Çok ileri seviye bir yazı burada. Yazıda bahsedilen lock çeşitleri şöyle
Table Locks
Row Locks
Page locks
Dead Locks
Advisory Locks

pg_advisory_xact_lock - Mutex Gibidir
İki farklı transaction aynı identifier ile aynı lock'ı isterse sadece bir tanesi geçebilir. 
Açıklaması şöyle
Now, when the data is in Postgres, you can just take the lock in the transaction that writes it. A transaction-scoped advisory lock is released only when that transaction ends. Losing the lock and losing the write become one event, because Postgres transactions are atomic. Tada!

Use pg_advisory_xact_lock (not the session level pg_advisory_lock). Postgres uses idle_in_transaction_session_timeout acting as the TTL, and will rollback the hung transaction.

BEGIN;
SELECT pg_advisory_xact_lock(:booking_key);
INSERT INTO bookings ...;
COMMIT;
Table Locks
Bunlar şöyle
1. ACCESS EXCLUSIVE
2. ACCESS SHARE
3. EXCLUSIVE
4. ROW SHARE
5. ROW EXCLUSIVE
6. SHARE ROW EXCLUSIVE
7. SHARE
8. SHARE UPDATE EXCLUSIVE

Hiç yorum yok:

Yorum Gönder