广告
返回顶部
首页 > 资讯 > 数据库 >怎么理解PostgreSQL Locks中的Fast Path Locking
  • 856
分享到

怎么理解PostgreSQL Locks中的Fast Path Locking

2024-04-02 19:04:59 856人浏览 泡泡鱼
摘要

这篇文章主要讲解了“怎么理解postgresql Locks中的Fast Path Locking”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解Po

这篇文章主要讲解了“怎么理解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这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!

您可能感兴趣的文档:

--结束END--

本文标题: 怎么理解PostgreSQL Locks中的Fast Path Locking

本文链接: https://www.lsjlt.com/news/63634.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么理解PostgreSQL Locks中的Fast Path Locking
    这篇文章主要讲解了“怎么理解PostgreSQL Locks中的Fast Path Locking”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解Po...
    99+
    2022-10-18
  • 怎么理解PostgreSQL Locks中的Lock Manager Internal Locking
    本篇内容主要讲解“怎么理解PostgreSQL Locks中的Lock Manager Internal Locking”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家...
    99+
    2022-10-18
  • 怎么理解PostgreSQL Locks中的The Deadlock Detection Algorithm
    这篇文章主要讲解了“怎么理解PostgreSQL Locks中的The Deadlock Detection Algorithm”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,...
    99+
    2022-10-18
  • 怎么使用PostgreSQL中的lightweight locks.
    这篇文章主要介绍“怎么使用PostgreSQL中的lightweight locks.”,在日常操作中,相信很多人在怎么使用PostgreSQL中的lightweight locks.问题上存在疑惑,小编查...
    99+
    2022-10-18
  • 怎么理解PostgreSQL中的参数autovacuum_max_workers
    这篇文章主要介绍“怎么理解PostgreSQL中的参数autovacuum_max_workers”,在日常操作中,相信很多人在怎么理解PostgreSQL中的参数autovacuum_max_worker...
    99+
    2022-10-18
  • 怎么理解PostgreSQL的分区表
    本篇内容主要讲解“怎么理解PostgreSQL的分区表”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解PostgreSQL的分区表”吧!在PG中,分区表通...
    99+
    2022-10-18
  • 怎么理解PostgreSQL的PG Index Properties
    本篇内容介绍了“怎么理解PostgreSQL的PG Index Properties”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家...
    99+
    2022-10-18
  • 怎么理解PostgreSQL中session hang情况
    这篇文章主要介绍“怎么理解PostgreSQL中session hang情况”,在日常操作中,相信很多人在怎么理解PostgreSQL中session hang情况问题上存在疑惑,小编查阅了各式资料,整理出...
    99+
    2022-11-30
    postgresql
  • 怎么理解PostgreSQL中Clock Sweep算法
    本篇内容介绍了“怎么理解PostgreSQL中Clock Sweep算法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能...
    99+
    2022-10-18
  • 怎么理解PostgreSQL事务管理中的子事务
    本篇内容主要讲解“怎么理解PostgreSQL事务管理中的子事务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解PostgreSQL事务管理中的子事务”吧...
    99+
    2022-10-18
  • 怎么理解PostgreSQL的词法分析
    这篇文章主要讲解了“怎么理解PostgreSQL的词法分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解PostgreSQL的词法分析”吧!一、词法...
    99+
    2022-10-18
  • 怎么理解PostgreSQL的后台进程autovacuum
    本篇内容介绍了“怎么理解PostgreSQL的后台进程autovacuum”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、数据结构Auto...
    99+
    2023-05-31
  • PostgreSQL中怎么监控VACUUM的处理过程
    这篇文章主要讲解了“PostgreSQL中怎么监控VACUUM的处理过程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PostgreSQL中怎么监控VACUUM的处理过程”吧!概览PG的MV...
    99+
    2023-05-31
  • 怎么理解PostgreSQL语法分析中的上下文无关语法
    本篇内容介绍了“怎么理解PostgreSQL语法分析中的上下文无关语法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够...
    99+
    2022-10-18
  • 怎么理解PostgreSQL创建数据表时的参数fillfactor
    这篇文章主要讲解了“怎么理解PostgreSQL创建数据表时的参数fillfactor”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解PostgreS...
    99+
    2022-10-18
  • 怎么解决数据库中没有索引导致的DIRECT PATH READ
    本篇内容主要讲解“怎么解决数据库中没有索引导致的DIRECT PATH READ”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么解决数据库中没有索引导致的DI...
    99+
    2022-10-18
  • Spring中的 @SessionAttributes注解怎么理解
    这篇文章将为大家详细讲解有关Spring中的 @SessionAttributes注解怎么理解,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。@ModelAttribute注解作用在方法上或者...
    99+
    2023-06-02
  • 怎么理解Java中的JSP
    本篇内容介绍了“怎么理解Java中的JSP”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!前言:JSP 代表 Java 服务器页面。它是一种在...
    99+
    2023-06-25
  • java中的for怎么理解
    java关键字for:循环控制的一个关键字,可以用来控制语句循环。通常的格式是:for(初始化;控制语句;控制变量调控){循环语句}。控制变量的初始化部分可以省略,也可以初始化很多变量。如:for(;i...
    99+
    2018-10-22
    java教程 java for
  • 怎么理解MySQL5.6中的PERFORMANCE_SCHEM
    本篇内容介绍了“怎么理解MySQL5.6中的PERFORMANCE_SCHEM”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作