您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么理解PostgreSQL Locks中的Fast Path Locking”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么理解PostgreSQL Locks中的Fast Path Locking”吧!
PG提供的系統表pg_locks中有一個字段:fastpath,用以表示是否fastpath,那fastpath指的是什么呢?
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這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。