更新时间:2025-07-10 GMT+08:00

缓存信息函数

9.1.1.100集群版本开始DWS支持缓存信息函数,缓存信息函数提供共享缓冲区(shared buffer)使用情况的查询接口,帮助用户了解缓存的使用情况,优化缓存策略,从而提高系统的性能和稳定性。

表1 DWS支持的缓存信息函数

查询维度

功能描述

缓存占用的统计信息

缓存占用的详细信息

实例级

查询实例的整个共享缓冲区的汇总信息,以及具体每个缓冲区的使用情况(不关联到具体表)。

表级

将各缓冲区以表为粒度进行聚合,显示每个表占用的共享缓冲区情况。

-

单表查询

快速查询指定表的缓冲区占用情况。

其中,对于查询单表缓存信息的函数,根据不同的表类型,需要传入不同的参数值,具体请参考表2。使用示例可参考pg_buffercache_rel(nspname, relname, partname, tempnspname)

表2 单表查询入参差异

表类型

nspname

relname

partname

tempnspname

普通表

指定表模式名。

指定表名。

必须为NULL。

必须为NULL。

分区表

指定表模式名。

指定表名。

指定分区名或者指定为NULL查询所有分区。

必须为NULL。

全局临时表

指定表模式名。

指定表名。

必须为NULL。

指定临时模式名或者指定为NULL查询所有临时模式。

pg_buffercache_summary()

描述:汇总当前实例共享缓冲区的使用情况。

返回信息如下:

表3 pg_buffercache_summary()返回字段

名称

类型

描述

buffers_used

integer

已使用的缓冲页数。

buffers_unused

integer

未使用的缓冲页数。

buffers_dirty

integer

脏缓冲页数。

buffers_pinned

integer

被pin住的缓冲页数。

usagecount_avg

real

缓冲页平均访问计数。

示例:

1
2
3
4
5
SELECT * FROM pg_catalog.pg_buffercache_summary();
 buffers_used | buffers_unused | buffers_dirty | buffers_pinned | usagecount_avg 
--------------+----------------+---------------+----------------+----------------
         1761 |         129311 |           725 |              0 |        4.40829
(1 row)

pgxc_buffercache_summary()

描述:汇总所有实例共享缓冲区的使用情况。

返回字段说明:除新增一列nodename标识实例名称外,其它字段与pg_buffercache_summary()相同。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
SELECT * FROM pg_catalog.pgxc_buffercache_summary();
   nodename   | buffers_used | buffers_unused | buffers_dirty | buffers_pinned | usagecount_avg 
--------------+--------------+----------------+---------------+----------------+----------------
 cn_5001      |         1237 |          31531 |            13 |              0 |        3.89248
 cn_5002      |         1047 |          31721 |             9 |              0 |        4.01146
 cn_5003      |         1473 |          31295 |            15 |              0 |        3.93143
 dn_6001_6002 |         1768 |         129304 |           371 |              0 |        4.41572
 dn_6003_6004 |         1549 |         129523 |           180 |              0 |        3.74758
 dn_6005_6006 |         1488 |         129584 |           176 |              0 |         4.0578
(6 rows)

pg_buffercache_usage_counts()

描述:在当前实例上,按访问计数汇总共享缓冲区的使用情况。

返回信息如下:

表4 pg_buffercache_usage_counts()返回字段

名称

类型

描述

usage_count

integer

缓冲页访问计数。

buffers

integer

对应访问计数的缓冲页总数。

dirty

integer

对应访问计数的所有缓冲页中的脏缓冲页数。

pinned

integer

对应访问计数的所有缓冲页中被pin住的缓冲页数。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
SELECT * FROM pg_catalog.pg_buffercache_usage_counts();
 usage_count | buffers | dirty | pinned 
-------------+---------+-------+--------
           0 |  129302 |     0 |      0
           1 |     141 |     0 |      0
           2 |      82 |     2 |      0
           3 |      59 |     2 |      0
           4 |     109 |     0 |      0
           5 |    1379 |   169 |      0
(6 rows)

pgxc_buffercache_usage_counts()

描述:在所有实例上,按访问计数汇总共享缓冲区的使用情况。

返回字段说明:除新增一列nodename标识实例名称外,其它字段与pg_buffercache_usage_counts()相同。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
SELECT * FROM pg_catalog.pgxc_buffercache_usage_counts() WHERE usage_count = 5;
   nodename   | usage_count | buffers | dirty | pinned 
--------------+-------------+---------+-------+--------
 cn_5001      |           5 |     804 |    55 |      0
 cn_5002      |           5 |     694 |    32 |      0
 cn_5003      |           5 |     987 |    61 |      0
 dn_6001_6002 |           5 |    1380 |   124 |      0
 dn_6003_6004 |           5 |     871 |    87 |      0
 dn_6005_6006 |           5 |     975 |    93 |      0
(6 rows)

pg_buffercache_rel()

描述:查询当前实例中每张表的共享缓冲区占用统计信息。

返回信息如下:

表5 pg_buffercache_rel返回字段

名称

类型

描述

nspname

name

模式名。

relname

name

表名。

partname

name

分区名。如果不是分区表,则为NULL。

tempnspname

name

全局临时表的临时模式名。如果不是全局临时表,则为NULL。

buffers

bigint

缓冲页数。

dirty

bigint

脏缓冲页数。

usage_count

bigint

缓冲页访问计数总和。

pinned

bigint

被pin住的缓冲页数。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SELECT * FROM pg_catalog.pg_buffercache_rel() ORDER BY usage_count DESC LIMIT 10;
  nspname   |               relname                | partname | tempnspname | buffers | dirty | usage_count | pinned 
------------+--------------------------------------+----------+-------------+---------+-------+-------------+--------
 cstore     | pg_hstore_delta_part_17696           |          |             |     539 |   142 |        2694 |      0
 pg_catalog | pg_attribute                         |          |             |     114 |     1 |         563 |      0
 pg_catalog | pg_class                             |          |             |      53 |     6 |         265 |      1
 pg_catalog | pg_statistic                         |          |             |      46 |    28 |         230 |      0
 cstore     | pg_hstore_delta_part_17696_tid_index |          |             |      44 |    11 |         216 |      0
 pg_catalog | pg_attribute_relid_attnum_index      |          |             |      29 |     1 |         139 |      0
 pg_toast   | pg_toast_2619                        |          |             |      23 |     3 |         109 |      0
 scheduler  | workload_collection                  |          |             |      34 |     2 |          94 |      0
 pg_catalog | pg_partition                         |          |             |      18 |     0 |          84 |      0
 pg_catalog | pg_type                              |          |             |      33 |     1 |          77 |      0
(10 rows)

pgxc_buffercache_rel()

描述:查询所有实例中每张表的共享缓冲区占用统计信息。

返回字段说明:除新增一列nodename标识实例名称外,其它字段与pg_buffercache_rel()相同。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SELECT * FROM pg_catalog.pgxc_buffercache_rel() ORDER BY usage_count DESC LIMIT 10;
   nodename   |  nspname   |          relname           | partname | tempnspname | buffers | dirty | usage_count | pinned 
--------------+------------+----------------------------+----------+-------------+---------+-------+-------------+--------
 dn_6001_6002 | cstore     | pg_hstore_delta_part_17696 |          |             |     539 |   187 |        2694 |      0
 dn_6003_6004 | cstore     | pg_hstore_delta_part_17704 |          |             |     486 |   266 |        1702 |      0
 dn_6005_6006 | cstore     | pg_hstore_delta_part_17696 |          |             |     445 |   181 |        1559 |      0
 cn_5003      | pg_catalog | pg_attribute               |          |             |     143 |     3 |         711 |      0
 cn_5001      | pg_catalog | pg_attribute               |          |             |     150 |     1 |         671 |      0
 dn_6005_6006 | pg_catalog | pg_attribute               |          |             |     125 |     1 |         574 |      0
 dn_6003_6004 | pg_catalog | pg_attribute               |          |             |     114 |     1 |         563 |      0
 dn_6001_6002 | pg_catalog | pg_attribute               |          |             |     114 |     1 |         563 |      0
 cn_5002      | pg_catalog | pg_attribute               |          |             |      99 |     1 |         483 |      0
 dn_6001_6002 | pg_catalog | pg_class                   |          |             |      53 |     6 |         265 |      1
(10 rows)

pg_buffercache_rel(nspname, relname, partname, tempnspname)

描述:查询指定表在当前实例上共享缓冲区占用的统计信息。

参数说明:根据不同的表类型,需要传入不同的参数值。具体参见表2

返回字段说明:与pg_buffercache_rel()相同。

示例1:查询普通表。

1
2
3
4
5
SELECT * FROM pg_catalog.pg_buffercache_rel('public', 't1', NULL, NULL);
 nspname | relname | partname | tempnspname | buffers | dirty | usage_count | pinned 
---------+---------+----------+-------------+---------+-------+-------------+--------
 public  | t1      |          |             |       4 |     2 |          12 |      0
(1 row)

示例2:查询分区表的指定分区。

1
2
3
4
5
SELECT * FROM pg_catalog.pg_buffercache_rel('public', 'part1', 'p1', NULL);
 nspname | relname | partname | tempnspname | buffers | dirty | usage_count | pinned 
---------+---------+----------+-------------+---------+-------+-------------+--------
 public  | part1   | p1       |             |       1 |     1 |           5 |      0
(1 row)

示例3:查询分区表的所有分区。

1
2
3
4
5
6
SELECT * FROM pg_catalog.pg_buffercache_rel('public', 'part1', NULL, NULL) ORDER BY partname;
 nspname | relname | partname | tempnspname | buffers | dirty | usage_count | pinned 
---------+---------+----------+-------------+---------+-------+-------------+--------
 public  | part1   | p1       |             |       1 |     1 |           5 |      0
 public  | part1   | p2       |             |       1 |     1 |           5 |      0
(2 rows)

示例4:查询全局临时表的指定会话。

1
2
3
4
5
SELECT * FROM pg_catalog.pg_buffercache_rel('public', 'gtt1', NULL, 'pg_temp_cn_5001_4_1_139815154281712') ORDER BY tempnspname;
 nspname | relname | partname |             tempnspname             | buffers | dirty | usage_count | pinned 
---------+---------+----------+-------------------------------------+---------+-------+-------------+--------
 public  | gtt1    |          | pg_temp_cn_5001_4_1_139815154281712 |       4 |     2 |          12 |      0
(1 row)

示例5:查询全局临时表的所有会话。

1
2
3
4
5
6
SELECT * FROM pg_catalog.pg_buffercache_rel('public', 'gtt1', NULL, NULL) ORDER BY tempnspname;
 nspname | relname | partname |             tempnspname             | buffers | dirty | usage_count | pinned 
---------+---------+----------+-------------------------------------+---------+-------+-------------+--------
 public  | gtt1    |          | pg_temp_cn_5001_4_1_139815154281712 |       4 |     2 |          12 |      0
 public  | gtt1    |          | pg_temp_cn_5001_4_2_139815154283080 |       4 |     2 |          12 |      0
(2 rows)

pgxc_buffercache_rel(nspname, relname, partname, tempnspname)

描述:查询指定表在所有实例上共享缓冲区占用的统计信息。

参数说明:根据不同的表类型,需要传入不同的参数值。具体参见表2

返回字段说明:与pgxc_buffercache_rel()相同。

示例:

1
2
3
4
5
6
7
SELECT * FROM pg_catalog.pgxc_buffercache_rel('public', 't1', NULL, NULL) ORDER BY nodename;
   nodename   | nspname | relname | partname | tempnspname | buffers | dirty | usage_count | pinned 
--------------+---------+---------+----------+-------------+---------+-------+-------------+--------
 dn_6001_6002 | public  | t1      |          |             |       4 |     2 |          12 |      0
 dn_6003_6004 | public  | t1      |          |             |       4 |     2 |          12 |      0
 dn_6005_6006 | public  | t1      |          |             |       4 |     2 |          12 |      0
(3 rows)

pg_buffercache_pages()

描述:查询当前实例上共享缓冲区的详细信息。

返回信息如下:

表6 pg_buffercache_pages返回字段

名称

类型

描述

bufferid

integer

缓冲页ID,范围为[1, shared_buffers]。

relfilenode

oid

缓冲页对应的物理文件的编号。

reltablespace

oid

缓冲页对应的物理文件所属表空间对应的OID。

reldatabase

oid

缓冲页对应的物理文件所属数据库对应的OID。如果是跨库共享系统表,该值为0。

relforknumber

smallint

缓冲页对应的物理文件的文件类型。

relblocknumber

bigint

缓冲页在对应的物理文件中的页面编号。

isdirty

boolean

缓冲页是否为脏页。

usage_count

smallint

缓冲页访问计数。

pinning_backends

integer

pin住缓冲页的后端线程数。

  • 共享缓存中的每一个缓冲区对应一条记录。未使用的缓冲区中仅bufferid字段有值,其他字段为空。
  • 该函数会列出所有缓冲页的使用情况,当shared_buffers较大时会导致结果过多。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SELECT * FROM pg_catalog.pg_buffercache_pages() LIMIT 10;
 bufferid | relfilenode | reltablespace | reldatabase | relforknumber | relblocknumber | isdirty | usage_count | pinning_backends 
----------+-------------+---------------+-------------+---------------+----------------+---------+-------------+------------------
        1 |       16637 |          1664 |           0 |             0 |              0 | f       |           5 |                0
        2 |       17183 |          1664 |           0 |             0 |              0 | f       |           5 |                0
        3 |       16750 |          1663 |       17187 |             0 |              0 | f       |           5 |                0
        4 |       16621 |          1663 |       17187 |             0 |              0 | f       |           5 |                0
        5 |       16785 |          1663 |       17187 |             0 |              0 | f       |           5 |                0
        6 |       16801 |          1663 |       17187 |             0 |              0 | f       |           5 |                0
        7 |       16797 |          1663 |       17187 |             0 |              0 | f       |           5 |                0
        8 |       16810 |          1663 |       17187 |             0 |              0 | f       |           5 |                0
        9 |       16810 |          1663 |       17187 |             0 |              1 | f       |           2 |                0
       10 |       16810 |          1663 |       17187 |             0 |              2 | f       |           2 |                0
(10 rows)

pgxc_buffercache_pages()

描述:查询所有实例上共享缓冲区的详细信息。

返回字段说明:除新增一列nodename标识实例名称外,其它字段与pg_buffercache_pages()相同。

  • 共享缓存中的每一个缓冲区对应一条记录。未使用的缓冲区中仅bufferid字段有值,其他字段为空。
  • 该函数会列出所有实例所有缓冲页的使用情况,当集群规模较大或者shared_buffers较大时会导致结果过多。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
SELECT * FROM pg_catalog.pgxc_buffercache_pages() WHERE isdirty LIMIT 10;
 nodename | bufferid | relfilenode | reltablespace | reldatabase | relforknumber | relblocknumber | isdirty | usage_count | pinning_backends 
----------+----------+-------------+---------------+-------------+---------------+----------------+---------+-------------+------------------
 cn_5001  |        7 |       17047 |          1664 |           0 |             0 |              1 | t       |           5 |                0
 cn_5001  |       15 |       16926 |          1663 |       17187 |             0 |              0 | t       |           5 |                0
 cn_5001  |       16 |       16931 |          1663 |       17187 |             0 |              1 | t       |           5 |                0
 cn_5001  |       19 |       16865 |          1663 |       17187 |             0 |             53 | t       |           5 |                0
 cn_5001  |       21 |       17022 |          1663 |       17187 |             0 |              0 | t       |           5 |                0
 cn_5001  |       22 |       17024 |          1663 |       17187 |             0 |              1 | t       |           5 |                0
 cn_5001  |       32 |       17050 |          1664 |           0 |             0 |              2 | t       |           5 |                0
 cn_5001  |       40 |       16617 |          1663 |       17187 |             1 |              2 | t       |           5 |                0
 cn_5001  |       41 |       16620 |          1663 |       17187 |             0 |              1 | t       |           5 |                0
 cn_5001  |       44 |       16750 |          1663 |       17187 |             0 |              1 | t       |           5 |                0
(10 rows)

pg_buffercache_pages(nspname, relname, partname, tempnspname)

描述:查询指定表在当前实例上占用的共享缓冲区的详细信息。

参数说明:根据不同的表类型,需要传入不同的参数值。具体参见表2

返回字段说明:和pg_buffercache_pages()相同。

示例:

1
2
3
4
5
6
7
8
SELECT * FROM pg_catalog.pg_buffercache_pages('public', 't1', NULL, NULL);
 nspname | relname | partname | tempnspname | bufferid | relforknumber | relblocknumber | isdirty | usage_count | pinning_backends 
---------+---------+----------+-------------+----------+---------------+----------------+---------+-------------+------------------
 public  | t1      |          |             |     1786 |             0 |              0 | t       |           5 |                0
 public  | t1      |          |             |     1787 |             1 |              2 | f       |           1 |                0
 public  | t1      |          |             |     1788 |             1 |              0 | f       |           1 |                0
 public  | t1      |          |             |     1789 |             0 |              1 | t       |           5 |                0
(4 rows)

pgxc_buffercache_pages(nspname, relname, partname, tempnspname)

描述:查询指定表在所有实例上占用的共享缓冲区的详细信息。

参数说明:根据不同的表类型,需要传入不同的参数值。具体参见表2

返回字段说明:和pgxc_buffercache_pages()相同。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
SELECT * FROM pg_catalog.pgxc_buffercache_pages('public', 't1', NULL, NULL) ORDER BY nodename, bufferid;
   nodename   | nspname | relname | partname | tempnspname | bufferid | relforknumber | relblocknumber | isdirty | usage_count | pinning_backends 
--------------+---------+---------+----------+-------------+----------+---------------+----------------+---------+-------------+------------------
 dn_6001_6002 | public  | t1      |          |             |     1786 |             0 |              0 | t       |           5 |                0
 dn_6001_6002 | public  | t1      |          |             |     1787 |             1 |              2 | f       |           1 |                0
 dn_6001_6002 | public  | t1      |          |             |     1788 |             1 |              0 | f       |           1 |                0
 dn_6001_6002 | public  | t1      |          |             |     1789 |             0 |              1 | t       |           5 |                0
 dn_6003_6004 | public  | t1      |          |             |     1740 |             0 |              0 | t       |           5 |                0
 dn_6003_6004 | public  | t1      |          |             |     1741 |             1 |              2 | f       |           1 |                0
 dn_6003_6004 | public  | t1      |          |             |     1742 |             1 |              0 | f       |           1 |                0
 dn_6003_6004 | public  | t1      |          |             |     1743 |             0 |              1 | t       |           5 |                0
 dn_6005_6006 | public  | t1      |          |             |     1196 |             0 |              1 | t       |           5 |                0
 dn_6005_6006 | public  | t1      |          |             |     1197 |             1 |              0 | f       |           1 |                0
 dn_6005_6006 | public  | t1      |          |             |     1198 |             1 |              2 | f       |           1 |                0
 dn_6005_6006 | public  | t1      |          |             |     1199 |             0 |              0 | t       |           5 |                0
(12 rows)