对PostgreSQL中NBuffers的理解

开始

看PostgreSQL 中  shared_buffers 的值 是  32MB

打印 src/backend/storage/buffer/bufmgr.c 中, NBuffers 的值:

结果:

[postgres@localhost bin]$ ./postgres -D /usr/local/pgsql/data
LOG:  database system was shut down at 2012-11-01 17:19:27 CST
NBuffers is: 4096
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
NBuffers is: 4096
......

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

再看NBuffers 的资料:-B NBuffers 是可以作为 Postgres 运行时候的命令行参数的。

http://www.postgresql.org/docs/current/static/app-postgres.html

-B nbuffers

Sets the number of shared buffers for use by the server processes. The default value of this parameter is chosen automatically by initdb. Specifying this option is equivalent to setting the shared_buffers configuration parameter.

也就是说 NBuffers 和 Shared_Buffers 应该相同。

32MB是如何等于 4096的呢。

32MB=32*1024*1024=33554432 字节

4096页*8K/页=4096*8*1024=33554432字节。

看一下修改后会如何:

[postgres@localhost bin]$ ./postgres -D /usr/local/pgsql/data -B 8192
LOG:  database system was shut down at 2012-11-02 09:32:27 CST
NBuffers is: 8192
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
NBuffers is: 8192
NBuffers is: 8192
NBuffers is: 8192
[postgres@localhost bin]$ ./psql
psql (9.2.0)
Type "help" for help.

postgres=# show shared_buffers;
 shared_buffers 
----------------
 64MB
(1 row)

postgres=# 

guc.c 中的代码,也可以论证其对应关系:

/*                                    
 * We sometimes multiply the number of shared buffers by two without                                    
 * checking for overflow, so we mustn't allow more than INT_MAX / 2.                                    
 */                                    
{                                    
    {"shared_buffers", PGC_POSTMASTER, RESOURCES_MEM,                                
        gettext_noop("Sets the number of shared memory buffers used by the server."),                            
        NULL,                            
        GUC_UNIT_BLOCKS                            
    },                                
    &NBuffers,                                
    1024, 16, INT_MAX / 2,                                
    NULL, NULL, NULL                                
},                                    
.....

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

结束

原文地址:https://www.cnblogs.com/gaojian/p/2750639.html