Postgresql 中文操作指南

F.27. pg_buffercache — inspect PostgreSQL buffer cache state #

pg_buffercache 模块提供了一种方式,用于检查在共享缓冲区高速缓存中正在实时发生的事情。

The pg_buffercache module provides a means for examining what’s happening in the shared buffer cache in real time.

此模块提供 pg_buffercache_pages() 函数(包装在 pg_buffercache 视图中),pg_buffercache_summary() 函数和 pg_buffercache_usage_counts() 函数。

This module provides the pg_buffercache_pages() function (wrapped in the pg_buffercache view), the pg_buffercache_summary() function, and the pg_buffercache_usage_counts() function.

pg_buffercache_pages() 函数返回一组记录,每一行描述了一个共享缓冲区条目的状态。pg_buffercache 视图包装函数,为了便于使用。

The pg_buffercache_pages() function returns a set of records, each row describing the state of one shared buffer entry. The pg_buffercache view wraps the function for convenient use.

pg_buffercache_summary() 函数返回一个行,总结共享缓冲区高速缓存的状态。

The pg_buffercache_summary() function returns a single row summarizing the state of the shared buffer cache.

pg_buffercache_usage_counts() 函数返回一组记录,每行描述具有给定使用计数的缓冲区数。

The pg_buffercache_usage_counts() function returns a set of records, each row describing the number of buffers with a given usage count.

默认情况下,仅超级用户和具有 pg_monitor 角色权限的角色可以访问。可以使用 GRANT 授予其他人访问权限。

By default, use is restricted to superusers and roles with privileges of the pg_monitor role. Access may be granted to others using GRANT.

F.27.1. The pg_buffercache View #

视图公开的列的定义显示在 Table F.15中。

The definitions of the columns exposed by the view are shown in Table F.15.

Table F.15. pg_buffercache Columns

Table F.15. pg_buffercache Columns

Column Type

Description

bufferid integer

ID, in the range 1..shared_buffers

relfilenode oid (references pg_class.relfilenode)

Filenode number of the relation

reltablespace oid (references pg_tablespace.oid)

Tablespace OID of the relation

reldatabase oid (references pg_database.oid)

Database OID of the relation

relforknumber smallint

Fork number within the relation; see common/relpath.h

relblocknumber bigint

Page number within the relation

isdirty boolean

Is the page dirty?

usagecount smallint

Clock-sweep access count

pinning_backends integer

Number of backends pinning this buffer

共享缓存中每一缓冲区都有一行。未使用的缓冲区的所有字段都显示为空,除了 bufferid。系统共享目录显示为数据库零。

There is one row for each buffer in the shared cache. Unused buffers are shown with all fields null except bufferid. Shared system catalogs are shown as belonging to database zero.

因为此缓存由所有数据库共享,所以通常会有不属于当前数据库的关系的页面。这意味着 pg_class 中某些行的匹配联接行可能不存在,或者甚至可能会有错误联接。如果你试图针对 pg_class 联接,最好是将联接限制到具有 reldatabase 的行,该行等于当前数据库的 OID 或零。

Because the cache is shared by all the databases, there will normally be pages from relations not belonging to the current database. This means that there may not be matching join rows in pg_class for some rows, or that there could even be incorrect joins. If you are trying to join against pg_class, it’s a good idea to restrict the join to rows having reldatabase equal to the current database’s OID or zero.

由于没有获得缓冲区管理器锁来复制视图将显示的缓冲器状态数据,所以访问 pg_buffercache 视图对正常缓冲区活动的影响更小,但是它不会提供跨所有缓冲区的一致的结果集。然而,我们确保每个缓冲区的信息是自洽的。

Since buffer manager locks are not taken to copy the buffer state data that the view will display, accessing pg_buffercache view has less impact on normal buffer activity but it doesn’t provide a consistent set of results across all buffers. However, we ensure that the information of each buffer is self-consistent.

F.27.2. The pg_buffercache_summary() Function #

函数公开的列的定义显示在 Table F.16中。

The definitions of the columns exposed by the function are shown in Table F.16.

Table F.16. pg_buffercache_summary() Output Columns

Table F.16. pg_buffercache_summary() Output Columns

Column Type

Description

buffers_used int4

Number of used shared buffers

buffers_unused int4

Number of unused shared buffers

buffers_dirty int4

Number of dirty shared buffers

buffers_pinned int4

Number of pinned shared buffers

usagecount_avg float8

Average usage count of used shared buffers

pg_buffercache_summary() 功能返回一行,概括所有共享缓冲区的状态。 pg_buffercache 视图提供了类似且更详细的信息,但 pg_buffercache_summary() 要便宜得多。

The pg_buffercache_summary() function returns a single row summarizing the state of all shared buffers. Similar and more detailed information is provided by the pg_buffercache view, but pg_buffercache_summary() is significantly cheaper.

pg_buffercache 视图一样,pg_buffercache_summary() 不会获取缓冲区管理器锁。因此,并发活动可能会导致结果中的小错误。

Like the pg_buffercache view, pg_buffercache_summary() does not acquire buffer manager locks. Therefore concurrent activity can lead to minor inaccuracies in the result.

F.27.3. The pg_buffercache_usage_counts() Function #

函数公开的列的定义显示在 Table F.17中。

The definitions of the columns exposed by the function are shown in Table F.17.

Table F.17. pg_buffercache_usage_counts() Output Columns

Table F.17. pg_buffercache_usage_counts() Output Columns

Column Type

Description

usage_count int4

A possible buffer usage count

buffers int4

Number of buffers with the usage count

dirty int4

Number of dirty buffers with the usage count

pinned int4

Number of pinned buffers with the usage count

pg_buffercache_usage_counts() 函数返回一组行,汇总所有共享缓冲区的状态,并汇总可能的用法计数值。pg_buffercache 视图提供了类似且更详细的信息,但 pg_buffercache_usage_counts() 的负担明显较轻。

The pg_buffercache_usage_counts() function returns a set of rows summarizing the states of all shared buffers, aggregated over the possible usage count values. Similar and more detailed information is provided by the pg_buffercache view, but pg_buffercache_usage_counts() is significantly cheaper.

pg_buffercache 视图一样,pg_buffercache_usage_counts() 不会获取缓冲区管理器锁。因此,并发活动可能会导致结果中的小错误。

Like the pg_buffercache view, pg_buffercache_usage_counts() does not acquire buffer manager locks. Therefore concurrent activity can lead to minor inaccuracies in the result.

F.27.4. Sample Output #

regression=# SELECT n.nspname, c.relname, count(*) AS buffers
             FROM pg_buffercache b JOIN pg_class c
             ON b.relfilenode = pg_relation_filenode(c.oid) AND
                b.reldatabase IN (0, (SELECT oid FROM pg_database
                                      WHERE datname = current_database()))
             JOIN pg_namespace n ON n.oid = c.relnamespace
             GROUP BY n.nspname, c.relname
             ORDER BY 3 DESC
             LIMIT 10;

  nspname   |        relname         | buffers
------------+------------------------+---------
 public     | delete_test_table      |     593
 public     | delete_test_table_pkey |     494
 pg_catalog | pg_attribute           |     472
 public     | quad_poly_tbl          |     353
 public     | tenk2                  |     349
 public     | tenk1                  |     349
 public     | gin_test_idx           |     306
 pg_catalog | pg_largeobject         |     206
 public     | gin_test_tbl           |     188
 public     | spgist_text_tbl        |     182
(10 rows)


regression=# SELECT * FROM pg_buffercache_summary();
 buffers_used | buffers_unused | buffers_dirty | buffers_pinned | usagecount_avg
--------------+----------------+---------------+----------------+----------------
          248 |        2096904 |            39 |              0 |       3.141129
(1 row)


regression=# SELECT * FROM pg_buffercache_usage_counts();
 usage_count | buffers | dirty | pinned
-------------+---------+-------+--------
           0 |   14650 |     0 |      0
           1 |    1436 |   671 |      0
           2 |     102 |    88 |      0
           3 |      23 |    21 |      0
           4 |       9 |     7 |      0
           5 |     164 |   106 |      0
(6 rows)