PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables

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)

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第1张图片

2、正常关闭服务

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

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第2张图片

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第3张图片

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

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第4张图片

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第5张图片

3、再次正常启动服务

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

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第6张图片

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

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

PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables_第7张图片

你可能感兴趣的:(PgSQL技术内幕-Analyze做的那些事-pg_stat_all_tables)