怎么理解PostgreSQL Locks中的Fast Path Locking
发表于:2025-01-20 作者:千家信息网编辑
千家信息网最后更新 2025年01月20日,这篇文章主要讲解了"怎么理解PostgreSQL Locks中的Fast Path Locking",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么理
千家信息网最后更新 2025年01月20日怎么理解PostgreSQL Locks中的Fast Path Locking
这篇文章主要讲解了"怎么理解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 theoverhead of taking and releasing certain types of locks which are takenand released very frequently but rarely conflict. Currently, this includestwo categories of locks:Fast path locking用以减少那些需要经常获取和释放但又很少出现冲突的锁类型的获取/释放负载.当前的做法,包括2种类型(category)的锁:(1) Weak relation locks. SELECT, INSERT, UPDATE, and DELETE must acquire alock on every relation they operate on, as well as various system catalogsthat can be used internally. Many DML operations can proceed in parallelagainst the same table at the same time; only DDL operations such asCLUSTER, 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 virtualtransaction ID. Currently, the only operations that wait for these locksare 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 elseneeding to care.(2)VXID 锁.每一个事务都会持有自身虚拟事务ID锁.当前的做法是,等待这些锁的操作只有CREATE INDEX CONCURRENTLY和Hot Standby(出现冲突的情况),因此大多数VXID锁跟其他进程无关.The primary locking mechanism does not cope well with this workload. Eventhough the lock manager locks are partitioned, the locktag for any givenrelation still falls in one, and only one, partition. Thus, if many shortqueries are accessing the same relation, the lock manager partition lock forthat partition becomes a contention bottleneck. This effect is measurableeven 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 ispermitted to record a limited number of locks on unshared relations in anarray within its PGPROC structure, rather than using the primary lock table.This mechanism can only be used when the locker can verify that no conflictinglocks 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 theabsence of possibly conflicting locks without fighting over a shared LWLock orspinlock. Otherwise, this effort would simply move the contention bottleneckfrom one place to another. We accomplish this using an array of 1024 integercounters, 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 unsharedrelations that fall into that partition. When this counter is non-zero, thefast path mechanism may not be used to take new relation locks within thatpartition. A strong locker bumps the counter and then scans each per-backendarray for matching fast-path locks; any which are found must be transferred tothe primary lock table before attempting to acquire the lock, to ensure properlock 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 werely on the fact that LWLock acquisition acts as a memory sequence point: ifA performs a store, A and B both acquire an LWLock in either order, and Bthen performs a load on the same memory location, it is guaranteed to seeA's store. In this case, each backend's fast-path lock queue is protectedby an LWLock. A backend wishing to acquire a fast-path lock grabs thisLWLock before examining FastPathStrongRelationLocks to check for the presenceof a conflicting strong lock. And the backend attempting to acquire a stronglock, because it must transfer any matching weak locks taken via the fast-pathmechanism to the shared lock table, will acquire every LWLock protecting abackend fast-path queue in turn. So, if we examineFastPathStrongRelationLocks and see a zero, then either the value is trulyzero, or if it is a stale value, the strong locker has yet to acquire theper-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. Thefirst 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 thanwaiting for the VXID to terminate via some other method) is for deadlockdetection. Thus, the initial VXID lock can *always* be taken via the fastpath without checking for conflicts. Any subsequent locker must checkwhether 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 entrymade 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 havebeen transferred to the main tables beforehand.死锁检查不需要检查fast-path数据结构,因为所有的锁已传输到main table中.
感谢各位的阅读,以上就是"怎么理解PostgreSQL Locks中的Fast Path Locking"的内容了,经过本文的学习后,相信大家对怎么理解PostgreSQL Locks中的Fast Path Locking这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
冲突
进程
后台
检查
情况
计数器
事务
内存
只有
数组
机制
瓶颈
用以
系统
死锁
传输
学习
验证
相同
做法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
软件测评好还软件开发好
青岛头条网络技术
现在的网络安全工程师
上海天戏网络技术
c 游戏服务器引擎
深信服数据库一体机
软件开发基础第二课
国家网络安全及信息化建设扎
以太网串口服务器调试软件
阿里云香港服务器镜像
云服务器的实例名称是什么
linux 下 网络安全
北京网络安全通讯板卡
广电网络技术工程部
主题为网络安全的征文
中外文常见数据库
塔式服务器散热能力
制定镇政府网络安全工作方案
杭州比较好的软件开发公司
无极数据库编辑器4.0.8
linux多线程服务器并发机制
软件开发有限公司的产品或服务
湖南长沙牛耳软件开发的地址
网络安全法是一部司法解释
2019网络安全竞赛题库
软件开发编码过程能力
江西中汇网络技术公司天眼查
网络技术的选上海百首网络
数据库 rownum
如何实现数据库的连接