天天看點

PostgreSQL 參數優先級講解

标簽

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.html
autovacuum_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虛拟機

PostgreSQL 參數優先級講解