选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0
历史版本PostgreSQL 9.2 已于 2017 年 11 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本

45.58. pg_locks #

视图pg_locks提供了对数据库服务器中打开的事务持有的锁的信息的访问。有关锁定的更多讨论,请参见第 13 章

pg_locks包含每个活动的可锁定对象、请求的锁模式和相关事务的一行。 因此,如果多个事务正在持有或等待锁定它,同一可锁定对象可能会出现多次。 但是,当前没有任何锁定的对象将不会出现。

有几种不同类型的可锁定对象: 整个关系(例如,表),关系的单个页面, 关系的单个元组, 事务ID(虚拟和永久ID均包括), 以及一般的数据库对象(由类OID和对象OID标识, 与pg_descriptionpg_depend中的方式相同)。 此外,还可以对具有用户定义含义的数字施加咨询锁

表 45.59. pg_locks

Name Type References Description
locktype text   Type of the lockable object: relation, extend, page, tuple, transactionid, virtualxid, object, userlock, or advisory
database oid pg_database.oid OID of the database in which the object exists, or zero if the object is a shared object, or null if the object is a transaction ID
relation oid pg_class.oid OID of the relation, or null if the object is not a relation or part of a relation
page integer   Page number within the relation, or null if the object is not a tuple or relation page
tuple smallint   Tuple number within the page, or null if the object is not a tuple
virtualxid text   Virtual ID of a transaction, or null if the object is not a virtual transaction ID
transactionid xid   ID of a transaction, or null if the object is not a transaction ID
classid oid pg_class.oid OID of the system catalog containing the object, or null if the object is not a general database object
objid oid any OID column OID of the object within its system catalog, or null if the object is not a general database object
objsubid smallint   Column number targeted by the lock (the classid and objid refer to the table itself), or zero if the target is some other general database object, or null if the target is not a general database object
virtualtransaction text   Virtual ID of the transaction that is holding or awaiting this lock
pid integer   Process ID of the server process holding or awaiting this lock, or null if the lock is held by a prepared transaction
mode text   Name of the lock mode held or desired by this process (see 第 13.3.1 节 and 第 13.2.3 节)
granted boolean   True if lock is held, false if lock is awaited
fastpath boolean   如果锁通过快速路径获得则为真,通过主锁表获得则为假

在表示由指定事务持有的锁的行中,granted 为真。为假表示该事务当前正在等待获取此锁, 这意味着某个其他事务正在同一可锁定对象上持有冲突的锁模式。等待的事务会休眠,直到另一个锁被释放 (或检测到死锁情况)。单个事务一次最多只能等待获取一个锁。

每个事务在其整个持续期内都对其虚拟事务ID持有独占锁。如果为事务分配了永久ID (通常仅在事务改变数据库状态时才会发生),它还会对其永久事务ID持有独占锁直到事务结束。 当一个事务发现有必要专门等待另一个事务时,它会尝试获取另一个事务ID(根据情况是虚拟ID还是永久ID) 的共享锁。只有当另一个事务终止并释放其锁时,这才会成功。

虽然元组是一种可锁定的对象类型,但关于行级锁的信息存储在磁盘上,而不是内存中,因此行级锁通常不会出现在此视图中。 如果一个事务正在等待行级锁,它通常会出现在视图中,等待当前持有该行锁的永久事务ID。

咨询锁可以在由单个 bigint 值或两个整数值组成的键上获取。bigint 键显示时,其高位一半放在 classid 列,低位一半放在 objid 列,而 objsubid 等于 1。整数键显示时,第一个键放在 classid 列,第二个键放在 objid 列,而 objsubid 等于 2。 The actual meaning of the keys is up to the user. Advisory locks are local to each database, so the database column is meaningful for an advisory lock.

pg_locks提供了集簇中所有锁的全局视图,不仅包括与当前数据库相关的锁。 虽然它的relation列可以与pg_class.oid 连接来识别被锁定的关系,但这仅对当前数据库中的关系有效(即 database 列为当前数据库的 OID 或零的那些关系)。

pid 列可以连接到 pg_stat_activity 视图的 pid 列,以获得关于持有或等待持有每个锁的会话的更多信息,例如

SELECT * FROM pg_locks pl LEFT JOIN pg_stat_activity psa
    ON pl.pid = psa.procpid;

Also, if you are using prepared transactions, the virtualtransaction column can be joined to the transaction column of the pg_prepared_xacts view to get more information on prepared transactions that hold locks. (A prepared transaction can never be waiting for a lock, but it continues to hold the locks it acquired while running.) For example:

SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
    ON pl.virtualtransaction = '-1/' || ppx.transaction;

pg_locks视图显示来自常规锁管理器和谓词锁管理器的数据,这两个是独立的系统; 此外,常规锁管理器将其锁分为常规锁和快速路径锁。 不能保证这些数据完全一致。 当查询该视图时, 快速路径锁的数据(具有fastpath = true) 从每个后端逐个收集,而不会冻结整个锁管理器的状态,因此在收集信息时可能会发生锁的获取或释放。 但请注意,这些锁已知不会与当前放置的任何其他锁发生冲突。 在查询所有后端的快速路径锁后,剩余的常规锁管理器将作为一个单元被锁定,并且所有剩余锁的一致快照将作为一个原子操作收集。 解锁常规锁管理器后,谓词锁管理器类似地被锁定,并且所有谓词锁将作为一个原子操作收集。 因此,除了快速路径锁外,每个锁管理器将提供一致的结果集,但由于我们不同时锁定两个锁管理器,因此在询问常规锁管理器后和在询问谓词锁管理器前,可能会发生锁的获取或释放。

如果这个视图被非常频繁地访问,锁定常规和(或)谓词锁管理器可能会对数据库性能产生一些影响。 锁仅在获取来自锁管理器的数据所需的最短时间内保持,但这并不能完全消除性能影响的可能性。

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。