postgres数据库表空间收缩之pg_squeeze,pg_repack
目录
下半年一直忙于NP的sybase,通过大家的共同努力,NP年底比较稳定。很久没有弄过pg相关的知识了,最近经常看到有人问如何用工具自动清理pg的坏元组。
除了我们经常手动使用vacuum之外,生产环境还有两个比较常用的工具一个是pg_squeeze,另外一个是pg_repack
pg_squeeze1.2
项目地址:https://github.com/cybertec-postgresql/pg_squeeze
原理
pg_squeeze是一个扩展,它从表中删除未使用的空间,并且可以选择根据特定索引对元组进行排序,一般当一个表膨胀时一般使用vacuum full或者cluster进行表重建,在这一过程中会加排他锁,导致该表无法进行读写,只有等整个过程完成后才可以进行正常使用
优点
相比pg_repack或者pg_reorg,pg_squeeze不需要建触发器,所以在重组时对原表的DML几乎没有性能影响。pg_squeeze支持自动重组,可以设置定时清理时间以及根据空闲空间比例来进行清理表。该过程会自动启动worker进程,将数据复制到重组表,然后加锁,切换filenode。
安装
1、下载安装包后,解压后修改MakeFile,在MakeFile中加入pg_config
PG_CONFIG =/home/thunisoft5/arterybase/5.0/bin/pg_config
2、安装
make && make install
3、修改postgresql.conf配置文件
wal_level = logical
max_replication_slots = 1 # 大于等于1
shared_preload_libraries = 'pg_squeeze'
4、重启数据库
使用
1、创建扩展
postgres=# create extension pg_squeeze;
CREATE EXTENSION
postgres=# dx
List of installed extensions
Name | Version | Schema | Description
------------+---------+------------+------------------------------------------------
pg_squeeze | 1.2 | squeeze | A tool to remove unused space from a relation.
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
2、安装完成后会有一个对应的系统表
postgres=# d squeeze.tables
Table "squeeze.tables"
Column | Type | Collation | Nullable | Default
------------------+-----------------------+-----------+----------+--------------------------------------------
id | integer | | not null | nextval('squeeze.tables_id_seq'::regclass)
tabschema | name | | not null |
tabname | name | | not null |
clustering_index | name | | |
rel_tablespace | name | | |
ind_tablespaces | name[] | | |
schedule | time with time zone[] | | not null |
free_space_extra | integer | | not null | 50
min_size | real |