久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么理解PostgreSQL Locks中的Fast Path Locking

146次閱讀
沒有評論

共計 6867 個字符,預計需要花費 18 分鐘才能閱讀完成。

這篇文章主要講解了“怎么理解 PostgreSQL Locks 中的 Fast Path Locking”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著丸趣 TV 小編的思路慢慢深入,一起來研究和學習“怎么理解 PostgreSQL Locks 中的 Fast Path Locking”吧!

PG 提供的系統表 pg_locks 中有一個字段:fastpath, 用以表示是否 fastpath, 那 fastpath 指的是什么呢?

一、Fast Path Locking

Fast Path Locking
-----------------
Fast path locking is a special purpose mechanism designed to reduce the
overhead of taking and releasing certain types of locks which are taken
and released very frequently but rarely conflict. Currently, this includes
two categories of locks:
Fast path locking 用以減少那些需要經常獲取和釋放但又很少出現沖突的鎖類型的獲取 / 釋放負載.
當前的做法, 包括 2 種類型 (category) 的鎖:
(1) Weak relation locks. SELECT, INSERT, UPDATE, and DELETE must acquire a
lock on every relation they operate on, as well as various system catalogs
that can be used internally. Many DML operations can proceed in parallel
against the same table at the same time; only DDL operations such as
CLUSTER, ALTER TABLE, or DROP -- or explicit user action such as LOCK TABLE
-- will create lock conflicts with the  weak  locks (AccessShareLock,
RowShareLock, RowExclusiveLock) acquired by DML operations.
(1)弱關系鎖.SELECT,INSERT,UPDATE 和 DELETE 必須在 relation 上獲取鎖,
這些 relation 是在內部使用的各種系統目錄(數據字典).
許多 DML 操作可同一時間在同一個表上進行并行操作; 只有 DDL 操作, 比如 CLUSTER,ALTER TABLE,DROP
或顯示的用戶操作如 LOCK TABLE 會與需要通過 DML 操作而獲得的 weak 鎖(AccessShareLock,
RowShareLock, RowExclusiveLock)出現沖突.
(2) VXID locks. Every transaction takes a lock on its own virtual
transaction ID. Currently, the only operations that wait for these locks
are CREATE INDEX CONCURRENTLY and Hot Standby (in the case of a conflict),
so most VXID locks are taken and released by the owner without anyone else
needing to care.
(2)VXID  鎖. 每一個事務都會持有自身虛擬事務 ID 鎖. 當前的做法是, 等待這些鎖的操作只有
CREATE INDEX CONCURRENTLY 和 Hot Standby(出現沖突的情況), 因此大多數 VXID 鎖
跟其他進程無關.
The primary locking mechanism does not cope well with this workload. Even
though the lock manager locks are partitioned, the locktag for any given
relation still falls in one, and only one, partition. Thus, if many short
queries are accessing the same relation, the lock manager partition lock for
that partition becomes a contention bottleneck. This effect is measurable
even on 2-core servers, and becomes very pronounced as core count increases.
主要的鎖定機制不能很好的處理這種工作負載. 就算鎖管理器的 locks 已分區, 對于任意給定的 realtion
仍會落在其中一個且唯一一個分區上. 因此, 如果許多端查詢正在訪問相同的 relation, 該分區上的鎖
會成為爭用瓶頸. 隨著 CPU 核數的升高, 這種影響會非常明顯.
To alleviate this bottleneck, beginning in PostgreSQL 9.2, each backend is
permitted to record a limited number of locks on unshared relations in an
array within its PGPROC structure, rather than using the primary lock table.
This mechanism can only be used when the locker can verify that no conflicting
locks exist at the time of taking the lock.
為了消除這樣的瓶頸, 在 PG 9.2 開始, 允許每一個后臺進程記錄非共享 relation 上有限數目的鎖在
PGPROC 結構體中的數組中, 而不是使用主要 lock table. 該機制只用于在鎖定者可以驗證沒有沖突的情況.
A key point of this algorithm is that it must be possible to verify the
absence of possibly conflicting locks without fighting over a shared LWLock or
spinlock. Otherwise, this effort would simply move the contention bottleneck
from one place to another. We accomplish this using an array of 1024 integer
counters, which are in effect a 1024-way partitioning of the lock space.
Each counter records the number of  strong  locks (that is, ShareLock,
ShareRowExclusiveLock, ExclusiveLock, and AccessExclusiveLock) on unshared
relations that fall into that partition. When this counter is non-zero, the
fast path mechanism may not be used to take new relation locks within that
partition. A strong locker bumps the counter and then scans each per-backend
array for matching fast-path locks; any which are found must be transferred to
the primary lock table before attempting to acquire the lock, to ensure proper
lock conflict and deadlock detection.
該算法的一個關鍵點是可以驗證可能的沖突不會出現, 而不需要與共享 LWLock 或 spinlock 競爭.
否則的話, 這樣的處理結果會簡單的把爭用瓶頸從一個地方移到了另外一個地方.
我們使用 1024 個整型計數器數組對應 1024 個鎖空間分區來實現這一點. 每一個計數器記錄鎖分區上
非共享 relation 上 strong 鎖 (ShareLock,ShareRowExclusiveLock, ExclusiveLock, and AccessExclusiveLock) 的數目.
如果該計數器非 0, 則不使用 fast path 機制.
 strong 鎖會修改計數器, 然后掃描每一個后臺進程匹配的 fast-path locks 數組; 每一個匹配的都必須
在嘗試獲取 lock 前轉換為主 lock table, 用以確保正使用確的鎖沖突和死鎖檢測.
On an SMP system, we must guarantee proper memory synchronization. Here we
rely on the fact that LWLock acquisition acts as a memory sequence point: if
A performs a store, A and B both acquire an LWLock in either order, and B
then performs a load on the same memory location, it is guaranteed to see
A s store. In this case, each backend s fast-path lock queue is protected
by an LWLock. A backend wishing to acquire a fast-path lock grabs this
LWLock before examining FastPathStrongRelationLocks to check for the presence
of a conflicting strong lock. And the backend attempting to acquire a strong
lock, because it must transfer any matching weak locks taken via the fast-path
mechanism to the shared lock table, will acquire every LWLock protecting a
backend fast-path queue in turn. So, if we examine
FastPathStrongRelationLocks and see a zero, then either the value is truly
zero, or if it is a stale value, the strong locker has yet to acquire the
per-backend LWLock we now hold (or, indeed, even the first per-backend LWLock)
and will notice any weak lock we take when it does.
在 SMP 系統上, 必須確保正確的內存同步. 在這里, 需要依賴于 LWLock 獲取作為內存序列點這一事實:
如果 A 執行 store,A 和 B 按任意順序獲取 LWLock, 然后 B 在相同的內存上執行 load, 這可以確保 A store.
在這種情況下, 每一個后臺進程的 fast-path 鎖會在檢查 FastPathStrongRelationLocks 是否與 strong lock
存在沖突前獲取此 LWLock. 后臺進程試圖獲取 strong lock, 因為它必須傳輸通過 fast-path 路徑獲取的
匹配 weak locks 到共享 lock table 中, 因此將依次獲取保護后臺進程 fast-path 的每個 LWLock.
因此, 如果檢查 FastPathStrongRelationLocks 結果為 0, 那么該值實際真的為 0 或者是一個固定值,
strong locks 必須請求持有的 per-backend LWLock, 在完成后會關注所有的 weak lock.
Fast-path VXID locks do not use the FastPathStrongRelationLocks table. The
first lock taken on a VXID is always the ExclusiveLock taken by its owner.
Any subsequent lockers are share lockers waiting for the VXID to terminate.
Indeed, the only reason VXID locks use the lock manager at all (rather than
waiting for the VXID to terminate via some other method) is for deadlock
detection. Thus, the initial VXID lock can *always* be taken via the fast
path without checking for conflicts. Any subsequent locker must check
whether the lock has been transferred to the main lock table, and if not,
do so. The backend owning the VXID must be careful to clean up any entry
made in the main lock table at end of transaction.
Fast-path VXID 鎖沒有使用 FastPathStrongRelationLocks 表.
在 VXID 上獲取的第一個鎖通常是其自身的 ExclusiveLock. 接下來的 lockers 是等待 VXID 結束的共享 lockers.
實際上,VXID 鎖只有使用鎖管理器的唯一理由是用于死鎖檢測. 因此,VXID 的初始化不需要檢查沖突
而是直接通過 fast-path 獲取. 所有后續的 locker 必須檢查鎖釋放已傳輸到主 lock table 中, 如沒有, 則執行此操作.
擁有 VXID 的后臺進程必須在事務結束后小心清理主 lock table 中的 entry.
Deadlock detection does not need to examine the fast-path data structures,
because any lock that could possibly be involved in a deadlock must have
been transferred to the main tables beforehand.
死鎖檢查不需要檢查 fast-path 數據結構, 因為所有的鎖已傳輸到 main table 中.

感謝各位的閱讀,以上就是“怎么理解 PostgreSQL Locks 中的 Fast Path Locking”的內容了,經過本文的學習后,相信大家對怎么理解 PostgreSQL Locks 中的 Fast Path Locking 這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是丸趣 TV,丸趣 TV 小編將為大家推送更多相關知識點的文章,歡迎關注!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-07-26發表,共計6867字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 乌拉特前旗| 乾安县| 江川县| 大宁县| 兴化市| 安乡县| 玉龙| 土默特右旗| 曲阳县| 嘉祥县| 凉山| 西峡县| 永福县| 徐州市| 鄢陵县| 高邮市| 文昌市| 韩城市| 垦利县| 界首市| 洛隆县| 庄河市| 安庆市| 商城县| 兴国县| 海南省| 乐至县| 界首市| 中卫市| 泸溪县| 鹿泉市| 罗定市| 黄陵县| 麟游县| 青阳县| 化德县| 梨树县| 河东区| 穆棱市| 镇原县| 高台县|