pg_buffercache

简介:


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


1. create extension



lhl@localhost:~$ psql mydb
psql (9.5rc1)
Type "help" for help.

mydb=# 
mydb=# 
mydb=# create database lhl;
CREATE DATABASE
mydb=# \c lhl 
You are now connected to database "lhl" as user "lhl".
lhl=# 
lhl=# 
lhl=# create extension pg_buffercache ;
CREATE EXTENSION
lhl=# \dx
                      List of installed extensions
      Name      | Version |   Schema   |           Description           
----------------+---------+------------+---------------------------------
 pg_buffercache | 1.1     | public     | examine the shared buffer cache
 plpgsql        | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)


lhl=# \d+ public.pg_buffercache
                  View "public.pg_buffercache"
      Column      |   Type   | Modifiers | Storage | Description 
------------------+----------+-----------+---------+-------------
 bufferid         | integer  |           | plain   | 
 relfilenode      | oid      |           | plain   | 
 reltablespace    | oid      |           | plain   | 
 reldatabase      | oid      |           | plain   | 
 relforknumber    | smallint |           | plain   | 
 relblocknumber   | bigint   |           | plain   | 
 isdirty          | boolean  |           | plain   | 
 usagecount       | smallint |           | plain   | 
 pinning_backends | integer  |           | plain   | 
View definition:
 SELECT p.bufferid,
    p.relfilenode,
    p.reltablespace,
    p.reldatabase,
    p.relforknumber,
    p.relblocknumber,
    p.isdirty,
    p.usagecount,
    p.pinning_backends
   FROM pg_buffercache_pages() p(bufferid integer, relfilenode oid, reltablespace oid, reldatabase oid, relforknumber smallint, relblocknumber bigint, isdirty boolean, usagecount smallint, pinning_backends integer);

2. parameters


lhl=# select setting from pg_settings where name='block_size';
 setting 
---------
 32768
(1 row)

lhl=# select setting from pg_settings where name='shared_buffers';
 setting 
---------
 16384
(1 row)

lhl=# show block_size;
 block_size 
------------
 32768
(1 row)

lhl=# show shared_buffers;
 shared_buffers 
----------------
 512MB
(1 row)

lhl=# select (select setting from pg_settings where name='shared_buffers')::integer * (select setting from pg_settings where name='block_size')::integer /1024/1024 ;
 ?column? 
----------
      512
(1 row)


lhl=# select count(1) from pg_buffercache;
 count 
-------
 16384
(1 row)


3. shared buffer cache in real time

注意网上有些资料将(select setting from pg_settings where name='block_size')直接写为 8192,

在block_size 是默认的8k情况下,没问题的,

但一旦编译PostgreSQL时更改了block_size就会得到不正确的结果,所以读取pg_settings的数值是最为靠谱,更通用的方式



lhl=# SELECT
c.relname,
pg_size_pretty(count(*) * (select setting from pg_settings where name='block_size')::integer ) as buffered,
round(100.0 * count(*) /
(SELECT setting FROM pg_settings
WHERE name='shared_buffers')::integer,1)
AS buffers_percent,
round(100.0 * count(*) * (select setting from pg_settings where name='block_size')::integer /
pg_relation_size(c.oid),1)
AS percent_of_relation
FROM pg_class c
INNER JOIN pg_buffercache b
ON b.relfilenode = c.relfilenode
INNER JOIN pg_database d
ON (b.reldatabase = d.oid AND d.datname = current_database())
GROUP BY c.oid,c.relname
ORDER BY 3 DESC
LIMIT 10; relname | buffered | buffers_percent | percent_of_relation ----------------------------------+----------+-----------------+--------------------- pg_depend_reference_index | 288 kB | 0.1 | 64.3 pg_opclass_oid_index | 64 kB | 0.0 | 100.0 pg_index_indrelid_index | 64 kB | 0.0 | 100.0 pg_transform_type_lang_index | 32 kB | 0.0 | 100.0 pg_namespace_oid_index | 64 kB | 0.0 | 100.0 pg_depend | 256 kB | 0.0 | 57.1 pg_statistic_relid_att_inh_index | 64 kB | 0.0 | 100.0 pg_cast_source_target_index | 64 kB | 0.0 | 100.0 pg_depend_depender_index | 160 kB | 0.0 | 38.5 pg_operator_oid_index | 64 kB | 0.0 | 100.0 (10 rows)

其中, buffered总和 <= shared_buffers



参考链接

1. http://www.postgresql.org/docs/9.5/static/pgbuffercache.html


目录
相关文章
|
3月前
|
SQL 关系型数据库 数据库
PG/Greenplum
PG/Greenplum 是指 PostgreSQL(简称 PG)和 Greenplum(简称 GP)两种关系型数据库管理系统。它们都是基于 SQL(结构化查询语言)的开放源代码数据库系统,具有高性能、可扩展性和高可靠性等特点
48 7
|
3月前
|
SQL 关系型数据库 数据库连接
`pg_dump` 和 `pg_restore`
`pg_dump` 和 `pg_restore`
27 4
|
6月前
|
JSON 关系型数据库 数据安全/隐私保护
PG
MYSQL VS PG
253 0
|
10月前
|
关系型数据库
pg为什么...?
pg为什么...?
21 0
|
10月前
|
存储 SQL JSON
PG备份恢复工具pg_probackup
PG备份恢复工具pg_probackup
84 0
|
关系型数据库
备份工具pg_dump的使用《postgres》
备份工具pg_dump的使用《postgres》
293 0
|
SQL 安全 关系型数据库
pg_dump
备份PostgreSQL数据库的工具,它甚至可以在数据库正在并发使用时进行完整一致的备份,而不会阻塞其它用户对数据库的访问。该工具生成的转储格式可以分为两种,脚本和归档文件。
161 0
LXJ
|
SQL 关系型数据库 开发工具
pg_pathman安装
pg_pathman安装
LXJ
484 0
|
关系型数据库 数据库 PostgreSQL
PostgreSQL pg_basebackup
本文探讨 pg_basebackup工具,本地备份、远程备份、单一表空间本地数据库的备份、表空间重定向
1833 0
PostgreSQL pg_basebackup
|
关系型数据库 API 数据库
PostgreSQL pg_start_backup
本文探讨 pg_start_backup命令的连续归档备份
1451 0
PostgreSQL pg_start_backup