postgresql 锁_PostgreSQL中的锁:4.内存中的锁

本文详细介绍了PostgreSQL中的内存锁,包括自旋锁、轻量级锁和缓冲销,以及如何通过等待事件监控来观察锁的使用情况。自旋锁适用于短时保护,轻量级锁支持共享和独占模式,而缓冲销用于保护数据结构的临时访问。文章通过缓冲区缓存和WAL缓冲区两个例子展示了锁在实际操作中的应用。
摘要由CSDN通过智能技术生成

postgresql 锁

relation-level locks, 关系级锁row-level locks, 行级锁locks on other objects (including predicate locks) and interrelationships of different types of locks. 其他对象上的锁 (包括谓词锁)以及不同类型锁的相互关系。

The following discussion of locks in RAM finishes this series of articles. We will consider spinlocks, lightweight locks and buffer pins, as well as events monitoring tools and sampling.

以下有关RAM锁的讨论结束了本系列文章。 我们将考虑自旋锁,轻型锁和缓冲销以及事件监视工具和采样。

自旋锁 (Spinlocks)

Unlike normal, «heavy-weight», locks, to protect structures in the shared memory, more lightweight and less expensive (in overhead costs) locks are used.

与普通的“超重”锁不同,它用于保护共享内存中的结构,而使用的是重量更轻,成本更低(在开销成本方面)的锁。

The simplest of them are spinlocks. They are meant to be acquired for very short time intervals (a few processor instructions), and they protect separate memory areas from simultaneous changes.

其中最简单的是自旋锁 。 可以在很短的时间间隔(一些处理器指令)中获取它们,并且它们可以保护单独的存储区免于同时更改。

Spinlocks are implemented based on atomic processor instructions, such as compare-and-swap. They support the only, exclusive, mode. If a lock is acquired, a waiting process performs busy waiting — the command is repeated («spins» in a loop, hence the name) until it is a success. This makes sense since spinlocks are used in the cases where the probability of a conflict is estimated as very low.

自旋锁是根据原子处理器指令(例如比较和交换)实现的。 它们支持唯一的独占模式。 如果获得了锁,则等待过程将执行繁忙的等待-重复该命令(循环中称为“旋转”,因此称为名称),直到成功为止。 这很有意义,因为在估计冲突可能性非常低的情况下使用了自旋锁。

Spinlocks do not enable detection of deadlocks (PostgreSQL developers take care of this) and provide no monitoring tools. Essentially, the only thing we can do with spinlocks is to be aware of their existence.

自旋锁不能启用死锁检测(PostgreSQL开发人员会注意这一点),并且不提供监视工具。 从本质上讲,我们对自旋锁唯一能做的就是知道它们的存在。

轻巧的锁 (Lightweight locks)

So-called lightweight locks (lwlocks) come next.

接下来是所谓的轻量级锁 (lwlocks)。

They get acquired for a short time that is needed to work with the data structure (such as a hash table or a list of pointers). As a rule, a lightweight lock is held briefly, but sometimes lightweight locks protect input/output operations, so in general, the time might also be considerable.

它们会在很短的时间内被获取,以使用数据结构(例如哈希表或指针列表)。 通常,轻量级锁是短暂持有的,但有时轻量级锁可以保护输入/输出操作,因此,通常情况下,时间可能也很长。

Two modes are supported: exclusive (for data modifications) and shared (only for reading). There is actually no wait queue: if a few processes wait for release of a lock, one of them will get the access in a more or less random fashion. In high-concurrency and large-load systems, this can be troublesome (for example, see this discussion).

支持两种模式:独占(用于数据修改)和共享(仅用于读取)。 实际上没有等待队列:如果有几个进程等待释放锁,那么其中一个进程或多或少会获得随机访问。 在高并发和大负载的系统中,这可能很麻烦(例如,请参阅此讨论 )。

There are no techniques to check for deadlocks, so this is left to the responsibility of developers of the core. However, lightweight locks have monitoring tools, so, unlike spinlocks, we can «see» them (I will show a bit later how to do this).

没有检查死锁的技术,因此,这由核心开发人员负责。 但是,轻量级锁具有监视工具,因此,与自旋锁不同,我们可以“查看”它们(稍后我将展示如何执行此操作)。

缓冲销 (Buffer pin)

Yet another type of locks, which we already touched upon in the article on the buffer cache, is a buffer pin.

我们已经在缓冲区高速缓存的文章中介绍过的另一种类型的锁是缓冲区pin

Different operations, including data modifications, can be performed with a pinned buffer, but under the condition that the changes are not visible to other processes due to multiversion concurrency control. That is, we can, for instance, add a new row to the page, but cannot replace a page in the buffer with another one.

可以使用固定缓冲区执行不同的操作(包括数据修改),但条件是由于多版本并发控制,更改对其他进程不可见。 也就是说,例如,我们可以在页面上添加新行,但是不能用另一页面替换缓冲区中的页面。

If a buffer pin hinders a process, as a rule, the latter just skips this buffer and chooses a different one. But in some cases, where exactly this buffer is needed, the process queues and «falls asleep»; the system will wake it up when the buffer is unpinned.

通常,如果缓冲销阻碍了处理,则后者仅跳过该缓冲并选择其他缓冲。 但是在某些情况下,正是需要此缓冲区的地方,进程排队并“入睡”; 取消固定缓冲区时,系统将唤醒它。

Monitoring can access waits related to buffer pins.

监视可以访问与缓冲区引脚相关的等待。

示例:缓冲区缓存 (Example: buffer cache)

Now, in order to get some (although incomplete!) insight into how and where locks are used, let's consider the buffer cache as an example.

现在,为了对使用锁的方式和位置有一些了解(尽

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值