成都创新互联网站制作重庆分公司

怎么理解PostgreSQLLocks中的FastPathLocking

这篇文章主要讲解了“怎么理解PostgreSQL Locks中的Fast Path Locking”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解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这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


分享题目:怎么理解PostgreSQLLocks中的FastPathLocking
分享URL:http://cxhlcq.com/article/gdhcdh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部