近期,生产环境出现不同库下的同一表名(小表-几十条数据)的占用size已达近G,频繁修改,删除记录,但是空间一直未释放,是何原因?
原因就在于vacuum,而vacuum怎么存储,清理数据的可参考官方文档进行查看。
PG vacuum官方文档
https://www.postgresql.org/docs/current/routine-vacuuming.html
为了便于大家选择工具,简单做了一个对比说明供参考。
pg_repack | pgcompacttable | |
是否需要保证性能 | 否 | 是 |
是否移动表/索引 | 是 | 否 |
是否有足够空间 | 是 | 否 |
压缩速率是否高 | 是 | 否 |
小结:磁盘空间有限,因而经常选择使用pgcompacttable较多, 那演示一下pgcompacttable吧。
cat etc/redhat-releaseRed Hat Enterprise Linux Server release 7.4 (Maipo)su - postgrespsql -c "select version();"version------------------------------------------------------------------------------------------------PostgreSQL 12.4,FlyingDB on x86_64-pc-linux-gnu, compiled by gcc (GCC) 10.2.1 20210220, 64-bit(1 row)# FlyingDB 飞象数据find home/postgres/ -name *stattuple*/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple.so/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple.control/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--1.4.sql/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--1.4--1.5.sql/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--1.3--1.4.sql/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--1.2--1.3.sql/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--1.1--1.2.sql/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--1.0--1.1.sql/home/postgres/FlyingDB12.4/xxxxxx/pgstattuple--unpackaged--1.0.sql/home/postgres/xxxxxxxxxxxxxxxxxxx/pgstattuple.sopsqlselect * from pg_available_extensions where name like 'pgstat%';name | default_version | installed_version | comment-------------+-----------------+-------------------+-----------------------------pgstattuple | 1.5 | | show tuple-level statistics(1 row)postgres# \c oldold=# create extension pgstattuple;old=# \dxList of installed extensionsName | Version | Schema | Description-------------+---------+------------+------------------------------pgstattuple | 1.5 | public | show tuple-level statisticsplpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language(2 rows)old=# \dxS+ pgstattupleObjects in extension "pgstattuple"Object description---------------------------------------function pg_relpages(regclass)function pg_relpages(text)function pgstatginindex(regclass)function pgstathashindex(regclass)function pgstatindex(regclass)function pgstatindex(text)function pgstattuple_approx(regclass)function pgstattuple(regclass)function pgstattuple(text)(9 rows)yum install perl-Time-HiRes perl-DBI perl-DBD-Pg -ysu - postgresgit clone https://github.com/dataegret/pgcompacttable.gitunzip 解压
2.2 准备环境
create database old;use old;begin;insert into test values(generate_series(1,10000),repeat( chr(int4(random()*26)+65),1),repeat( chr(int4(random()*26)+65),6),repeat( chr(int4(random()*26)+65),30),repeat( chr(int4(random()*26)+65),30));commit;create index on test(id,sex);create index on test(name,now_address,address);old=# select count(*) from test ;count-------10000(1 row)old=# \d+List of relationsSchema | Name | Type | Owner | Size | Description--------+------+-------+----------+---------+-------------public | test | table | postgres | 1096 kB |(1 row)old=# select * from test limit 10 ;id | sex | name | now_address | address----+-----+--------+--------------------------------+--------------------------------1 | P | WWWWWW | [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ | YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY2 | X | ZZZZZZ | KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK | QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ3 | U | SSSSSS | QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ | KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK4 | R | ZZZZZZ | MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM | BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5 | B | [[[[[[ | LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL | ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ6 | I | XXXXXX | UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU | PPPPPPPPPPPPPPPPPPPPPPPPPPPPPP7 | W | EEEEEE | TTTTTTTTTTTTTTTTTTTTTTTTTTTTTT | UUUUUUUUUUUUUUUUUUUUUUUUUUUUUU8 | F | JJJJJJ | MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM | BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB9 | X | EEEEEE | ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ | HHHHHHHHHHHHHHHHHHHHHHHHHHHHHH10 | T | ZZZZZZ | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | PPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
2.3 模拟修改数据
#!/bin/bash#version: 1.0#function: bulk update data#author: ansel.zhangfor((i=1;i<=10000;i++));doa=`tr -dc A-Z[ < dev/urandom | head -c1`psql -Upostgres -h 127.0.0.1 -d old -c "update test set name=repeat( chr(int4(random()*26)+65),6),now_address=repeat( chr(int4(random()*26)+65),30),address=repeat( chr(int4(random()*26)+65),30) where sex='$a';" >>/dev/nullif [ $? == 0 ]thenecho "$a is ok" >>update.logelseecho "$a is close" >>update.err.logexit 1;fidone
2.4 膨胀再现
old=# \xExpanded display is on.old=# SELECT * FROM pgstattuple('public.test');-[ RECORD 1 ]------+---------table_len | 93306880tuple_count | 10396tuple_len | 1029204tuple_percent | 1.1 tuple_len/table_len*100%dead_tuple_count | 16735dead_tuple_len | 1656765dead_tuple_percent | 1.78 dead_tuple_len/table_len*100%free_space | 73878732free_percent | 79.18 free_space/table_len*100%pgstattuple 输出列如下:字段 类型 描述table_len bigint 物理关系长度,以字节计tuple_count bigint 活的元组的数量tuple_len bigint 活的元组的总长度,以字节计tuple_percent float8 活的元组的百分比dead_tuple_count bigint 死的元组的数量dead_tuple_len bigint 死的元组的总长度,以字节计dead_tuple_percent float8 死的元组的百分比free_space bigint 空闲空间总量,以字节计free_percent float8 空闲空间的百分比old=# \d+List of relationsSchema | Name | Type | Owner | Size | Description--------+------+-------+----------+-------+-------------public | test | table | postgres | 89 MB |(1 row)存1w条数据原表大小1096 KB,目前89MB
./pgcompacttable -h localhost -U postgres -d testdb./pgcompacttable -h localhost -U postgres -d testdb -n public./pgcompacttable -h localhost -U postgres -d old -n public -t test[Thu Apr 22 11:29:16 2021] (old) Connecting to database[Thu Apr 22 11:29:16 2021] (old) Postgres backend pid: 9022[Thu Apr 22 11:29:16 2021] (old) Handling tables. Attempt 1[Thu Apr 22 11:30:50 2021] (old:public.test) Statistics: 11390 pages (52732 pages including toasts and indexes), it is expected that ~86.490% (9850 pages) can be compacted with the estimated space saving being 76.959MB.[Thu Apr 22 11:31:50 2021] (old:public.test) Progress: 67%, 6690 pages completed.[Thu Apr 22 11:32:50 2021] (old:public.test) Progress: 107%, 10570 pages completed.[Thu Apr 22 11:33:50 2021] (old:public.test) Reindex: public.test_id_sex_idx, initial size 6411 pages(50.086MB), has been reduced by 99% (49.852MB), duration 0 seconds, attempts 0.[Thu Apr 22 11:33:50 2021] (old:public.test) Reindex: public.test_name_now_address_address_idx, initial size 34925 pages(272.852MB), has been reduced by 99% (271.922MB), duration 0 seconds, attempts 0.[Thu Apr 22 11:33:50 2021] (old:public.test) Processing results: 150 pages left (303 pages including toasts and indexes), size reduced by 87.812MB (409.602MB including toasts and indexes) in total.[Thu Apr 22 11:33:50 2021] (old) Processing complete.[Thu Apr 22 11:33:50 2021] (old) Processing results: size reduced by 87.812MB (409.602MB including toasts and indexes) in total.[Thu Apr 22 11:33:50 2021] (old) Disconnecting from database[Thu Apr 22 11:33:50 2021] Processing complete: 1 retries to process has been done[Thu Apr 22 11:33:50 2021] Processing results: size reduced by 87.812MB (409.602MB including toasts and indexes) in total, 87.812MB (409.602MB) old.old=# \d+List of relationsSchema | Name | Type | Owner | Size | Description--------+------+-------+----------+---------+-------------public | test | table | postgres | 1356 kB |(1 row)
上一篇: CllickHouse 部署架构和国内大厂应用实践
近期文章推荐:
ClickHouse (MATERIALIZED) VIEW
更多精彩内容欢迎关注微信公众号

文章转载自 ClickHouse周边,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




