合 PG使用插件pg_buffercache查询缓冲区数据
Tags: PG优化插件pg_buffercache清空缓存pg_dropcacheshared Buffer
pg_buffercache
pg_buffercache
模块提供了一种方法实时检查共享缓冲区。
该模块提供了一个 C 函数pg_buffercache_pages
,它返回一个记录的集合,外加一个包装了该函数以便于使用的视图pg_buffercache
。
默认情况下,使用仅限于超级用户和pg_monitor
角色的成员。可以使用GRANT
给其他人授予访问权限。
安装
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | postgres=# select * from pg_available_extensions where name like '%pg_buffercache%' order by name; name | default_version | installed_version | comment ----------------+-----------------+-------------------+--------------------------------- pg_buffercache | 1.3 | 1.3 | examine the shared buffer cache (1 row) postgres=# create extension pg_buffercache ; CREATE EXTENSION postgres=# \dx List of installed extensions Name | Version | Schema | Description --------------------+---------+------------+------------------------------------------------------------------------ pageinspect | 1.8 | public | inspect the contents of database pages at a low level pg_buffercache | 1.3 | public | examine the shared buffer cache pg_prewarm | 1.2 | public | prewarm relation data pg_stat_statements | 1.8 | public | track planning and execution statistics of all SQL statements executed plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language (5 rows) postgres=# \dx pg_buffercache List of installed extensions Name | Version | Schema | Description ----------------+---------+--------+--------------------------------- pg_buffercache | 1.3 | public | examine the shared buffer cache (1 row) postgres=# \dx+ pg_buffercache Objects in extension "pg_buffercache" Object description --------------------------------- function pg_buffercache_pages() view pg_buffercache (2 rows) |
pg_buffercache视图
表 F.15. pg_buffercache
列
列类型描述 |
---|
bufferid integer ID,在范围 1..shared_buffers 中 |
relfilenode oid (references pg_class .relfilenode )关系的文件结点编号 |
reltablespace oid (references pg_tablespace .oid )关系的表空间 OID |
reldatabase oid (references pg_database .oid )关系的数据库 OID |
relforknumber smallint 关系内的分叉数,见include/common/relpath.h |
relblocknumber bigint 关系内的页面数 |
isdirty boolean 页面是否为脏? |
usagecount smallint Clock-sweep 访问计数 |
pinning_backends integer 对这个缓冲区加 pin 的后端数量 |
共享缓存中的每一个缓冲区都有一行。没有使用的缓冲区的行中只有bufferid
为非空。共享的系统目录被显示为属于数据库零。
因为缓冲是所有数据库共享的,通常会有不属于当前数据库的关系的页面。这意味着对于一些行在pg_class
中可能不会有匹配的连接行,或者甚至有错误的连接。如果你试图与pg_class
连接,将连接限制于reldatabase
等于当前数据库 OID 或零的行是一个好主意。