- Mastering PostgreSQL 10
- Hans Jürgen Sch?nig
- 247字
- 2021-06-30 19:03:51
Utilizing advisory locks
PostgreSQL has a highly efficient and sophisticated transaction machinery that is capable of handling locks in a really fine-grained and efficient way. Some years ago, some people came up with the idea of using this code to synchronize applications with each other.
Thus, advisory locks were born.
When using advisory locks, it is important to mention that they won't go away on COMMIT as normal locks do. Therefore, it is really important to make sure that unlocking is done properly and in a totally reliable way.
If you decide to use an advisory lock, what you really lock is a number. So, this is not about rows or data; it is really just a number. Here is how it works:

The first transaction will lock 15. The second transaction has to wait until this number has been unlocked again. The second session will even wait after the first one has committed. This is highly important as you cannot rely on the fact that the end of the transaction will nicely and miraculously solve things for you.
If you want to unlock all locked numbers, PostgreSQL offers the pg_advisory_unlock_all() function to do exactly this:
test=# SELECT pg_advisory_unlock_all(); pg_advisory_unlock_all
------------------------
(1 row)
Sometimes, you might want to see if you can get a lock and error-out if it is not possible. To achieve this, PostgreSQL offers a couple of function, to see a list of all such available functions, at the command line, enter: df *try*advisory*.
- ArchiCAD 19:The Definitive Guide
- 工業機器人產品應用實戰
- 快學Flash動畫百例
- 自動化控制工程設計
- 大學計算機應用基礎
- STM32嵌入式微控制器快速上手
- 21天學通C語言
- Dreamweaver CS6精彩網頁制作與網站建設
- 簡明學中文版Photoshop
- 未來學徒:讀懂人工智能飛馳時代
- Unreal Development Kit Game Design Cookbook
- Natural Language Processing and Computational Linguistics
- 企業級Web開發實戰
- 西門子S7-1200/1500 PLC從入門到精通
- 項目實踐精解:C#核心技術應用開發