首页 > 数据库 >postgresql 锁类型

postgresql 锁类型

时间:2022-12-06 16:48:24浏览次数:36  
标签:postgresql 21 19 AccessShareLock relation 16384 类型 27353

锁类型 对应的数据库操作
ACCESS SHARE select
ROW SHARE select for update, select for share
ROW EXCLUSIVE update,delete,insert
SHARE UPDATE EXCLUSIVE vacuum(without full),analyze,create index concurrently
SHARE create index
SHARE ROW EXCLUSIVE 任何Postgresql命令不会自动获得这种锁,创建trigger和某些形式的alter table会获得该锁
EXCLUSIVE 任何Postgresql命令不会自动获得这种类型的锁 ,刷新雾化视图获得该锁
ACCESS EXCLUSIVE alter table,drop table,truncate,reindex,cluster,vacuum full
1.ACCESS SHARE

select语句会获取该锁

 "ACCESS SHARE"锁模式只与"ACCESS EXCLUSIVE" 锁模式冲突;
即select与"ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL"冲突

session 1
aaa=# select * from pg_locks ;
  locktype  | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |      mode       | granted | fastpath
------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+-----------------+---------+----------
 relation   |    16384 |    11653 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2685 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2684 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     3455 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2663 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2662 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2615 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     1259 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 virtualxid |          |          |      |       | 4/19       |               |         |       |          | 4/19               | 27353 | ExclusiveLock   | t       | t
(9 rows)

aaa=# select * from t1;
 i | j
---+---
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 1 | 2
 2 | 2
 3 | 2
 3 | 3
 3 | 4
 3 | 5
(12 rows)

aaa=# select * from pg_locks ;
  locktype  | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |      mode       | granted | fastpath
------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+-----------------+---------+----------
 relation   |    16384 |    16388(加锁的表) |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t  //此处可以看到加的ACCESS SHARE锁
 relation   |    16384 |    11653(pg_locks表) |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2685 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2684 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     3455 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2663 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2662 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     2615 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 relation   |    16384 |     1259 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock | t       | t
 virtualxid |          |          |      |       | 4/19       |               |         |       |          | 4/19               | 27353 | ExclusiveLock   | t       | t



session 2

aaa=# alter table t1 add column k varchar(10);

session 3

 select * from pg_locks;
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |        mode         | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+---------------------+---------+----------
 relation      |    13321 |    11653 |      |       |            |               |         |       |          | 3/209              | 27713 | AccessShareLock     | t       | t
 virtualxid    |          |          |      |       | 3/209      |               |         |       |          | 3/209              | 27713 | ExclusiveLock       | t       | t
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2685 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2684 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     3455 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2663 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2662 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2615 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     1259 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | t
 virtualxid    |          |          |      |       | 4/19       |               |         |       |          | 4/19               | 27353 | ExclusiveLock       | t       | t
 virtualxid    |          |          |      |       | 5/131      |               |         |       |          | 5/131              | 27624 | ExclusiveLock       | t       | t
 transactionid |          |          |      |       |            |           618 |         |       |          | 5/131              | 27624 | ExclusiveLock       | t       | f
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/19               | 27353 | AccessShareLock     | t       | f  session 1 select获取到的锁
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 5/131              | 27624 | AccessExclusiveLock | f       | f  session 3 alter table需要获取ACCESS EXCLUSIVE锁,两个锁模式冲突,所以session 3未获取到锁,grant为f
(15 rows)

2.ROW SHARE (该实验测试删掉了上面加的k列)
select for update, select for share 会获取该锁
"Row Share" 锁模式与"Exclusive’和"Access Exclusive"锁模式冲突;

aaa=# select * from pg_locks ;
  locktype  | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |      mode       | granted | fastpath
------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+-----------------+---------+----------
 relation   |    16384 |    11653 |      |       |            |               |         |       |          | 5/138              | 27624 | AccessShareLock | t       | t
 virtualxid |          |          |      |       | 5/138      |               |         |       |          | 5/138              | 27624 | ExclusiveLock   | t       | t
(2 rows)

aaa=# begin;
BEGIN
aaa=# select * from t1 for update;
 i | j
---+---
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 1 | 2
 2 | 2
 3 | 2
 3 | 3
 3 | 4
 3 | 5
(12 rows)

aaa=# select * from pg_locks ;
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |      mode       | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+-----------------+---------+----------
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 5/139              | 27624 | AccessShareLock | t       | t
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 5/139              | 27624 | RowShareLock    | t       | t  //获取表的rowshare锁
 virtualxid    |          |          |      |       | 5/139      |               |         |       |          | 5/139              | 27624 | ExclusiveLock   | t       | t
 transactionid |          |          |      |       |            |           621 |         |       |          | 5/139              | 27624 | ExclusiveLock   | t       | f
(4 rows)

3.ROW EXCLUSIVE

 "Row exclusive" 与 "Share,Shared row exclusive,Exclusive,Access exclusive"模式冲突;即DML与"CREATE INDEX","CREATE TRIGGER","REFRESH ATERIALIZED VIEW CONCURRENTLY","ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL"冲突
 "Update,Delete,Insert"命令会在目标表上获得这种类型的锁,并且在其它被引用的表上加上"Access shared"锁,一般地,更改表数据的命令都将在这张表上获得"Row exclusive"锁。
只示范delete与alter table锁冲突,即"Row exclusive" 与 "Access exclusive"

session 1
aaa=# begin;
BEGIN
aaa=# select * from t1;
 i | j
---+---
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 3 | 1
 1 | 2
 2 | 2
 3 | 2
 3 | 3
 3 | 4
 3 | 5
(12 rows)

aaa=# delete from t1 where j=4;
DELETE 1
aaa=# select * from pg_locks ;
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |       mode       | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+------------------+---------+----------
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     3455 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2663 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2662 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2685 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2684 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2615 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     1259 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/21               | 27353 | RowExclusiveLock | t       | t
 virtualxid    |          |          |      |       | 4/21       |               |         |       |          | 4/21               | 27353 | ExclusiveLock    | t       | t
 virtualxid    |          |          |      |       | 5/140      |               |         |       |          | 5/140              | 27624 | ExclusiveLock    | t       | t
 transactionid |          |          |      |       |            |           622 |         |       |          | 4/21               | 27353 | ExclusiveLock    | t       | f
(13 rows)


session 2

aaa=# begin;
BEGIN
aaa=# select * from pg_locks ;
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |       mode       | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+------------------+---------+----------
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 6/108              | 29310 | AccessShareLock  | t       | t
 virtualxid    |          |          |      |       | 6/108      |               |         |       |          | 6/108              | 29310 | ExclusiveLock    | t       | t
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     3455 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2663 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2662 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2685 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2684 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     2615 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |     1259 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock  | t       | t  第一次执行select获得的锁
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/21               | 27353 | RowExclusiveLock | t       | t  第二次执行delete所获得的锁
 virtualxid    |          |          |      |       | 4/21       |               |         |       |          | 4/21               | 27353 | ExclusiveLock    | t       | t
 transactionid |          |          |      |       |            |           622 |         |       |          | 4/21               | 27353 | ExclusiveLock    | t       | f
(14 rows)

aaa=# alter table t1 add column k varchar(10);


session 1
aaa=# select * from pg_locks ;
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |        mode         | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+---------------------+---------+----------
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 6/108              | 29310 | AccessShareLock     | t       | t
 virtualxid    |          |          |      |       | 6/108      |               |         |       |          | 6/108              | 29310 | ExclusiveLock       | t       | t
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     3455 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2663 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2662 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2685 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2684 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     2615 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 relation      |    16384 |     1259 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | t
 virtualxid    |          |          |      |       | 4/21       |               |         |       |          | 4/21               | 27353 | ExclusiveLock       | t       | t
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 6/108              | 29310 | AccessExclusiveLock | f       | f alter  table所需的锁
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/21               | 27353 | AccessShareLock     | t       | f
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/21               | 27353 | RowExclusiveLock    | t       | f
 transactionid |          |          |      |       |            |           623 |         |       |          | 6/108              | 29310 | ExclusiveLock       | t       | f
 transactionid |          |          |      |       |            |           622 |         |       |          | 4/21               | 27353 | ExclusiveLock       | t       | f

4 SHARE UPDATE EXCLUSIVE
"Share update exclusive,Share,Share row exclusive,exclusive,Access exclusive"模式冲突,这种模式保护一张表不被并发的模式更改和VACUUM;
"Vacuum(without full), Analyze "和 "Create index concurrently"命令会获得这种类型锁。
即"Vacuum(without full), Analyze "和 "Create index concurrently"与"CREATE INDEX","CREATE TRIGGER","REFRESH ATERIALIZED VIEW CONCURRENTLY","ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL"冲突

aaa=# select * from pg_locks ;
  locktype  | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |      mode       | granted | fastpath
------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+-----------------+---------+----------
 relation   |    16384 |    11653 |      |       |            |               |         |       |          | 4/23               | 27353 | AccessShareLock | t       | t
 virtualxid |          |          |      |       | 4/23       |               |         |       |          | 4/23               | 27353 | ExclusiveLock   | t       | t
(2 rows)

aaa=# begin
aaa-# ;
BEGIN
aaa=# Analyze t1;
ANALYZE
aaa=# select * from pg_locks ;
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |           mode           | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+--------------------------+---------+----------
 relation      |    16384 |    11653 |      |       |            |               |         |       |          | 4/24               | 27353 | AccessShareLock          | t       | t
 virtualxid    |          |          |      |       | 4/24       |               |         |       |          | 4/24               | 27353 | ExclusiveLock            | t       | t
 transactionid |          |          |      |       |            |           624 |         |       |          | 4/24               | 27353 | ExclusiveLock            | t       | f
 relation      |    16384 |    16388 |      |       |            |               |         |       |          | 4/24               | 27353 | ShareUpdateExclusiveLock | t       | f  analyze表获得锁

5.SHARE
  与"Row exclusive,Shared update exclusive,Share row exclusive ,Exclusive,Access exclusive"锁模式冲突,这种模式保护一张表数据不被并发的更改;
  "Create index"命令会获得这种锁模式。
即 "Create index"与, "Update,Delete,Insert","Vacuum(without full), Analyze ","CREATE TRIGGER","REFRESH ATERIALIZED VIEW CONCURRENTLY","ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL"冲突

6.SHARE ROW EXCLUSIVE
与"Row exclusive,Share update exclusive,Shared,Shared row exclusive,Exclusive,Access Exclusive"锁模式冲突;
  任何Postgresql 命令不会自动获得这种锁。创建trigger和某些形式的alter table会获得该锁
7. EXCLUSIVE
  与" ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, ACCESS EXCLUSIVE"模式冲突,这种索模式仅能与Access Share 模式并发,换句话说,只有读操作可以和持有"EXCLUSIVE"锁的事务并行;
  任何Postgresql 命令不会自动获得这种类型的锁;只有再刷新物化视图的时候获得该锁,刷新雾化视图只可以刷新
8. ACCESS EXCLUSIVE
  与所有模式锁冲突(ACCESS SHARE, ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE),这种模式保证了当前只有一个事务访问这张表;
  "ALTER TABLE, DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL" 命令会获得这种类型锁,在Lock table 命令中,如果没有申明其它模式,它也是缺省模式。

标签:postgresql,21,19,AccessShareLock,relation,16384,类型,27353
From: https://www.cnblogs.com/nanblog/p/16955717.html

相关文章

  • UEC++ 会话暴露给蓝图 ,无法识别会话相关类型
    在实在联网功能时,我们常常需要获取到会话类型,当我们需要在蓝图中使用这些类型的时候。如果我们直接在头文件中直接使用会话相关类型在编译时就会报错Unrecognizedtype......
  • PostgreSQL和MySQL的优劣对比
    在开发项目的过程中,难免要面对选择数据库的情况。总结此文章是因为在之前公司里使用的都是MYSQL数据库,而在现在公司里,新项目中使用的是PostgreSQL数据库,在使用过程中,经......
  • PostgreSql和MySql数据类型之间的比较以及推荐
    文章介绍了postgresql和MySql之间数据类型的比较,以及推荐使用。因为存在数据库表迁移等场景,会更需要此类情况的对比1.数值类型的比较整数:mysql中的整数类型和pg......
  • PostgreSQL会话及阻塞相关常用SQL
    ====PostgreSQL===========select*frompg_settings;selectversion(),txid_current(),pg_backend_pid(),current_user,current_schema,current_timestamp;--......
  • Go--基本类型
    摘抄自:https://www.topgoer.cn/docs/golang/chapter03-8基本类型介绍Golang更明确的数字类型命名,支持Unicode,支持常用数据结构。类型长度(字节)默认值说明bool......
  • C# 元组类型和元组文本
    从C#7.0开始,可以使用元组类型和元组文本轻松实现此目的。元组类型定义元组元素的数据类型。元组文本提供返回的元组的实际值。在下面的示例中,(string,string,str......
  • 数据模型的类型的约束
    基于描述符建立数据模型实现赋值验证框架在实例属性的获取和设定方面,前面已经提过可以使用property和描述符实现1、创建数据模型的基础构建模块1classDescriptor:......
  • 【Python小随笔】将str类型的list列表,转换成List类型
    str_list="['001678,英大国企改革主题股票,YDGQGGZTGP,2022-12-05,1.6577,2.3077,1.15,4.62,3.72,1.57,15.88,34.04,46.95,115.66,33.43,152.94,2018-11-22,1,34.61......
  • java中的clone()方法的研究---(3)如何编写正确的clone()方法:基本数据类型
    先来看看一个自定义Object,它里面的属性都有啥:基本数据类型WrapperClass(基本数据类型的包装类型)StringStringBuffer,StringBuilderJava提供的其他类型DateTimestampArr......
  • [c++11新特性]02-自动类型推导
    自动类型推导在c++11中,关于类型推导的关键字有using,decltype,auto,typeid。在c++11之前我们要声明一个类型的别名往往使用的是typedef,但是该关键字在模板类型推导中存在一......