PostgreSQL pg_stat_statements pg_stat_statements_reset 函数

1 背景知识

本文主要介绍 pg_stat_statements_reset 函数

pg_stat_statements_reset 函数 用于删除指定的 pg_stat_statements 收集的统计信息。

2 语法结构

pg_stat_statements_reset(userid Oid, dbid Oid, queryid bigint) returns void

3 语法详解

4 函数使用

4.1 统计信息查询

查询 pg_stat_statements 视图,发现当前数据库中有很多语句的统计信息。

SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
     nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+--------------------------------------------------------------------
query           | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2
calls           | 3000
total_exec_time | 6016.957095999991
rows            | 3000
hit_percent     | 99.9953684405539345
-[ RECORD 2 ]---+--------------------------------------------------------------------
query           | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2
calls           | 3000
total_exec_time | 139.5114090000003
rows            | 3000
hit_percent     | 98.7694097641741621
-[ RECORD 3 ]---+--------------------------------------------------------------------
query           | copy pgbench_accounts from stdin with (freeze on)
calls           | 1
total_exec_time | 95.47931
rows            | 100000
hit_percent     | 100.0000000000000000
-[ RECORD 4 ]---+--------------------------------------------------------------------
query           | alter table pgbench_accounts add primary key (aid)
calls           | 1
total_exec_time | 42.100152
rows            | 0
hit_percent     | 100.0000000000000000
-[ RECORD 5 ]---+--------------------------------------------------------------------
query           | SELECT abalance FROM pgbench_accounts WHERE aid = $1
calls           | 3000
total_exec_time | 34.60989200000003
rows            | 3000
hit_percent     | 100.0000000000000000

4.2 重置统计信息

调用 pg_stat_statements_reset 函数传入参数 0

--postgres-testdb>
SELECT pg_stat_statements_reset(0,0,0);

4.3 统计信息查询

SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
     nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+---------------------------------------
query           | SELECT pg_stat_statements_reset(0,0,0)
calls           | 1
total_exec_time | 0.497293
rows            | 1
hit_percent     | 
Note

函数操作生效,统计信息已经被清空。