PostgreSQL pg_stat_reset清除track_counts的隐患

简介:

标签

PostgreSQL , track_counts , 统计信息 , pg_stat_reset


背景

PostgreSQL数据库的statstic模块有一些计数器,用于统计每个表被插入、更新、删除的记录数。

通过这些视图,可以查看计数器统计到的一些计数:

postgres=# \dv pg_stat*    
                     List of relations    
   Schema   |            Name             | Type |  Owner       
------------+-----------------------------+------+----------    
 pg_catalog | pg_stat_activity            | view | postgres    
 pg_catalog | pg_stat_all_indexes         | view | postgres    
 pg_catalog | pg_stat_all_tables          | view | postgres    
 pg_catalog | pg_stat_archiver            | view | postgres    
 pg_catalog | pg_stat_bgwriter            | view | postgres    
 pg_catalog | pg_stat_database            | view | postgres    
 pg_catalog | pg_stat_database_conflicts  | view | postgres    
 pg_catalog | pg_stat_progress_vacuum     | view | postgres    
 pg_catalog | pg_stat_replication         | view | postgres    
 pg_catalog | pg_stat_ssl                 | view | postgres    
 pg_catalog | pg_stat_subscription        | view | postgres    
 pg_catalog | pg_stat_sys_indexes         | view | postgres    
 pg_catalog | pg_stat_sys_tables          | view | postgres    
 pg_catalog | pg_stat_user_functions      | view | postgres    
 pg_catalog | pg_stat_user_indexes        | view | postgres    
 pg_catalog | pg_stat_user_tables         | view | postgres    
 pg_catalog | pg_stat_wal_receiver        | view | postgres    
 pg_catalog | pg_stat_xact_all_tables     | view | postgres    
 pg_catalog | pg_stat_xact_sys_tables     | view | postgres    
 pg_catalog | pg_stat_xact_user_functions | view | postgres    
 pg_catalog | pg_stat_xact_user_tables    | view | postgres    
 pg_catalog | pg_statio_all_indexes       | view | postgres    
 pg_catalog | pg_statio_all_sequences     | view | postgres    
 pg_catalog | pg_statio_all_tables        | view | postgres    
 pg_catalog | pg_statio_sys_indexes       | view | postgres    
 pg_catalog | pg_statio_sys_sequences     | view | postgres    
 pg_catalog | pg_statio_sys_tables        | view | postgres    
 pg_catalog | pg_statio_user_indexes      | view | postgres    
 pg_catalog | pg_statio_user_sequences    | view | postgres    
 pg_catalog | pg_statio_user_tables       | view | postgres    
 pg_catalog | pg_stats                    | view | postgres    

例如表相关的计数:

postgres=# \d pg_stat_all_tables     
                      View "pg_catalog.pg_stat_all_tables"    
       Column        |           Type           | Collation | Nullable | Default     
---------------------+--------------------------+-----------+----------+---------    
 relid               | oid                      |           |          |     
 schemaname          | name                     |           |          |     
 relname             | name                     |           |          |     
 seq_scan            | bigint                   |           |          |     
 seq_tup_read        | bigint                   |           |          |     
 idx_scan            | bigint                   |           |          |     
 idx_tup_fetch       | bigint                   |           |          |     
 n_tup_ins           | bigint                   |           |          |     
 n_tup_upd           | bigint                   |           |          |     
 n_tup_del           | bigint                   |           |          |     
 n_tup_hot_upd       | bigint                   |           |          |     
 n_live_tup          | bigint                   |           |          |     
 n_dead_tup          | bigint                   |           |          |     
 n_mod_since_analyze | bigint                   |           |          |     
 last_vacuum         | timestamp with time zone |           |          |     
 last_autovacuum     | timestamp with time zone |           |          |     
 last_analyze        | timestamp with time zone |           |          |     
 last_autoanalyze    | timestamp with time zone |           |          |     
 vacuum_count        | bigint                   |           |          |     
 autovacuum_count    | bigint                   |           |          |     
 analyze_count       | bigint                   |           |          |     
 autoanalyze_count   | bigint                   |           |          |     

查看某张表的计数,例如

postgres=# select * from pg_stat_all_tables where relname='test1';    
-[ RECORD 1 ]-------+-------    
relid               | 31129    
schemaname          | public    
relname             | test1    
seq_scan            | 0    
seq_tup_read        | 0    
idx_scan            |     
idx_tup_fetch       |     
n_tup_ins           | 0    
n_tup_upd           | 0    
n_tup_del           | 0    
n_tup_hot_upd       | 0    
n_live_tup          | 0    
n_dead_tup          | 0    
n_mod_since_analyze | 0    
last_vacuum         |     
last_autovacuum     |     
last_analyze        |     
last_autoanalyze    |     
vacuum_count        | 0    
autovacuum_count    | 0    
analyze_count       | 0    
autoanalyze_count   | 0    

通过reset函数,可以对这些计数清零。

Function Return Type Description
pg_stat_reset() void Reset all statistics counters for the current database to zero (requires superuser privileges by default, but EXECUTE for this function can be granted to others.)
pg_stat_reset_shared(text) void Reset some cluster-wide statistics counters to zero, depending on the argument (requires superuser privileges by default, but EXECUTE for this function can be granted to others). Calling pg_stat_reset_shared('bgwriter') will zero all the counters shown in the pg_stat_bgwriter view. Calling pg_stat_reset_shared('archiver') will zero all the counters shown in the pg_stat_archiver view.
pg_stat_reset_single_table_counters(oid) void Reset statistics for a single table or index in the current database to zero (requires superuser privileges by default, but EXECUTE for this function can be granted to others)
pg_stat_reset_single_function_counters(oid) void Reset statistics for a single function in the current database to zero (requires superuser privileges by default, but EXECUTE for this function can be granted to others)

清零有什么后果呢?

autovacuum launcher进程依赖计数器

autovacuum launcher进程,在一个autovacuum_naptime周期内,轮询所有的database内的计数,并根据计数以及设置的阈值(表级、或全库级阈值)判断是否需要对表实施vacuum或analyze的动作。

vacuum threshold = vacuum base threshold + vacuum scale factor * number of tuples

analyze threshold = analyze base threshold + analyze scale factor * number of tuples

如果计数器被清零,可能无法及时对表进行垃圾回收或analyze。

例子

1、配置参数,便于观察。

vi postgresql.conf

autovacuum = on    
log_autovacuum_min_duration = 0    
autovacuum_max_workers = 5    
autovacuum_naptime = 5s    

2、生效参数:pg_ctl reload

3、建立一个测试表

create table test1(id int);    

4、观察日志

 tail -f -n 1 postgresql-Wed.csv    

5、写入批量数据

postgres=# insert into test1 select generate_series(1,100000);     

超过自动analyze的阈值,观察到自动触发了analyze。

2017-11-01 13:39:02.853 CST,,,25591,,59f95df6.63f7,1,,2017-11-01 13:39:02 CST,4/1074,1912083,日志,00000,"自动分析表 ""postgres.public.test1""的系统使用情况: CPU: user: 0.01 s, system: 0.00 s, elapsed: 0.02 s",,,,,,,,"do_analyze_rel, analyze.c:688",""    

6、更新批量数据

postgres=# update test1 set id=1;    

超过自动vacuum和analyze的阈值,观察到自动触发了vacuum和analyze。

2017-11-01 13:39:32.972 CST,,,25599,,59f95e14.63ff,1,,2017-11-01 13:39:32 CST,4/1088,0,日志,00000,"自动清理表""postgres.public.test1"":索引扫描:0    
页面:0 被移除,885 保留,0 由于被占用而跳过,0 被跳过的已被冻结    
tuples: 100003 removed, 100003 remain, 0 are dead but not yet removable, oldest xmin: 1912085    
缓冲区使用:1795次命中,2次失效,4次脏    
平均读取率:0.835 MB/s,平均写入率:1.670 MB/s    
系统用法:CPU: user: 0.01 s, system: 0.00 s, elapsed: 0.01 s",,,,,,,,"lazy_vacuum_rel, vacuumlazy.c:402",""    
    
2017-11-01 13:39:32.989 CST,,,25599,,59f95e14.63ff,2,,2017-11-01 13:39:32 CST,4/1089,1912085,日志,00000,"自动分析表 ""postgres.public.test1""的系统使用情况: CPU: user: 0.01 s, system: 0.00 s, elapsed: 0.01 s",,,,,,,,"do_analyze_rel, analyze.c:688",""    

7、更新批量数据,并同时清零计数器。

postgres=# update test1 set id=1;select pg_stat_reset();    

计数器被清零

postgres=# select * from pg_stat_all_tables where relname='test1';    
-[ RECORD 1 ]-------+-------    
relid               | 31129    
schemaname          | public    
relname             | test1    
seq_scan            | 0    
seq_tup_read        | 0    
idx_scan            |     
idx_tup_fetch       |     
n_tup_ins           | 0    
n_tup_upd           | 0    
n_tup_del           | 0    
n_tup_hot_upd       | 0    
n_live_tup          | 0    
n_dead_tup          | 0    
n_mod_since_analyze | 0    
last_vacuum         |     
last_autovacuum     |     
last_analyze        |     
last_autoanalyze    |     
vacuum_count        | 0    
autovacuum_count    | 0    
analyze_count       | 0    
autoanalyze_count   | 0    

计数器清零后,autovacuum不会触发vacuum和analyze。

小结

计数器清零会影响autovacuum launcher发起vacuum和analyze,导致一些表实际上已经超过分析或垃圾回收的阈值,但是不会被触发。严重时,可能导致表膨胀,或统计信息不准确。

《PostgreSQL AWR报告》

pg_awr里面涉及到计数器的默认清理,我会在后期改掉,默认不清理。

参考

《PostgreSQL pg_stat_ pg_statio_ 统计信息(scan,read,fetch,hit)源码解读》

https://www.postgresql.org/docs/10/static/routine-vacuuming.html#autovacuum

https://www.postgresql.org/docs/10/static/runtime-config-statistics.html#guc-track-counts

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
存储 编译器
什么是数据块?西门子S7-200SMART数据块如何使用?
今天我们来学习在西门子S7-200 SMART中如何使用数据块。在讲解数据块的使用之前我们先来看一下什么是数据块:数据块用来对V存储区也叫变量存储区赋初始值;可以对字节、字或双字来分配数据值。
什么是数据块?西门子S7-200SMART数据块如何使用?
|
存储 SQL 大数据
列式存储系列(一)C-Store
列式存储系列(一)概述 序 本文是列式存储系列的第一篇。在这个系列中,我们将介绍几个典型的列式存储系统。这些列式系统的出现都有各自的时代背景。在介绍这些系统的同时,我们也尽量介绍一下它们的背景,以便大家有一个更宏观的认识,理解这个系统为什么会出现,它要解决的问题,以及它如何影响后来类似系统的发展。
3388 0
|
9月前
|
机器学习/深度学习 缓存 人工智能
一文了解DeepSeek及应用场景
本文详细介绍了DeepSeek及其应用场景,涵盖了大模型的发展历程、基本原理和分类(通用与推理模型)。文章分析了DeepSeek的具体特性、性能优势、低成本训练与调用特点,以及其技术路线(如MoE、MLA架构),并与竞品进行了对比。此外,还探讨了DeepSeek在金融风控等领域的应用前景。
一文了解DeepSeek及应用场景
|
运维 监控 安全
构建高效运维体系的策略与实践
【10月更文挑战第7天】 本文旨在探讨如何构建高效的运维体系。从明确定义目标、优化流程、引入自动化工具、建立监控机制到提升团队能力,我们将全面解析高效运维体系的构建步骤和关键要素。通过具体策略和成功案例的分享,帮助运维团队提升工作效率、减少故障发生,并持续改进运维质量。
423 0
|
网络协议 Java
【Java】已解决java.net.UnknownHostException异常
【Java】已解决java.net.UnknownHostException异常
4183 0
|
Web App开发 网络安全 数据安全/隐私保护
Lua中实现HTTP请求的User-Agent自定义
Lua中实现HTTP请求的User-Agent自定义
|
SQL 数据处理 数据库
西门子S7-200 SMART如何使用状态图表,如何创建、监视、强制、趋势显示
上篇文章中我们学习了S7-200 SMART系统块的组态,本篇我们来介绍在编程软件STEP7-Micro/WIN SMART中如何使用状态图表,以及如何创建、监视、强制、趋势显示。在STEP7-Micro/WIN SMART与PLC之间成功建立通信,并且将程序下载到PLC后,就可以监控和调试程序了。程序状态监控可以监视程序的运行情况,但是如果需要监控的变量较多,不能在程序编辑器中同时显示的时候就需要使用状态图表监控。接下来我们来介绍在STEP7-Micro/WIN SMART如何使用状态图表监控和调试程序。
西门子S7-200 SMART如何使用状态图表,如何创建、监视、强制、趋势显示
|
存储 供应链 安全
解释区块链技术的应用场景、优势及经典案例
解释区块链技术的应用场景、优势及经典案例