postgres数据库表空间收缩之pg |
您所在的位置:网站首页 › pg库查看表所属表空间 › postgres数据库表空间收缩之pg |
#数据库表空间收缩之pg_squeeze,pg_repack 文章目录 pg_squeeze1.2原理优点安装使用pgstattuple临时处理监控方式注意事项squeeze1.2和低版本的区别 pg_repack原理安装使用方法测试系统表在线pg_repackrepack数据库repack模式repack表和索引repack所有索引repack指定索引 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 =../bin/pg_config2、安装 make && make install3、修改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 | | not null | 8 vacuum_max_age | interval | | not null | '01:00:00'::interval max_retry | integer | | not null | 0 skip_analyze | boolean | | not null | false Indexes: "tables_pkey" PRIMARY KEY, btree (id) "tables_tabschema_tabname_key" UNIQUE CONSTRAINT, btree (tabschema, tabname) Check constraints: "tables_free_space_extra_check" CHECK (free_space_extra >= 0 AND free_space_extra 0.0::double precision) Referenced by: TABLE "squeeze.tables_internal" CONSTRAINT "tables_internal_table_id_fkey" FOREIGN KEY (table_id) REFERENCES squeeze.tables(id) ON DELETE CASCADE TABLE "squeeze.tasks" CONSTRAINT "tasks_table_id_fkey" FOREIGN KEY (table_id) REFERENCES squeeze.tables(id) ON DELETE CASCADE Triggers: tables_internal_trig AFTER INSERT ON squeeze.tables FOR EACH ROW EXECUTE PROCEDURE squeeze.tables_internal_trig_func()squeeze.tables表字段说明 tabschema:表的模式名。tabname:表名。clustering_index:表示重建表时,表数据的物理顺序按照该索引进行聚簇。rel_tablespace:表示表重建时,移动到哪个表空间中。ind_tablespace:这个一个二维数组,表示索引和表空间的映射关系。schedule:vacuum在一天中运行的时间范围free_space_extra:表示空闲空间超过多少时就会对表进行重建,默认是50。min_size:表必须占用的最小磁盘空间(兆字节)才有资格进行处理,默认值为8。vacuum_max_age:当进行一次vacuum后,认为fsm是有效的最大时间,默认1小时。max_retry:当重建表失败时最大的重新尝试的次数,默认是0.skip_analyse:跳过对表进行analyse,默认是false。3、创建测试表 --创建表 postgres=# create table test(n_id int,c_name varchar(300),primary key(n_id)); CREATE TABLE --初始化数据 postgres=# insert into test select generate_series(1,4000000),'zhangsan'; INSERT 0 4000000 --查看表大小:169MB postgres=# \dt+ test List of relations Schema | Name | Type | Owner | Size | Description --------+------+-------+-------+--------+------------- public | test | table | sa | 169 MB | (1 row)4、给表test创建squeeze任务 --需要在表squeeze.tables插入一条记录。添加后,将定期检查表的统计信息。只要满足‘压缩’的太偶见,就会将‘任务’添加到队列中,任务按照创建爱女顺序依次处理 --schedule标识该任务在晚上八点到24点执行,并且free_space_extra表示空闲空间超过10时就会对表进行重建 postgres=# insert into squeeze.tables (tabschema, tabname, schedule, free_space_extra) values ('public', 'test', '{20:00, 24:00}', '10'); INSERT 0 1 --如果需要取消注册表,只需要从‘squeeze.tables’表删除响应的行即可 --查看任务 postgres=# select * from squeeze.tables; id | tabschema | tabname | clustering_index | rel_tablespace | ind_tablespaces | schedule | free_space_extra | min_size | vacuum_max_age | max_retry | skip_analyze ----+-----------+---------+------------------+----------------+-----------------+---------------------------+------------------+----------+----------------+-----------+-------------- 2 | public | test | | | | {20:00:00+08,24:00:00+08} | 10 | 8 | 01:00:00 | 0 | f (1 row)5、启动和关闭pg_squeeze进程 select squeeze.start_worker(); select squeeze.stop_worker();6、验证 --更新数据 postgres=# update test set c_name = '张三-1' where n_id |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |