首頁 資料庫 mysql教程 postgresql pg_buffercache

postgresql pg_buffercache

Jun 07, 2016 pm 02:58 PM
postgresql

postgresql pg_buffercache pg_buffercache模块是用于查看shared buffer cache信息,决定shared buffer cache大还是

postgresql pg_buffercache

 

pg_buffercache模块是用于查看shared buffer cache信息,决定shared buffer cache大还是小。

Installing pg_buffercache into a database:

$ createdb pgbench

$ psql -d pgbench -f /usr/share/postgresql/contrib/pg_buffercache.sql

两步即可完成

pg_buffercache.sql内容:

/* contrib/pg_buffercache/pg_buffercache--1.0.sql */

-- complain if script is sourced in psql, rather than via CREATE EXTENSION

\echo Use "CREATE EXTENSION pg_buffercache" to load this file. \quit

-- Register the function.

CREATE FUNCTION pg_buffercache_pages()

RETURNS SETOF RECORD

AS 'MODULE_PATHNAME', 'pg_buffercache_pages'

LANGUAGE C;

-- Create a view for convenient access.

CREATE VIEW pg_buffercache AS

        SELECT P.* FROM pg_buffercache_pages() AS P

        (bufferid integer, relfilenode oid, reltablespace oid, reldatabase oid,

         relforknumber int2, relblocknumber int8, isdirty bool, usagecount int2);

-- Don't want these to be available to public.

REVOKE ALL ON FUNCTION pg_buffercache_pages() FROM PUBLIC;

REVOKE ALL ON pg_buffercache FROM PUBLIC;

创建函数和视图,回收PUBLIC 权限。

Name Type References Description

bufferid integer   ID, in the range 1..shared_buffers

relfilenode oid pg_class.relfilenode Filenode number of the relation

reltablespace oid pg_tablespace.oid Tablespace OID of the relation

reldatabase oid pg_database.oid Database OID of the relation

relblocknumber bigint   Page number within the relation

relforknumber smallint   Fork number within the relation

isdirty boolean   Is the page dirty?

usagecount smallint   Page LRU count

pg_buffercache使用:

查看shared buffers大小:

postgres=# SELECT name,setting,unit,current_setting(name) FROM pg_settings WHERE name='shared_buffers';

      name      | setting | unit | current_setting 

----------------+---------+------+-----------------

 shared_buffers | 4096    | 8kB  | 32MB

(1 row)

postgres=# select count(*) from pg_buffercache;

 count 

-------

  4096

(1 row)

可见block数量一致,大小一致。

查看当前数据库buffer的使用情况排名:

SELECT

c.relname,

count(*) AS buffers

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.relname

ORDER BY 2 DESC

LIMIT 10;

          relname          | buffers 

---------------------------+---------

 pg_statistic              |      15

 pg_operator               |      13

 pg_depend_reference_index |      13

 pg_depend                 |      13

 pg_rewrite                |       8

 pg_depend_depender_index  |       6

 pg_toast_2619             |       6

 pg_index                  |       6

 pg_extension              |       5

 pg_namespace              |       5

(10 rows)

使用pg_buffercache比较灵活,可以通过isdirty字段查询脏块,如果是未使用的buffer,那么除了bufferid,其他字段都为空值。

select count(*) from pg_buffercache where isdirty is true;

select count(*)*8/1024||'MB' from pg_buffercache where relfilenode is null and reltablespace is null and reldatabase is null and relforknumber is null and relblocknumber is null and isdirty is null and usagecount is null;

查看buffercache对象的使用大小以及百分比

SELECT

c.relname,

pg_size_pretty(count(*) * 8192) as buffered,

round(100.0 * count(*) /

(SELECT setting FROM pg_settings

WHERE name='shared_buffers')::integer,1)

AS buffers_percent,

round(100.0 * count(*) * 8192 /

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_statistic                     | 120 kB   |             0.4 |               100.0

 pg_depend                        | 104 kB   |             0.3 |                29.5

 pg_operator                      | 104 kB   |             0.3 |               100.0

 pg_depend_reference_index        | 104 kB   |             0.3 |                50.0

 pg_rewrite                       | 64 kB    |             0.2 |                66.7

 pg_operator_oid_index            | 32 kB    |             0.1 |               100.0

 pg_statistic_relid_att_inh_index | 32 kB    |             0.1 |               100.0

 pg_operator_oprname_l_r_n_index  | 40 kB    |             0.1 |               100.0

 pg_depend_depender_index         | 48 kB    |             0.1 |                22.2

 pg_amop_fam_strat_index          | 32 kB    |             0.1 |               100.0

缓冲区使用分布:

SELECT

c.relname, count(*) AS buffers,usagecount

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.relname,usagecount

ORDER BY c.relname,usagecount;

             relname              | buffers | usagecount 

-----------------------------------+---------+------------

 pg_aggregate                      |       1 |          5

 pg_aggregate_fnoid_index          |       1 |          4

 pg_aggregate_fnoid_index          |       1 |          5

 pg_am                             |       1 |          5

 pg_amop                           |       3 |          5

 pg_amop_fam_strat_index           |       1 |          1

 pg_amop_fam_strat_index           |       3 |          5

 pg_amop_opr_fam_index             |       3 |          5

 pg_amproc                         |       1 |          4

 pg_amproc                         |       1 |          5

 pg_amproc_fam_proc_index          |       2 |          5

 pg_attrdef                        |       1 |          3

 pg_attrdef_adrelid_adnum_index    |       2 |          3

 pg_attrdef_oid_index              |       1 |          1

 pg_attrdef_oid_index              |       1 |          2

 pg_cast                           |       2 |          5

 pg_cast_source_target_index       |       2 |          5

 pg_collation                      |       1 |          1

 pg_collation_oid_index            |       1 |          3

 pg_collation_oid_index            |       2 |          5

 pg_constraint                     |       1 |          1

 pg_default_acl_role_nsp_obj_index |       1 |          5

 pg_depend                         |       3 |          1

 pg_depend                         |       1 |          2

 pg_depend                         |       9 |          5

 pg_depend_depender_index          |       1 |          4

 pg_depend_depender_index          |       5 |          5

 pg_depend_reference_index         |       2 |          1

 pg_depend_reference_index         |       1 |          2

 pg_depend_reference_index         |       1 |          4

 pg_depend_reference_index         |       9 |          5

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

MySQL與PostgreSQL:效能對比與最佳化技巧 MySQL與PostgreSQL:效能對比與最佳化技巧 Jul 13, 2023 pm 03:33 PM

MySQL與PostgreSQL:效能比較與最佳化技巧在開發web應用程式時,資料庫是不可或缺的組成部分。而在選擇資料庫管理系統時,MySQL和PostgreSQL是兩個常見的選擇。他們都是開源的關係型資料庫管理系統(RDBMS),但在效能和最佳化方面有一些不同之處。本文將比較MySQL和PostgreSQL的效能,並提供一些最佳化技巧。性能對比在比較兩個資料庫管

MySQL和PostgreSQL:在Web開發中的最佳實踐 MySQL和PostgreSQL:在Web開發中的最佳實踐 Jul 14, 2023 pm 02:34 PM

MySQL和PostgreSQL:在Web開發中的最佳實務引言:在現代的Web開發領域中,資料庫是不可或缺的組成部分。在選擇資料庫時,常見的選擇是MySQL和PostgreSQL。本文將介紹在Web開發中使用MySQL和PostgreSQL的最佳實踐,並提供一些程式碼範例。一、適用場景MySQL適用於大多數Web應用程序,特別是那些需要高性能、可擴展性和易於使

學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作 學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作 Jul 31, 2023 pm 12:54 PM

學習Go語言中的資料庫函數並實作PostgreSQL資料的增刪改查操作在現代的軟體開發中,資料庫是不可或缺的一部分。 Go語言作為一門強大的程式語言,提供了豐富的資料庫操作函數和工具包,可以輕鬆實現資料庫的增刪改查操作。本文將介紹如何學習Go語言中的資料庫函數,並使用PostgreSQL資料庫進行實際的操作。第一步:安裝資料庫驅動程式在Go語言中,每個資料庫

MySQL與PostgreSQL:資料安全與備份策略 MySQL與PostgreSQL:資料安全與備份策略 Jul 13, 2023 pm 03:31 PM

MySQL與PostgreSQL:資料安全與備份策略引言:在現代社會中,資料成為了企業和個人生活中不可或缺的一部分。對於資料庫管理系統來說,資料安全與備份策略是至關重要的,既能保護資料免受遺失或損壞,也能確保恢復資料的可靠性和完整性。本文將重點放在MySQL和PostgreSQL兩種主流關係型資料庫系統的資料安全性和備份策略。一、資料安全性方面:(一)用戶權

在Go語言中使用PostgreSQL:完整指南 在Go語言中使用PostgreSQL:完整指南 Jun 18, 2023 am 09:28 AM

Go語言是一種快速、有效率的程式語言,適合建立Web服務和後端應用程式。而PostgreSQL是一個開源的關聯式資料庫管理系統,承諾提供更高的可靠性、可擴展性和資料安全性。在本文中,我們將深入探討如何在Go語言中使用PostgreSQL,並提供一些實用的程式碼範例和技巧。安裝和設定PostgreSQL首先,我們需要安裝和設定PostgreSQL。可以在官方網

如何在PHP程式設計中使用PostgreSQL資料庫? 如何在PHP程式設計中使用PostgreSQL資料庫? Jun 12, 2023 am 09:27 AM

隨著資料庫技術的發展,資料庫管理系統也呈現出多種多樣的選擇,開發人員可以根據自己的需求和喜好選擇最適合自己的資料庫。而PostgreSQL作為一種先進的開源關係型資料庫系統,越來越受到開發人員的關注與使用。那麼,在PHP程式設計中如何使用PostgreSQL資料庫呢?一、安裝和設定PostgreSQL資料庫在使用PostgreSQL之前,需要先安裝並設定它。首先

MySQL和PostgreSQL:如何優化資料庫查詢效能? MySQL和PostgreSQL:如何優化資料庫查詢效能? Jul 12, 2023 pm 03:15 PM

MySQL和PostgreSQL:如何優化資料庫查詢效能?概述:在開發應用程式時,資料庫查詢效能是一個重要的考慮因素。良好的查詢效能可以提高應用程式的回應速度和使用者體驗。本文將介紹一些最佳化資料庫查詢效能的方法,重點涵蓋MySQL和PostgreSQL兩種常用資料庫。資料庫索引的最佳化:資料庫索引是提高查詢效能的重要因素。索引可以加快資料的查找速度,減少查詢時掃

資料庫容量規劃與擴展:MySQL vs. PostgreSQL 資料庫容量規劃與擴展:MySQL vs. PostgreSQL Jul 12, 2023 pm 01:43 PM

資料庫容量規劃與擴展:MySQLvs.PostgreSQL引言:隨著網際網路的快速發展和大數據時代的到來,資料庫的容量規劃和擴展變得越來越重要。 MySQL和PostgreSQL是兩個流行的關聯式資料庫管理系統(RDBMS),它們在資料庫容量規劃和擴充方面有著不同的特性和適用場景。本文將對這兩個資料庫進行比較,並給出一些程式碼範例來展示它們的差異。一、MySQ

See all articles