β

pgfincore

连顺科的个人页面 58 阅读

一、系统

# cat /etc/issue

CentOS release 6.5 (Final)

Kernel \r on an \m

# uname -a

Linux barman 2.6.32-431.11.2.el6.x86_64 #1 SMP Tue Mar 25 19:59:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

# cat /etc/hosts

二、安装

2.1 简介

With PostgreSQL, each Table or Index is splitted in segments of (usually) 1GB, and each segment is splitted in pages in memory then in blocks for the filesystem.

Those functions let you know which and how many disk block from a relation are in the page cache of the operating system. It can provide the result as a VarBit and can be stored in a table. Then using this table, it is possible to restore the page cache state for each block of the relation, even in another server, thanks to Streaming Replication.

Other functions are used to set a POSIX_FADVISE flag on the entire relation (each segment). The more usefull are probably WILLNEED and DONTNEED which push and pop blocks of each segments of a relation from page cache, respectively.

Each functions are call with at least a table name or an index name (or oid) as a parameter and walk each segment of the relation.

注意:pgfincore将对象缓存在操作系统内存中,并不占用数据库cache。

要求:Pg version >=8.3

2.2 安装

# su - postgres

$ wget http://pgfoundry.org/frs/download.php/3186/pgfincore-v1.1.1.tar.gz

$ tar -zxvf pgfincore-v1.1.1.tar.gz

$ cd pgfincore-1.1.1/

$ make

$ make install

2.3 引入扩展

Pg Version >= 9.1:

pgbench=# create extension pgfincore;

CREATE EXTENSION

Other:

psql pgbench -f pgfincore.sql

至此,安装完毕。

查看引入的函数:

pgbench=# select proname,prosrc from pg_proc where proname like 'pgfadvise%' or proname like 'pgfincore%' or proname like 'pgsysconf%';

proname        |                                 prosrc

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

pgsysconf            | pgsysconf

pgsysconf_pretty     |                                                                       +

| select pg_size_pretty(os_page_size)                  as os_page_size, +

|        pg_size_pretty(os_pages_free * os_page_size)  as os_pages_free,+

|        pg_size_pretty(os_total_pages * os_page_size) as os_total_pages+

| from pgsysconf()

pgfadvise            | pgfadvise

pgfadvise_willneed   | SELECT pgfadvise($1, 'main', 10)

pgfadvise_dontneed   | SELECT pgfadvise($1, 'main', 20)

pgfadvise_normal     | SELECT pgfadvise($1, 'main', 30)

pgfadvise_sequential | SELECT pgfadvise($1, 'main', 40)

pgfadvise_random     | SELECT pgfadvise($1, 'main', 50)

pgfadvise_loader     | pgfadvise_loader

pgfadvise_loader     | SELECT pgfadvise_loader($1, 'main', $2, $3, $4, $5)

pgfincore            | pgfincore

pgfincore            | SELECT * from pgfincore($1, 'main', $2)

pgfincore            | SELECT * from pgfincore($1, 'main', false)

(13 rows)

三、示例

3.1 查看当前OS的block与page情况

pgbench=# select * from pgsysconf();

os_page_size | os_pages_free | os_total_pages

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

4096 |         30991 |         255086

(1 row)

pgbench=# select * from pgsysconf_pretty();

os_page_size | os_pages_free | os_total_pages

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

4096 bytes   | 121 MB        | 996 MB

(1 row)

os_page_size:操作系统内存块大小

os_pages_free:操作系统空闲的内存大小

os_total_pages:操作系统总共的内存大小

3.2 缓存对象

pgbench=# select * from pgfadvise_willneed('pgbench_accounts');

relpath      | os_page_size | rel_os_pages | os_pages_free

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

base/16384/16460 |         4096 |        33250 |         32033

(1 row)

rel_os_pages:该对象在操作系统内存中占用的内存页

3.3 卸载对象

pgbench=# select * from pgfadvise_dontneed('pgbench_accounts');

relpath      | os_page_size | rel_os_pages | os_pages_free

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

base/16384/16460 |         4096 |        33250 |         65234

(1 row)

四、参考

http://pgfoundry.org/projects/pgfincore/

https://github.com/klando/pgfincore

作者:连顺科的个人页面
连顺科的博客
原文地址:pgfincore, 感谢原作者分享。