本文介绍: PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tablespg_stat_all_tables视图中记录有analyze信息,比如何时做的analyze、表元组个数(活元组、死元组)等。重启后发现该视图中表的统计信息重置不见了,发生了什么?1、pg_stat_all_tables先理解下该视图,该视图定义如下。从视图定义可以看到相关统计信息都是通过一系列函数获取的:yzs=…
PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables
pg_stat_all_tables视图中记录有analyze信息,比如何时做的analyze、表元组个数(活元组、死元组)等。重启后发现该视图中表的统计信息重置不见了,发生了什么?
1、pg_stat_all_tables
先理解下该视图,该视图定义如下。从视图定义可以看到相关统计信息都是通过一系列函数获取的:
yzs=# d+ pg_stat_all_tables
视图 "pg_catalog.pg_stat_all_tables"
栏位 | 类型 | 校对规则 | 可空的 | 预设 | 存储 | 描述
---------------------+--------------------------+----------+--------+------+-------+------
relid | oid | | | | plain |
schemaname | name | | | | plain |
relname | name | | | | plain |
seq_scan | bigint | | | | plain |
seq_tup_read | bigint | | | | plain |
idx_scan | bigint | | | | plain |
idx_tup_fetch | bigint | | | | plain |
n_tup_ins | bigint | | | | plain |
n_tup_upd | bigint | | | | plain |
n_tup_del | bigint | | | | plain |
n_tup_hot_upd | bigint | | | | plain |
n_live_tup | bigint | | | | plain |
n_dead_tup | bigint | | | | plain |
n_mod_since_analyze | bigint | | | | plain |
last_vacuum | timestamp with time zone | | | | plain |
last_autovacuum | timestamp with time zone | | | | plain |
last_analyze | timestamp with time zone | | | | plain |
last_autoanalyze | timestamp with time zone | | | | plain |
vacuum_count | bigint | | | | plain |
autovacuum_count | bigint | | | | plain |
analyze_count | bigint | | | | plain |
autoanalyze_count | bigint | | | | plain |
视图定义:
SELECT c.oid AS relid,
n.nspname AS schemaname,
c.relname,
pg_stat_get_numscans(c.oid) AS seq_scan,
pg_stat_get_tuples_returned(c.oid) AS seq_tup_read,
sum(pg_stat_get_numscans(i.indexrelid))::bigint AS idx_scan,
sum(pg_stat_get_tuples_fetched(i.indexrelid))::bigint + pg_stat_get_tuples_fetched(c.oid) AS idx_tup_fetch,
pg_stat_get_tuples_inserted(c.oid) AS n_tup_ins,
pg_stat_get_tuples_updated(c.oid) AS n_tup_upd,
pg_stat_get_tuples_deleted(c.oid) AS n_tup_del,
pg_stat_get_tuples_hot_updated(c.oid) AS n_tup_hot_upd,
pg_stat_get_live_tuples(c.oid) AS n_live_tup,
pg_stat_get_dead_tuples(c.oid) AS n_dead_tup,
pg_stat_get_mod_since_analyze(c.oid) AS n_mod_since_analyze,
pg_stat_get_last_vacuum_time(c.oid) AS last_vacuum,
pg_stat_get_last_autovacuum_time(c.oid) AS last_autovacuum,
pg_stat_get_last_analyze_time(c.oid) AS last_analyze,
pg_stat_get_last_autoanalyze_time(c.oid) AS last_autoanalyze,
pg_stat_get_vacuum_count(c.oid) AS vacuum_count,
pg_stat_get_autovacuum_count(c.oid) AS autovacuum_count,
pg_stat_get_analyze_count(c.oid) AS analyze_count,
pg_stat_get_autoanalyze_count(c.oid) AS autoanalyze_count
FROM pg_class c
LEFT JOIN pg_index i ON c.oid = i.indrelid
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = ANY (ARRAY['r'::"char", 't'::"char", 'm'::"char"])
GROUP BY c.oid, n.nspname, c.relname;
我们以n_live_tup这一列为例,看下这个统计信息来自哪里。需要梳理下pg_stat_get_live_tuples函数:可以看到统计信息来自pg_stat_tmp/global.stat(由配置项pgstat_temp_directory控制,默认pg_stat_tmp)
2、正常关闭服务
3、再次正常启动服务
发现统计信息文件又移动到了,pg_stat_tmp。此时查看pg_stat_all_tables视图,发现统计信息是存在的:
经分析,在崩溃重启恢复时,会将pg_stat_tmp和pg_stat目录都进行重置,此后重启成功后,因没有统计信息文件,就导致pg_stat_all_tables视图中的信息为空了:
原文地址:https://blog.csdn.net/yanzongshuai/article/details/134623025
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_22000.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。