【转自运维技术 - HackRoad.com】PostgreSQL中使用ALTER SYSTEM修改系统参数

本文转自
https://blog.hackroad.com/operations-engineer/linux_server/13212.html



在Postgresql9.5之前的版本中,修改系统参数一般的步骤是:
1、连接到Postgresql数据库服务器;
2、打开postgresql.conf文件,找到需要修改的参数,修改参数的值为所需要的值;
3、执行restart或reload使修改生效;
还有一点比较不方便的是,不能很直观的看到需要重启才生效的参数到底生效了没有,只能把参数show出来观察是否满足预期。
在Postgresql9.5中,增加了ALTER SYstem命令,并且在pg_settings视图中增加了pending_restart(boolean)列,该列标识了是否在重启之后才生效,使得修改参数和查看参数是否生效都方便了很多。步骤可以简化为:
1、执行ALTER SYstem命令
2、执行restart或reload使修改生效;
对于脚本的支持也好了很多,而且通过ALTER SYstem配置的参数都会保存在postgresql.auto.conf文件中,需要清除这些配置也只需要一条命令就可以很方便的处理完成,下面有详细的解释。

ALTER SYstem语法

vincent=# \h ALTER SYstem
Command:     ALTER SYstem
Description: change a server configuration parameter
Syntax:
ALTER SYstem SET configuration_parameter { TO | = } { value | 'value' | DEFAULT }
ALTER SYstem RESET configuration_parameter
ALTER SYstem RESET ALL

vincent=# \h ALTER SYstemCommand: ALTER SYstemDescription: change a server configuration parameterSyntax:ALTER SYstem SET configuration_parameter { TO | = } { value | 'value' | DEFAULT }ALTER SYstem RESET configuration_parameteraLTER SYstem RESET ALL

示例

举两个例子,一个shared_buffers参数,该参数需要重启生效;另一个work_mem参数,该参数只需要pg_ctl reloadSELECT pg_reload_conf();就可以生效。
通过show命令查看,当前的shared_buffers的值为128MB

# show shared_buffers;
 shared_buffers 
----------------
 128MB
(1 row)

vincent=# show shared_buffers; shared_buffers ---------------- 128MB(1 row)

也可以直接从pg_settings表中去查

# \x
Expanded display is on.
# select * from pg_settings where name = 'shared_buffers';
-[ RECORD 1 ]---+-------------------------------------------------------------
name            | shared_buffers
setting         | 16384
unit            | 8kB
category        | Resource Usage / Memory
short_desc      | Sets the number of shared memory buffers used by the server.
extra_desc      | 
context         | postmaster
vartype         | integer
source          | configuration file
min_val         16
max_val         1073741823
enumvals        | 
boot_val        1024
reset_val       16384
sourcefile      /opt/pgdev/data/postgresql.conf
sourceline      115
pending_restart | f

vincent=# \xExpanded display is on.vincent=# select * from pg_settings where name = 'shared_buffers';-[ RECORD 1 ]---+-------------------------------------------------------------name | shared_buffeRSSetting | 16384unit | 8kBcategory | Resource Usage / Memoryshort_desc | Sets the number of shared memory buffers used by the server.extra_desc | context | postmastervartype | integersource | configuration filemin_val | 16max_val | 1073741823enumvals | boot_val | 1024reset_val | 16384sourcefile | /opt/pgdev/data/postgresql.confsourceline | 115pending_restart | f

执行ALTER SYstem命令修改shared_buffers为1GB

# ALTER SYstem SET shared_buffers = '1GB';
ALTER SYstem

vincent=# ALTER SYstem SET shared_buffers = '1GB';ALTER SYstem

查看pg_settings表,有哪些参数在等待生效:

# select name,setting,category,source,sourcefile,pending_restart from pg_settings where pending_restart = true;
-]---+--------------------------------
name            16384
category        / Memory
file
sourcefile      /postgresql.conf
pending_restart | t

vincent=# select name,pending_restart from pg_settings where pending_restart = true;-[ RECORD 1 ]---+--------------------------------name | shared_buffeRSSetting | 16384category | Resource Usage / Memorysource | configuration filesourcefile | /opt/pgdev/data/postgresql.confpending_restart | t

重新启动DB

vincent@dellpad:~$ pg_ctl -D / -m fast restart

vincent@dellpad:~$ pg_ctl -D /opt/pgdev/data/ -m fast restart

通过查询pg_settings表看到shared_buffers已经生效

| setting |        category         |       source       |              sourcefile              | pending_restart 
----------------+---------+-------------------------+--------------------+--------------------------------------+-----------------
 shared_buffers 131072  / Memory file /postgresql.auto.conf | f
)

vincent=# select name,pending_restart from pg_settings where name='shared_buffers'; name | setting | category | source | sourcefile | pending_restart ----------------+---------+-------------------------+--------------------+--------------------------------------+----------------- shared_buffers | 131072 | Resource Usage / Memory | configuration file | /opt/pgdev/data/postgresql.auto.conf | f(1 row)

如果想清除所有通过ALTER SYstem命令配置的参数值,执行

# ALTER SYstem RESET ALL;
ALTER SYstem

vincent=# ALTER SYstem RESET ALL;ALTER SYstem

下面是对work_mem的修改,就不逐步解释了

# ALTER SYstem SET work_mem = '1MB';
ALTER SYstem
# 
| unit | source  | sourcefile | pending_restart 
----------+---------+------+-------------------------+---------+------------+-----------------
 work_mem 4096    | kB   | default |            )
# select pg_reload_conf();
 pg_reload_conf 
----------------
 t
| pending_restart 
----------+---------+------+-------------------------+--------------------+--------------------------------------+-----------------
 work_mem 1024    # \! cat /opt/pgdev/data/postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by ALTER SYstem command.
work_mem = '1MB'
#

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...