标签
PostgreSQL , 参数 , 优先级 , 配置文件 , alter system , 命令行 , 用户 , 数据库 , 所有用户 , 会话 , 事务 , 函数 , 表
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#%E8%83%8C%E6%99%AF 背景
PostgreSQL 参数配置包罗万象,可以在配置文件 , alter system , 命令行 , 用户 , 数据库 , 所有用户 , 会话 , 事务 , 函数 , 表 等层面进行配置,非常的灵活。
灵活是好,但是可配置的入口太多了,优先级如何?如果在多个入口配置了同一个参数的不同值,最后会以哪个为准?
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#%E5%8F%82%E6%95%B0%E4%BC%98%E5%85%88%E7%BA%A7 参数优先级
优先级如下,数值越大,优先级越高。
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#1-postgresqlconf 1 postgresql.conf
work_mem=1MB
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#2-postgresqlautoconf 2 postgresql.auto.conf
work_mem=2MB
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#3-command-line-options 3 command line options
work_mem=3MB
pg_ctl start -o "-c work_mem='3MB'"
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#4-all-role 4 all role
work_mem=4MB
alter role all set work_mem='4MB';
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#5-database 5 database
work_mem=5MB
alter database postgres set work_mem='5MB';
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#6-role 6 role
work_mem=6MB
alter role digoal set work_mem='6MB';
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#7-session-%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%8F%82%E6%95%B0 7 session (客户端参数)
work_mem=7MB
set work_mem ='7MB';
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#8-%E4%BA%8B%E5%8A%A1 8 事务
work_mem=8MB
postgres=# begin;
BEGIN
postgres=# set local work_mem='8MB';
SET
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#9-function 9 function
(参数在函数内有效,函数调用完成后依旧使用其他最高优先级参数值)
work_mem=9MB
postgres=# create or replace function f_test() returns void as $$
declare
res text;
begin
show work_mem into res;
raise notice '%', res;
end;
$$ language plpgsql strict set work_mem='9MB';
CREATE FUNCTION
postgres=# select f_test();
NOTICE: 9MB
f_test
--------
(1 row)
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#10-table 10 table
TABLE相关参数(垃圾回收相关)
https://www.postgresql.org/docs/11/sql-createtable.htmlautovacuum_enabled
toast.autovacuum_enabled
... ...
autovacuum_vacuum_threshold
toast.autovacuum_vacuum_threshold
... ...
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#%E5%B0%8F%E7%BB%93 小结
PostgreSQL 支持的配置入口:
配置文件(postgresql.conf) ,
alter system(postgresql.auto.conf) ,
命令行(postgres -o, pg_ctl -o) ,
所有用户(alter role all set) ,
数据库(alter database xxx set) ,
用户(alter role 用户名 set) ,
会话(set xxx) ,
事务(set local xxx;) ,
函数(create or replace function .... set par=val;) ,
表(表级垃圾回收相关参数)
如果一个参数在所有入口都配置过,优先级如上,从上到下,优先级越来越大。
https://github.com/digoal/blog/blob/master/201901/20190130_03.md#%E5%8F%82%E8%80%83 参考
《PostgreSQL GUC 参数级别介绍》 《连接PostgreSQL时,如何指定参数》https://github.com/digoal/blog/blob/master/201901/20190130_03.md#%E5%85%8D%E8%B4%B9%E9%A2%86%E5%8F%96%E9%98%BF%E9%87%8C%E4%BA%91rds-postgresql%E5%AE%9E%E4%BE%8Becs%E8%99%9A%E6%8B%9F%E6%9C%BA 免费领取阿里云RDS PostgreSQL实例、ECS虚拟机
