- 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*.
- 自動(dòng)控制工程設(shè)計(jì)入門
- ETL with Azure Cookbook
- Managing Mission:Critical Domains and DNS
- 并行數(shù)據(jù)挖掘及性能優(yōu)化:關(guān)聯(lián)規(guī)則與數(shù)據(jù)相關(guān)性分析
- 計(jì)算機(jī)控制技術(shù)
- 來吧!帶你玩轉(zhuǎn)Excel VBA
- Java開發(fā)技術(shù)全程指南
- PHP開發(fā)手冊
- 新編計(jì)算機(jī)圖形學(xué)
- 網(wǎng)絡(luò)服務(wù)搭建、配置與管理大全(Linux版)
- 與人共融機(jī)器人的關(guān)節(jié)力矩測量技術(shù)
- 工業(yè)機(jī)器人操作
- Mastering MongoDB 4.x
- 局域網(wǎng)組建與使用完全自學(xué)手冊
- 亮劍.NET:圖解ASP.NET網(wǎng)站開發(fā)實(shí)戰(zhàn)