暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

Awesome Extensions: pg_buffercache

原创 严少安 2024-07-27
148

pg_buffercache 简介

IvorySQL 是一款以 PostgreSQL 为基础进行开发,并且兼容 Oracle 的开源数据库。它提供了多种扩展来增强其功能,pg_buffercache就是这样一个扩展,它提供了对共享缓冲区状态的深入洞察。

ivorysql=# select version();
+----------------------------------------------------------------------------------------------------------------------------------+
|                                                             version                                                              |
+----------------------------------------------------------------------------------------------------------------------------------+
| PostgreSQL 16.3-ShawnYan (IvorySQL 3.3) on x86_64-pc-linux-gnu, compiled by gcc (GCC) 11.4.1 20231218 (Red Hat 11.4.1-3), 64-bit |
+----------------------------------------------------------------------------------------------------------------------------------+
(1 row)

本文将探讨 pg_buffercache 扩展的功能,并通过实际示例演示其使用方法。

pg_buffercache模块是一个扩展,对于性能调优和理解数据库系统在不同负载下的行为非常有用。

pg_buffercache 安装

要使用pg_buffercache扩展,必须首先在PostgreSQL数据库中创建该扩展,使用以下命令:

ivorysql=# CREATE EXTENSION IF NOT EXISTS pg_buffercache;
CREATE EXTENSION
ivorysql=# \dx
                                List of installed extensions
+----------------+---------+------------+--------------------------------------------------+
|      Name      | Version |   Schema   |                   Description                    |
+----------------+---------+------------+--------------------------------------------------+
| ivorysql_ora   | 1.0     | sys        | Oracle Compatible extenison on Postgres Database |
| pg_buffercache | 1.4     | public     | examine the shared buffer cache                  |
| pgtam          | 0.0.1   | public     |                                                  |
| plisql         | 1.0     | pg_catalog | PL/iSQL procedural language                      |
| plpgsql        | 1.0     | pg_catalog | PL/pgSQL procedural language                     |
+----------------+---------+------------+--------------------------------------------------+
(5 rows)

ivorysql=# \dx+ pg_buffercache
  Objects in extension "pg_buffercache"
+----------------------------------------+
|           Object description           |
+----------------------------------------+
| function pg_buffercache_pages()        |
| function pg_buffercache_summary()      |
| function pg_buffercache_usage_counts() |
| view pg_buffercache                    |
+----------------------------------------+
(4 rows)
  • pg_buffercache_pages() 函数:此函数返回每个共享缓冲条目的详细记录集,描述其状态。
  • pg_buffercache视图:一个方便的视图,包装了pg_buffercache_pages()函数,提供了一个易于使用的接口来查询缓冲区缓存信息。
  • pg_buffercache_summary() 函数:返回一行汇总了共享缓冲区的总体状态。
  • pg_buffercache_usage_counts() 函数:提供了按使用计数值聚合的所有共享缓冲区状态的摘要。

扩展安装后,你可以查询pg_buffercache视图以获取有关共享缓冲区的详细信息。

例如,要检查特定表的缓存状态,你可能会使用如下查询:

SELECT count(1) FROM pg_buffercache WHERE relfilenode = 't1'::regclass;

pg_buffercache视图提供了几个信息列,包括:

  • bufferid:共享缓冲区中缓冲区的唯一标识符。
  • relfilenode:与缓冲区关联的关系的文件节点号。
  • isdirty:一个布尔值,指示缓冲区页面是否为脏页(即已被修改并需要写入磁盘)。
  • usagecount:时钟扫描访问计数,这是缓冲区被访问频率的指标。

pg_buffercache 示例

以下是如何使用pg_buffercache扩展的几个实际示例:

识别脏缓冲区:

SELECT c.relname
FROM pg_buffercache b, pg_class c
WHERE b.relfilenode=c.relfilenode AND b.isdirty='t';

+---------------------------+
|          relname          |
+---------------------------+
| t11                       |
| pg_depend_depender_index  |
| pg_depend_depender_index  |
| pg_depend_reference_index |
| pg_depend_reference_index |
| pg_depend                 |
+---------------------------+
(6 rows)

检查缓冲区缓存使用情况

WITH buffer_by_relfilenode AS (
    SELECT reldatabase, relfilenode,
        NULLIF(COUNT(CASE WHEN relfilenode IS NOT NULL THEN 1 END), 0) as used_buffer,
        COUNT(CASE WHEN relfilenode IS NULL THEN 1 END) as unused_buffer,
        SUM(usagecount) as sum_usagecount,
        NULLIF(SUM(isdirty::int), 0) as dirty_buffer,
        NULLIF(SUM(pinning_backends), 0) as sum_pinning_backends
    FROM pg_buffercache
    GROUP BY reldatabase, relfilenode
)
SELECT COALESCE(d.datname, 'shared') db, n.nspname schema, c.relname relation,
       used_buffer, unused_buffer, sum_usagecount, dirty_buffer, sum_pinning_backends
  FROM buffer_by_relfilenode b
  LEFT JOIN pg_database d ON b.reldatabase = d.oid
  LEFT JOIN pg_class c ON b.relfilenode = pg_relation_filenode(c.oid)
  LEFT JOIN pg_namespace n ON n.oid = c.relnamespace;

+-----------+------------+-----------------------------------------------+-------------+---------------+----------------+--------------+----------------------+
|    db     |   schema   |                   relation                    | used_buffer | unused_buffer | sum_usagecount | dirty_buffer | sum_pinning_backends |
+-----------+------------+-----------------------------------------------+-------------+---------------+----------------+--------------+----------------------+
| ivorysql  | pg_catalog | pg_statistic                                  |           6 |             0 |             25 |       [null] |               [null] |
| ivorysql  | pg_catalog | pg_type                                       |          10 |             0 |             30 |       [null] |               [null] |
| ivorysql  | pg_toast   | pg_toast_2619                                 |           1 |             0 |              3 |       [null] |               [null] |
...

统计共享内存中表的使用情况

SELECT c.relname, COUNT(*) AS buffers
FROM pg_buffercache b
INNER 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()))
GROUP BY c.relname
ORDER BY 2 DESC
LIMIT 5;

+--------------+---------+
|   relname    | buffers |
+--------------+---------+
| pg_attribute |      35 |
| pg_proc      |      34 |
| pg_operator  |      27 |
| pg_class     |      18 |
| pg_type      |      11 |
+--------------+---------+
(5 rows)

pg_buffercache扩展提供了有关共享缓冲区状态的详细、实时信息,可使管理员能够就性能调优和资源分配做出明智的决策。


🌻 往期精彩 ▼


– / END / –

👉 这里可以找到我

如果这篇文章为你带来了灵感或启发,就请帮忙点『』or『在看』or『转发』吧,感谢!ღ( ´・ᴗ・` )~

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论