开源PostgreSQL爱好者,长期从事PostgreSQL数据库运维工作。
默认创建索引过程中由于需要请求ShareLock级别的锁,将会阻塞对表的修改操作,为了降低索引创建的影响,在PG中支持并发索引创建,该操作请求的锁级别为ShareUpdateExclusiveLock级别的表,允许对表的插入/更新/删除操作。
但是,并发创建索引也有自身需要注意的事项,由于不阻塞对表的修改操作,因此需要分多个步骤完成,这一点在手册上有详细说明,如下
并发创建索引需要分多个步骤完成,首先在一个事务中将相关索引信息记录到系统表中,但是将索引信息标记为非法状态,然后需要进行两次扫描,并且在最后需要等待第二次扫描之前产生的所有具有快照信息的事务结束,最后修改索引的状态信息为可用。
以上是手册上给出的并发索引创建的描述,理解起来不太容易,结合下面的例子业务会更加容易理解其过程。
create table test1(a int);
create table test2(a int);
Sessio1:
Begin;
Lock table test1;
Session2:
Begin;
Insert into test1 values(10);
Session3:
Create index concurrently b_idx on test2(a);
在上面的例子中,session3中并发索引创建将被阻塞,导致无法创建成功,阻塞的原因是前面描述中标注导致的。索引创建进程信息如下
postgres=# select * from pg_stat_activity where pid = 3561;
-[ RECORD 1 ]----+---------------------------------------------
datid | 13665
datname | postgres
pid | 3561
usesysid | 10
usename | postgres
application_name | psql
wait_event_type | Lock
wait_event | virtualxid
state | active
query | create index concurrently a_idx on test2(a);
从上面可以看到索引创建在等待虚拟事务ID的释放,进一步通过pg_locks视图可以找到持有该锁的进程。
postgres=#select relation,virtualxid,virtualtransaction,granted,pid from pg_locks where pid = 3561;
relation | virtualxid | virtualtransaction | granted | pid
----------+------------+--------------------+---------+------
| 5/8 | 5/8 | t | 3561
16387 | | 5/8 | t | 3561
| 4/2 | 5/8 | f | 3561
(3 rows)
postgres=# select relation,virtualxid,virtualtransaction,granted,pid from pg_locks where virtualxid='4/2' and granted = 't';
relation | virtualxid | virtualtransaction | granted | pid
----------+------------+--------------------+---------+------
| 4/2 | 4/2 | t | 3456
(1 row)
从上面结果可以看出,索引创建在等待进程3456的结束,对应操作为
postgres=# select * from pg_stat_activity where pid = 3456;
-[ RECORD 1 ]----+------------------------------
datid | 13665
datname | postgres
pid | 3456
usesysid | 10
usename | postgres
wait_event_type | Lock
wait_event | relation
state | active
backend_xmin | 516
query | insert into test1 values(10);
对应前面的session2操作,如果需要了解这其中的具体原因,需要仔细阅读索引创建函数DefineIndex,了解其中操作过程,下面给出整理的大致流程和等待进程的调用栈信息。

整数包括0吗:https://www.nanss.com/xuexi/18309.html 母爱的俗语:https://www.nanss.com/wenan/20036.html 李白的简介:https://www.nanss.com/xuexi/18584.html 无法识别的usb设备怎么解决:https://www.nanss.com/wenti/19795.html 昭君出塞的故事:https://www.nanss.com/yuedu/18373.html 圣诞树是什么树:https://www.nanss.com/jiaju/19004.html 韭菜的功效与作用:https://www.nanss.com/yinshi/19754.html 安全保卫工作总结:https://www.nanss.com/gongzuo/19255.html 体育教研组工作总结:https://www.nanss.com/gongzuo/18761.html 会议纪要范本:https://www.nanss.com/gongzuo/19574.html 左脑控制什么:https://www.nanss.com/yuer/18333.html sep是几月:https://www.nanss.com/wenti/19773.html 批评与自我批评范文:https://www.nanss.com/gongzuo/19228.html 10的因数有哪些:https://www.nanss.com/xuexi/18325.html 冬游作文:https://www.nanss.com/xuexi/19121.html 无常读后感:https://www.nanss.com/xuexi/19704.html 昆虫记被誉为:https://www.nanss.com/shenghuo/18958.html 揭牌仪式主持词:https://www.nanss.com/gongzuo/19992.html 16朵玫瑰代表什么意思:https://www.nanss.com/shenghuo/19956.html 漂白剂成分:https://www.nanss.com/shenghuo/19386.html 吉水在哪里属于哪个省:https://www.nanss.com/wenti/19964.html 世界人口排名前十名:https://www.nanss.com/shenghuo/19640.html 宠物名字大全:https://www.nanss.com/mingcheng/18558.html 史记是一部什么体史书:https://www.nanss.com/wenti/19933.html 四大名绣指的是:https://www.nanss.com/shenghuo/19443.html 商务信函:https://www.nanss.com/gongzuo/18652.html 装修设计方案:https://www.nanss.com/gongzuo/18674.html 埃菲尔铁塔在哪里:https://www.nanss.com/wenti/19044.html 举行活动:https://www.nanss.com/gongzuo/18435.html 快乐寒假作文:https://www.nanss.com/xuexi/19170.html