cluster 重新组织表

os: ubuntu 16.04
db: postgresql 9.6.8
pg_repack: 1.4.4

CLUSTER 是根据一个索引聚簇一个表,优点是使用这个索引高效,确定是可能会影响其它索引的效率。

语法

peiybdb=# \h cluster
Command:     CLUSTER
Description: cluster a table according to an index
Syntax:
CLUSTER [VERBOSE] table_name [ USING index_name ]
CLUSTER [VERBOSE]

实验

peiybdb=# \d+ tmp_t0;
                       Table "public.tmp_t0"
 Column |  Type  | Modifiers | Storage | Stats target | Description 
--------+--------+-----------+---------+--------------+-------------
 c0     | bigint |           | plain   |              | 
 c1     | bigint | not null  | plain   |              | 
Indexes:
    "tmp_t0_pk" PRIMARY KEY, btree (c1)

peiybdb=# cluster verbose public.tmp_t0 using tmp_t0_pk;
INFO:  clustering "public.tmp_t0" using index scan on "tmp_t0_pk"
INFO:  "tmp_t0": found 0 removable, 2999983 nonremovable row versions in 16217 pages
DETAIL:  0 dead row versions cannot be removed yet.
CPU 0.20s/7.75u sec elapsed 9.06 sec.
CLUSTER
peiybdb=# 

执行 CLUSTER 时的锁

peiybdb=# select oid,relname from pg_class where relname in ('tmp_t0','tmp_t0_pk');
   oid   |  relname  
---------+-----------
 1447752 | tmp_t0_pk
 1447603 | tmp_t0
(2 rows)



peiybdb=# select * from pg_locks where pid = '29475';
   locktype    | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction |  pid  |        mode         | granted | fastpath 
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+---------------------+---------+----------
 virtualxid    |          |          |      |       | 3/4278     |               |         |       |          | 3/4278             | 29475 | ExclusiveLock       | t       | t
 relation      |  1406609 |  1453912 |      |       |            |               |         |       |          | 3/4278             | 29475 | AccessExclusiveLock | t       | f
 relation      |  1406609 |  1447752 |      |       |            |               |         |       |          | 3/4278             | 29475 | AccessShareLock     | t       | f
 relation      |  1406609 |  1447752 |      |       |            |               |         |       |          | 3/4278             | 29475 | AccessExclusiveLock | t       | f
 relation      |  1406609 |  1447603 |      |       |            |               |         |       |          | 3/4278             | 29475 | ShareLock           | t       | f
 relation      |  1406609 |  1447603 |      |       |            |               |         |       |          | 3/4278             | 29475 | AccessExclusiveLock | t       | f
 transactionid |          |          |      |       |            |     454353304 |         |       |          | 3/4278             | 29475 | ExclusiveLock       | t       | f
(7 rows)

可以看到在对表做 cluster 时,表的锁为 ShareLock、AccessExclusiveLock。
有 AccessExclusiveLock 时,表是做不了任何操作的,包括select,在生产环境慎用。最好使用 pg_repack 操作。

参考:
http://postgres.cn/docs/9.6/sql-cluster.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库人生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值