pg_buffercache

pg_buffercache模块提供了一种实时检查共享缓冲区高速缓存中发生的内容。

该模块提供了一个C函数pg_buffercache_pages,该函数返回一组记录,以及一个视图pg_buffercache,该视图包装了该函数以便于使用。

默认情况下,仅限超级用户和pg_read_all_stats角色成员使用。可以使用GRANT将访问权限授予其他用户。

开启pg_buffercache模块:

postgres=# create extension pg_buffercache;
CREATE EXTENSION
postgres=# 

  

共享缓存中的每个buffer都有一行。未使用的缓冲区,除bufferid之外的所有字段均为null。共享的system catalog显示为属于数据库0。

因为共享缓存是所有数据库都共享的,所以通常会有不属于当前数据库的关系中的页面。这意味着在pg_class中某些行可能没有匹配的连接行,或者甚至可能有不正确的连接。如果要针对pg_class进行联接,则最好将联接限制为reldatabase等于当前数据库的OID或零的行。

当访问pg_buffercache视图时,将使用内部buffer管理器锁定足够长的时间以复制该视图将显示的所有缓冲区状态数据。这样可确保视图产生一致的结果集,而不会超过正常的缓冲区活动所需的时间从而阻塞活动。但是,如果经常阅读该视图,可能会对数据库性能产生一些影响。

查询示例:

postgres=# SELECT c.relname, count(*) AS buffers
postgres-#              FROM pg_buffercache b INNER JOIN pg_class c
postgres-#              ON b.relfilenode = pg_relation_filenode(c.oid) AND
postgres-#                 b.reldatabase IN (0, (SELECT oid FROM pg_database
postgres(#                                       WHERE datname = current_database()))
postgres-#              GROUP BY c.relname
postgres-#              ORDER BY 2 DESC
postgres-#              LIMIT 10;
            relname             | buffers 
--------------------------------+---------
 pg_proc                        |      80
 pg_depend                      |      59
 pg_attribute                   |      52
 pg_proc_proname_args_nsp_index |      24
 pg_depend_reference_index      |      18
 pg_class                       |      16
 pg_operator                    |      15
 pg_statistic                   |      15
 pg_toast_2618                  |      13
 pg_shdepend                    |      12
(10 rows)

postgres=# 

  

原文地址:https://www.cnblogs.com/abclife/p/12090618.html