Advisory lock là khóa do chính app định nghĩa bằng một key dạng số, không gắn với dòng hay bảng nào cả — database chỉ giữ giúp cái khóa, còn ý nghĩa là do bạn quy ước.
Hợp cho "chỉ một tiến trình được chạy tại một thời điểm" nhẹ nhàng, ví dụ đảm bảo job tính tiền của mỗi tenant chỉ chạy một bản.
SELECT pg_advisory_xact_lock(hashtext('tenant:42:billing'));Dùng cẩn thận: vì database không hiểu "vật thể nghiệp vụ" sau cái khóa, nên quy ước cách sinh key phải thống nhất toàn hệ thống, và đừng giữ khóa quá lâu.
An advisory lock is a lock your app defines with a numeric key, not tied to any row or table — the database just holds the lock for you; the meaning is your convention.
It fits lightweight "only one process at a time", e.g. ensuring each tenant's billing job runs only once.
SELECT pg_advisory_xact_lock(hashtext('tenant:42:billing'));Use carefully: since the database doesn't understand the "business object" behind the lock, the key-generation convention must be consistent across the whole system, and don't hold the lock too long.