您好,登錄后才能下訂單哦!
本篇內容主要講解“PostgreSQL Locks中LOCK結構體是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“PostgreSQL Locks中LOCK結構體是什么”吧!
/* * The LOCKTAG struct is defined with malice aforethought to fit into 16 * bytes with no padding. Note that this would need adjustment if we were * to widen Oid, BlockNumber, or TransactionId to more than 32 bits. * * We include lockmethodid in the locktag so that a single hash table in * shared memory can store locks of different lockmethods. */ typedef struct LOCKTAG { uint32 locktag_field1; /* a 32-bit ID field */ uint32 locktag_field2; /* a 32-bit ID field */ uint32 locktag_field3; /* a 32-bit ID field */ uint16 locktag_field4; /* a 16-bit ID field */ uint8 locktag_type; /* see enum LockTagType */ uint8 locktag_lockmethodid; /* lockmethod indicator */ } LOCKTAG; /* * Per-locked-object lock information: * * tag -- uniquely identifies the object being locked * grantMask -- bitmask for all lock types currently granted on this object. * waitMask -- bitmask for all lock types currently awaited on this object. * procLocks -- list of PROCLOCK objects for this lock. * waitProcs -- queue of processes waiting for this lock. * requested -- count of each lock type currently requested on the lock * (includes requests already granted!!). * nRequested -- total requested locks of all types. * granted -- count of each lock type currently granted on the lock. * nGranted -- total granted locks of all types. * * Note: these counts count 1 for each backend. Internally to a backend, * there may be multiple grabs on a particular lock, but this is not reflected * into shared memory. */ typedef struct LOCK { /* hash key */ LOCKTAG tag; /* unique identifier of lockable object */ /* data */ LOCKMASK grantMask; /* bitmask for lock types already granted */ LOCKMASK waitMask; /* bitmask for lock types awaited */ SHM_QUEUE procLocks; /* list of PROCLOCK objects assoc. with lock */ PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */ int requested[MAX_LOCKMODES]; /* counts of requested locks */ int nRequested; /* total of requested[] array */ int granted[MAX_LOCKMODES]; /* counts of granted locks */ int nGranted; /* total of granted[] array */ } LOCK; #define LOCK_LOCKMETHOD(lock) ((LOCKMETHODID) (lock).tag.locktag_lockmethodid) --------------------------------------------------------------------------- The lock manager's LOCK objects contain: LOCK結構體包括: tag - The key fields that are used for hashing locks in the shared memory lock hash table. The contents of the tag essentially define an individual lockable object. See include/storage/lock.h for details about the supported types of lockable objects. This is declared as a separate struct to ensure that we always zero out the correct number of bytes. It is critical that any alignment-padding bytes the compiler might insert in the struct be zeroed out, else the hash computation will be random. (Currently, we are careful to define struct LOCKTAG so that there are no padding bytes.) tag - 該鍵域用于標記共享內存lock哈希表中的hashing locks.標記tag的內容本質上定義了 一個獨立的可鎖定對象.關于已支持的可鎖定對象類型的詳細信息可參考include/storage/lock.h. 之所以定義為一個單獨的結構是為了確保能夠把歸零正確的字節數. 編譯器可能插入到結構體中的所有對齊字節數正確被歸零是很很重要的,否則的話哈希的計算會是隨機的. (當前來看,定義結構體LOCKTAG以避免對齊字節) grantMask - This bitmask indicates what types of locks are currently held on the given lockable object. It is used (against the lock table's conflict table) to determine if a new lock request will conflict with existing lock types held. Conflicts are determined by bitwise AND operations between the grantMask and the conflict table entry for the requested lock type. Bit i of grantMask is 1 if and only if granted[i] > 0. grantMask - 該bitmask表示在給定的可鎖定對象上持有了哪些類型的locks. 該字段用于確定新申請的鎖是否會與現存的鎖存在沖突. 沖突通過grantMask和請求鎖類型的沖突表條目的bitwise AND操作實現. 當且僅當granted[i] > 0,grantMask的第i位為1. waitMask - This bitmask shows the types of locks being waited for. Bit i of waitMask is 1 if and only if requested[i] > granted[i]. waitMask - 該字段標記了正在等待的鎖類型.當且僅當requested[i] > granted[i],waitMask中的第1位為1. procLocks - This is a shared memory queue of all the PROCLOCK structs associated with the lock object. Note that both granted and waiting PROCLOCKs are in this list (indeed, the same PROCLOCK might have some already-granted locks and be waiting for more!). procLocks - 與lock object相關的PROCLOCK結構體在共享內存中的隊列. 注意鏈表中存在granted和waiting PROCLOCKs. (實際上,同一個PROCLOCK可能有已授予的locks但正在等待更多的鎖) waitProcs - This is a shared memory queue of all PGPROC structures corresponding to backends that are waiting (sleeping) until another backend releases this lock. The process structure holds the information needed to determine if it should be woken up when the lock is released. waitProcs - 對應等待其他后臺進程釋放鎖的后臺進程的PGPROC結構體在共享內存中的隊列. 進程結構體保存了用于確定在鎖釋放時是否需要喚醒的相關信息. nRequested - Keeps a count of how many times this lock has been attempted to be acquired. The count includes attempts by processes which were put to sleep due to conflicts. It also counts the same backend twice if, for example, a backend process first acquires a read and then acquires a write. (But multiple acquisitions of the same lock/lock mode within a backend are not multiply counted here; they are recorded only in the backend's LOCALLOCK structure.) nRequested - 該字段保存了嘗試獲取該鎖的次數.計數包括因為沖突而處于休眠狀態的次數. 如果一個進程第一次請求讀然后請求寫時可能會導致該進程被多次統計. requested - Keeps a count of how many locks of each type have been attempted. Only elements 1 through MAX_LOCKMODES-1 are used as they correspond to the lock type defined constants. Summing the values of requested[] should come out equal to nRequested. requested - 該字段保存了嘗試獲取多少種鎖類型.只有1 -> MAX_LOCKMODES-1被使用,因為這對應了鎖類型常量. 計算requested數組的和應等于nRequested. nGranted - Keeps count of how many times this lock has been successfully acquired. This count does not include attempts that are waiting due to conflicts. Otherwise the counting rules are the same as for nRequested. nGranted - 成功獲取該鎖的次數.該計數不包括因為沖突而等待的次數.因此該計數規則與nRequested一樣. granted - Keeps count of how many locks of each type are currently held. Once again only elements 1 through MAX_LOCKMODES-1 are used (0 is not). Also, like requested[], summing the values of granted[] should total to the value of nGranted. granted - 保存每種類型有多少鎖.1 -> MAX_LOCKMODES-1是有用的. 與requested類似,granted[]數組的和應等于nGranted. We should always have 0 <= nGranted <= nRequested, and 0 <= granted[i] <= requested[i] for each i. When all the request counts go to zero, the LOCK object is no longer needed and can be freed. nGranted的的范圍為[0,nRequested],對于每一個granted[i]范圍為[0,requested[i]]. 如果所有請求變為0,那么LOCK對象不再需要,會通過free釋放.
到此,相信大家對“PostgreSQL Locks中LOCK結構體是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。