【PostgreSQL PGCE-091题目解析14】PostgreSQL中使用CONCURRENTLY选项创建索引时,允许增删改数据表。

本文为++云贝教育刘峰(V:yunbee_DBA)原创,请尊重知识产权,转发请注明出处,不接受++

++任何抄袭、演绎和未经注明出处的转载。++

PostgreSQL中使用CONCURRENTLY选项创建索引时,允许增删改数据表。

A. 正确

B. 错误

参考答案:A


解析:

我们知道,PG是有行级琐的,在创建索引的时候,会在行上加琐

实验如下:

1、准备目标数据

复制代码
drop table if exists t1;
create table t1 ( a int, b varchar(50));
insert into t1
select a.*, md5(a::varchar) from generate_series(1,5000000) a;

2、在表上创建索引,不加CONCURRENTLY

复制代码
testdb=# create index idx_t1_a3 on t1(a);
CREATE INDEX

3、修改表数据

复制代码
testdb=# begin;
BEGIN
testdb=*# select pg_backend_pid();
 pg_backend_pid
----------------
          32600
(1 row)

testdb=*# select txid_current();
 txid_current
--------------
          759
(1 row)

testdb=*# update t1 set a=55 where a=6;
。。。等待中

发现修改不了

4、查看此时的琐

复制代码
testdb=# select locktype,database,relation::regclass,transactionid,pid,mode from pg_locks order by pid;
      locktype | database | relation | transactionid |  pid  |   mode
---------------+----------+----------+---------------+-------+---------------------
relation       | 16389    | pg_locks |               | 30854 | AccessShareLock
virtualxid     |          |          |               | 30854 | ExclusiveLock
virtualxid     |          |          |               | 32410 | ExclusiveLock
relation       | 16389    | 16411    |               | 32410 | AccessExclusiveLock
transactionid  |          |          |           760 | 32410 | ExclusiveLock
relation       | 16389    | t1       |               | 32410 | ShareLock
virtualxid     |          |          |               | 32600 | ExclusiveLock
relation       | 16389    | t1       |               | 32600 | RowExclusiveLock
transactionid  |          |          |           759 | 32600 | ExclusiveLock
(9 rows)

5、以CONCURRENTLY模式创建索引

复制代码
testdb=# create index concurrently idx_t1_a41 on t1(a);
CREATE INDEX

6、修改表数据

复制代码
testdb=# update t1 set a=55 where a=6;
UPDATE 2

发现很顺利修改成功

7、查看此时的琐情况

复制代码
testdb=# select locktype,database,relation::regclass,transactionid,pid,mode from pg_locks order by pid;
      locktype | database |  relation  | transactionid |  pid  |  mode
---------------+----------+------------+---------------+-------+--------------------------
virtualxid     |          |            |               | 30854 | ExclusiveLock
relation       |    16389 | pg_locks   |               | 30854 | AccessShareLock
relation       |    16389 | idx_t1_a5  |               | 35304 | RowExclusiveLock
relation       |    16389 | idx_t1_a4  |               | 35304 | RowExclusiveLock
relation       |    16389 | idx_t1_a3  |               | 35304 | RowExclusiveLock
relation       |    16389 | idx_t1_a2  |               | 35304 | RowExclusiveLock
relation       |    16389 | idx_t1_a1  |               | 35304 | RowExclusiveLock
relation       |    16389 | idx_t1_a   |               | 35304 | RowExclusiveLock
relation       |    16389 | t1         |               | 35304 | RowExclusiveLock
relation       |    16389 | idx_t1_a41 |               | 35304 | RowExclusiveLock
transactionid  |          |            |           773 | 35304 | ExclusiveLock
virtualxid     |          |            |               | 35304 | ExclusiveLock
virtualxid     |          |            |               | 35470 | ShareLock
relation       |    16389 | t1         |               | 35470 | ShareUpdateExclusiveLock
virtualxid     |          |            |               | 35470 | ExclusiveLock

结论:PG在创建索引使用concurrently参数时,不影响DML操作,反而是DML操作会影响 concurrently索引创建。

PostgreSQL PGCE考试为理论考试,需通过两门考试才能拿到"PostgreSQL PGCE证书"。

相关推荐
韩立学长4 分钟前
基于Springboot课堂教学辅助系统08922bq1(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
goxingman1 小时前
Oracle视图基础
数据库·oracle
黎相思2 小时前
MySQL索引特性
数据库·mysql
rit84324992 小时前
压缩感知信号恢复算法:OMP与CoSaMP对比分析
数据库·人工智能·算法
Forget_85502 小时前
RHCE复习作业4
数据库
f***28142 小时前
【零基础学Mysql】常用函数讲解,提升数据操作效率的利器
数据库·mysql
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot+vue的学校课程管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
Elastic 中国社区官方博客3 小时前
ES|QL 在 9.2:智能查找连接和时间序列支持
大数据·数据库·人工智能·sql·elasticsearch·搜索引擎·全文检索
q***01653 小时前
PostgreSQL 17 发布了!非常稳定的版本
数据库·postgresql