调整vacuum参数为默认值
select name, setting, short_desc from pg_settings
where name in (‘autovacuum’,
‘autovacuum_freeze_max_age’,
‘log_autovacuum_min_duration’,
‘vacuum_failsafe_age’,
‘vacuum_freeze_min_age’,
‘vacuum_freeze_table_age’
);
name | setting | short_desc -----------------------------+------------+-------------------------------------------------------------------------------- autovacuum | off | Starts the autovacuum subprocess. autovacuum_freeze_max_age | 200000000 | Age at which to autovacuum a table to prevent transaction ID wraparound. log_autovacuum_min_duration | 0 | Sets the minimum execution time above which autovacuum actions will be logged. vacuum_failsafe_age | 1600000000 | Age at which VACUUM should trigger failsafe to avoid a wraparound outage. vacuum_freeze_min_age | 50000000 | Minimum age at which VACUUM should freeze a table row. vacuum_freeze_table_age | 150000000 | Age at which VACUUM should scan whole table to freeze tuples.
复制
频繁增删改表,模拟表膨胀
call insert_trx(1500000); call update_trx(1500000); call delete_trx(1500000); call update_trx(1500000); call update_trx(1500000); call update_trx(1500000); call update_trx(1500000); call update_trx(1500000);
复制
查看表膨胀率
SELECT relname AS TABLE_NAME, pg_size_pretty(pg_relation_size(schemaname||'.'||relname)) AS table_size, n_dead_tup, n_live_tup, (n_dead_tup * 100 / (n_live_tup + n_dead_tup))AS dead_tup_ratio FROM pg_stat_user_tables WHERE n_dead_tup<>0 order by n_dead_tup LIMIT 10; table_name | table_size | n_dead_tup | n_live_tup | dead_tup_ratio ------------+------------+------------+------------+---------------- test_table | 103 MB | 2211853 | 0 | 100
复制
回收表膨胀表
pg_repack -t public.test_table -j 2 -d postgres
复制
pg_repack -t public.test_table -j 2 -d postgres NOTICE: Setting up workers.conns INFO: repacking table "public.test_table" SELECT relname AS TABLE_NAME, pg_size_pretty(pg_relation_size(schemaname||'.'||relname)) AS table_size, n_dead_tup, n_live_tup, (n_dead_tup * 100 / (n_live_tup + n_dead_tup))AS dead_tup_ratio FROM pg_stat_user_tables WHERE n_dead_tup<>0 order by n_dead_tup LIMIT 10; table_name | table_size | n_dead_tup | n_live_tup | dead_tup_ratio ------------+------------+------------+------------+---------------- (0 rows)
复制
需注意的是表需要有主键
postgres=# \d+ test_table Table "public.test_table" Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description --------+-------------------+-----------+----------+----------------------------------------+----------+-------------+------- -------+------------- id | integer | | not null | nextval('test_table_id_seq'::regclass) | plain | | | name | character varying | | | | extended | | | Indexes: "test_table_pkey" PRIMARY KEY, btree (id)
复制
如建一个无主键的表
drop table test_table; CREATE TABLE test_table ( id SERIAL , name VARCHAR ); call insert_trx(3000000); call update_trx(3000000); call delete_trx(3000000); call update_trx(3000000);
复制
pg_repack -t public.test_table -j 2 -d postgres
NOTICE: Setting up workers.conns WARNING: relation "public.test_table" must have a primary key or not-null unique keys
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
玩一玩系列——玩玩pg_mooncake(PostgreSQL的高性能列存新贵)
小满未满、
512次阅读
2025-03-03 17:18:03
王炸!OGG 23ai 终于支持从PostgreSQL备库抽取数据了
曹海峰
389次阅读
2025-03-09 12:54:06
玩一玩系列——玩玩login_hook(一款即将停止维护的PostgreSQL登录插件)
小满未满、
371次阅读
2025-03-08 18:19:28
明明想执行的SQL是DELETE、UPDATE,但为什么看到的是SELECT(FDW的实现原理解析)
小满未满、
347次阅读
2025-03-19 23:11:26
PostgreSQL初/中/高级认证考试(3.15)通过考生公示
开源软件联盟PostgreSQL分会
297次阅读
2025-03-20 09:50:36
IvorySQL 4.4 发布 - 基于 PostgreSQL 17.4,增强平台支持
通讯员
184次阅读
2025-03-20 15:31:04
套壳论
梧桐
179次阅读
2025-03-09 10:58:17
命名不规范,事后泪两行
xiongcc
168次阅读
2025-03-13 14:26:08
版本发布| IvorySQL 4.4 发布
IvorySQL开源数据库社区
110次阅读
2025-03-13 09:52:33
宝藏PEV,助力你成为SQL优化高手
xiongcc
102次阅读
2025-03-09 23:34:23