本文介绍: 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

b7af19428a960f8c4bf2a9873e0e4dcd.png

2、正常关闭服务

关闭服务前,统计信息文件位于pg_stat_tmp:

246f0a99db1d41f025180e3ac68e8b45.png

bd152ea5bd298804f4c8e4899ebb7e47.png

关闭服务后:统计信息文件移动到了pg_stat

ad56af9eb0241a5cf9f518c9f8bf328a.png

fd7d8020467c0fcd7bbc38b070389a64.png

3、再次正常启动服务

发现统计信息文件移动到了,pg_stat_tmp。此时查看pg_stat_all_tables视图,发现统计信息是存在的:

80134335aa18f5cfb9dc3e1c528101af.png

为什么重启后统计信息并没有重置呢?

分析崩溃重启恢复时,会将pg_stat_tmp和pg_stat目录都进行重置,此后重启成功后,因没有统计信息文件,就导致pg_stat_all_tables视图中的信息为空

97a66742b315410ecf381577331e0725.png

原文地址:https://blog.csdn.net/yanzongshuai/article/details/134623025

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_22000.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注