PostgreSQL 9.6 等待事件出炉

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介:

PostgreSQL 9.6 等待事件

作者

digoal

日期

2016-10-06

标签

PostgreSQL , 9.6 , 等待事件 , wait_event


背景

PostgreSQL 9.6动态视图pg_stat_activity新增了wait_event_type, wait_event的等待事件展示。

当会话处于等待状态时,wait_event与wait_event_type非空,表示会话正在等待的类型。

根据等待信息,可以了解当前会话的状态。

将来也可以通过插件的形式,掌握数据库在某个时间段内的等待事件统计,更好的诊断数据库的健康状态。

已有的插件如下

https://github.com/postgrespro/pg_wait_sampling

用于对等待事件进行采样。

例子

pg_wait_sampling_history view – history of wait events obtained by sampling into
in-memory ring buffer.

Column name Column type Description
pid int4 Id of process
ts timestamptz Sample timestamp
event_type text Name of wait event type
event text Name of wait event

pg_wait_sampling_profile view – profile of wait events obtained by sampling into
in-memory hash table.

Column name Column type Description
pid int4 Id of process
event_type text Name of wait event type
event text Name of wait event
count text Count of samples

pg_wait_sampling_reset_profile() function resets the profile.

The work of wait event statistics collector worker is controlled by following
GUCs.

Parameter name Data type Description Default value
pg_wait_sampling.history_size int4 Size of history in-memory ring buffer 5000
pg_wait_sampling.history_period int4 Period for history sampling in milliseconds 10
pg_wait_sampling.profile_period int4 Period for profile sampling in milliseconds 10
pg_wait_sampling.profile_pid bool Whether profile should be per pid true

PostgreSQL 9.6 等待事件

详见
https://www.postgresql.org/docs/9.6/static/monitoring-stats.html

pg_stat_activity 视图新增等待事件列,可以观察到会话当前的等待。

1. wait_event_type
表示等待时间的类别,如果backend处于等待状态则有内容,否则为空。

类别如下

1.1 LWLockNamed:

命名的轻量锁,这种锁的目的是用于保护内存中的数据结构,防止并发的问题。      

The backend is waiting for a specific named lightweight lock.     

Each such lock protects a particular data structure in shared memory.     

1.2 LWLockTranche:

分组轻量锁,没有细分名字,只是笼统的分类。      

The backend is waiting for one of a group of related lightweight locks.     

All locks in the group perform a similar function;     

1.3 Lock:

重量级锁,用于保护SQL可见对象,例如表。  也可以用于保护存储,例如扩展表时。      

见 src/include/storage/lock.h        

The backend is waiting for a heavyweight lock.     

Heavyweight locks, also known as lock manager locks or simply locks, primarily protect SQL-visible objects such as tables.     

However, they are also used to ensure mutual exclusion for certain internal operations such as relation extension.     

1.4 BufferPin:

bufferpin用于保护数据库data buffer中的数据,例如保护游标访问的数据。      

The server process is waiting to access to a data buffer during a period when no other process can be examining that buffer.     

Buffer pin waits can be protracted if another process holds an open cursor which last read data from the buffer in question.    

2. wait_event

表示wait_event_type中对应的详细的等待事件。      

如果当前backend处于等待状态,则有值,否则为空    

Wait event name if backend is currently waiting, otherwise NULL.     

2.1 LWLockNamed

ShmemIndexLock      
    Waiting to find or allocate space in shared memory.    

OidGenLock      
    Waiting to allocate or assign an OID.    

XidGenLock      
    通常出现在高并发的请求事务号时    
    Waiting to allocate or assign a transaction id.    

ProcArrayLock       
    通常出现在高并发的请求事务号,并且开启了old_snapshot_threshold时    
    Waiting to get a snapshot or clearing a transaction id at transaction end.    

SInvalReadLock      
    Waiting to retrieve or remove messages from shared invalidation queue.    

SInvalWriteLock     
    Waiting to add a message in shared invalidation queue.    

WALBufMappingLock       
    Waiting to replace a page in WAL buffers.    

WALWriteLock        
    wal刷盘较慢时,可以提高wal writer频率,或者加大BUFFER,或者提高目标盘的IOPS,降低目标盘的RT。      
    Waiting for WAL buffers to be written to disk.    

ControlFileLock     
    如果产生XLOG确实很频繁,并且没有办法降低,可以使用较大的XLOG文件,最大64MB。      
    通常这个很少见。    
    Waiting to read or update the control file or creation of a new WAL file.    

CheckpointLock      
    Waiting to perform checkpoint.    

CLogControlLock     
    Waiting to read or update transaction status.    

SubtransControlLock     
    Waiting to read or update sub-transaction information.    

MultiXactGenLock        
    Waiting to read or update shared multixact state.    

MultiXactOffsetControlLock      
    Waiting to read or update multixact offset mappings.    

MultiXactMemberControlLock      
    Waiting to read or update multixact member mappings.    

RelCacheInitLock        
    Waiting to read or write relation cache initialization file.    

CheckpointerCommLock        
    检查点分三步(write, sync_file_range, fsync),表示fsync请求出现等待,需要提高IO,或者减少fsync时的dirty page。      
    Waiting to manage fsync requests.    

TwoPhaseStateLock       
    Waiting to read or update the state of prepared transactions.    

TablespaceCreateLock        
    Waiting to create or drop the tablespace.    

BtreeVacuumLock     
    频繁出现,说明索引字段被频繁更新。      
    Waiting to read or update vacuum-related information for a B-tree index.    

AddinShmemInitLock      
    Waiting to manage space allocation in shared memory.    

AutovacuumLock      
    Autovacuum worker or launcher waiting to update or read the current state of autovacuum workers.    

AutovacuumScheduleLock      
    说明autovacuum单表比较慢,看看是否可以关闭autovacuum的SLEEP调度。    
    Waiting to ensure that the table it has selected for a vacuum still needs vacuuming.    

SyncScanLock        
    Waiting to get the start location of a scan on a table for synchronized scans.    

RelationMappingLock     
    Waiting to update the relation map file used to store catalog to filenode mapping.    

AsyncCtlLock        
    Waiting to read or update shared notification state.    

AsyncQueueLock      
    Waiting to read or update notification messages.    

SerializableXactHashLock        
    Waiting to retrieve or store information about serializable transactions.   

SerializableFinishedListLock        
    Waiting to access the list of finished serializable transactions.    

SerializablePredicateLockListLock       
    Waiting to perform an operation on a list of locks held by serializable transactions.    

OldSerXidLock       
    Waiting to read or record conflicting serializable transactions.    

SyncRepLock     
    Waiting to read or update information about synchronous replicas.    

BackgroundWorkerLock        
    Waiting to read or update background worker state.    

DynamicSharedMemoryControlLock      
    Waiting to read or update dynamic shared memory state.    

AutoFileLock        
    Waiting to update the postgresql.auto.conf file.    

ReplicationSlotAllocationLock       
    Waiting to allocate or free a replication slot.    

ReplicationSlotControlLock      
    Waiting to read or update replication slot state.    

CommitTsControlLock     
    Waiting to read or update transaction commit timestamps.    

CommitTsLock        
    Waiting to read or update the last value set for the transaction timestamp.    

ReplicationOriginLock       
    Waiting to setup, drop or use replication origin.    

MultiXactTruncationLock     
    Waiting to read or truncate multixact information.    

OldSnapshotTimeMapLock      
    Waiting to read or update old snapshot control information.    

2.2 LWLockTranche

clog        
    通常很少见,可能出现在在非常高并发的极小写事务时,文件IO出现等待,使用cgroup可以很容易复现。      
    Waiting for I/O on a clog (transaction status) buffer.    

commit_timestamp        
    Waiting for I/O on commit timestamp buffer.    

subtrans        
    Waiting for I/O a subtransaction buffer.    

multixact_offset        
    Waiting for I/O on a multixact offset buffer.   

multixact_member        
    Waiting for I/O on a multixact_member buffer.   

async       
    Waiting for I/O on an async (notify) buffer.    

oldserxid       
    Waiting to I/O on an oldserxid buffer.    

wal_insert      
    Waiting to insert WAL into a memory buffer.    

buffer_content      
    指 数据库 shared buffer    
    Waiting to read or write a data page in memory.    

buffer_io       
    指 数据库 shared buffer    
    Waiting for I/O on a data page.    

replication_origin      
    Waiting to read or update the replication progress.    

replication_slot_io     
    Waiting for I/O on a replication slot.    

proc        
    Waiting to read or update the fast-path lock information.    

buffer_mapping      
    Waiting to associate a data block with a buffer in the buffer pool.  

lock_manager        
    Waiting to add or examine locks for backends, or waiting to join or exit a locking group (used by parallel query).    

predicate_lock_manager      
    Waiting to add or examine predicate lock information.    

2.3 Lock

relation        
    Waiting to acquire a lock on a relation.    

extend      
    Waiting to extend a relation.    

page        
    Waiting to acquire a lock on page of a relation.    

tuple       
    Waiting to acquire a lock on a tuple.    

transactionid       
    Waiting for a transaction to finish.    

virtualxid      
    Waiting to acquire a virtual xid lock.    

speculative token       
    Waiting to acquire a speculative insertion lock.    

object      
    Waiting to acquire a lock on a non-relation database object.    

userlock        
    Waiting to acquire a userlock.    

advisory        
    Waiting to acquire an advisory user lock.    

2.4 BufferPin

BufferPin       
    Waiting to acquire a pin on a buffer.    

3. 获取当指定PID当前的等待信息。

pg_stat_get_backend_wait_event_type(integer)        
    Wait event type name if backend is currently waiting, otherwise NULL. See Table 28-4 for details.    

pg_stat_get_backend_wait_event(integer)     
    Wait event name if backend is currently waiting, otherwise NULL. See Table 28-4 for details.    

Count

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
关系型数据库 PostgreSQL
PostgreSQL 性能优化: 等待事件
等待事件是 PostgreSQL 的重要优化工具。当您能查明会话为什么在等待资源以及会话在做什么时,您就能更好地减少瓶颈。您可以使用本节中的信息来查找可能的原因和纠正措施。
267 0
|
Oracle 关系型数据库 数据库
PostgreSQL技术周刊第27期:PostgreSQL变更事件捕获(CDC) 原理应用与实践
【点击订阅PostgreSQL技术周刊】 PostgreSQL(简称PG)的开发者们:云栖社区已有5000位PG开发者,发布了3000+PG文章(文章列表),沉淀了700+的PG精品问答(问答列表)。
5575 0
|
关系型数据库 数据库 PostgreSQL
【PostgreSQL系列直播】PostgreSQL变更事件捕获(CDC) 原理应用与实践
主讲人 冯若航前探探PostgreSQL DBA,前阿里全栈工程师。译有《设计数据密集型应用》,《PostgreSQL原理浅析》。 直播内容 《PostgreSQL CDC(变更事件捕获)原理应用与实践》 变更数据捕获(change data capture, CDC)是一种订阅数据库的写入变更,并将其提取转换为可以复制到其他系统中的形式的过程,妙用无穷。
2295 0
|
关系型数据库 PostgreSQL
PostgreSQL 10.1 手册_部分 IV. 客户端接口_第 33 章 libpq - C 库_33.13. 事件系统
33.13. 事件系统 33.13.1. 事件类型 33.13.2. 事件回调函数 33.13.3. 事件支持函数 33.13.4. 事件实例 libpq的事件系统被设计为通知已注册的事件处理器它感兴趣的libpq事件,例如PGconn以及PGresult对象的创建和毁灭。
1110 0
|
SQL 关系型数据库 PostgreSQL
PostgreSQL 10.1 手册_部分 II. SQL 语言_第 9 章 函数和操作符_9.28. 事件触发器函数
9.28. 事件触发器函数 9.28.1. 捕获命令结尾的改变 9.28.2. 通过DDL命令删除处理的对象 9.28.3. 处理表重写事件 PostgreSQL提供这些帮助函数用以从事件触发器中检索信息。
1427 0
|
Web App开发 SQL 关系型数据库
|
SQL 关系型数据库 PostgreSQL

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 下一篇
    无影云桌面