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

前端之家收集整理的这篇文章主要介绍了【转自运维技术 - 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'
#
原文链接:https://www.f2er.com/postgresql/193238.html

猜你在找的Postgre SQL相关文章