• 【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证书”。

  • 相关阅读:
    软设上午题错题知识点8
    buildadmin+tp8表格操作(7.1)表格的事件监听(el-table中的事件)
    算法通过村第八关-树(深度优先)白银笔记|深度和高度问题
    电子学会青少年软件编程 Python编程等级考试二级真题解析(选择题)2021年9月
    C/C++开发,libiec61850库学习及运用
    Acwing 周赛135 解题报告 | 珂学家 | 反悔堆贪心
    2021年11月数据库流行度排行:openGauss 跃居第三,人大金仓晋身前十
    【科普分享】linux服务器文件挂载技术介绍——mount
    基于微信小程序的智能停车场管理系统设计与实现-计算机毕业设计源码+LW文档
    win10 修改MySQL8 wait_timeout 等超时和连接数配置
  • 原文地址:https://blog.csdn.net/hdhdhdk/article/details/137261311