1. 程式人生 > 資料庫 >postgresql 中的引數檢視和修改方式

postgresql 中的引數檢視和修改方式

1.檢視引數檔案的位置

使用show 命令檢視,比較常用的show config_file.此還可以檢視pg_settings資料字典.

test=# show config_file;
     config_file     
------------------------------
 /data/pgdata/postgresql.conf
(1 row)
test=# show hba_file 
test-# ;
     hba_file     
--------------------------
 /data/pgdata/pg_hba.conf
(1 row)
test=# show ident_file ;
     ident_file     
----------------------------
 /data/pgdata/pg_ident.conf

2.檢視當前會話的引數值

可以使用show命令或者檢視pg_settings字典.

使用show all可以檢視全部的引數值.show 引數名檢視指定引數

test=# show all;
-------------------------------------+------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------
 allow_system_table_mods       | off                         | Allows modifications of the structure of system tables.
 application_name          | psql                         | Sets the application name to be reported in statistics and logs.
 archive_command           | test ! -f /data/archive/%f && cp %p /data/archive/%f | Sets the shell command that will be called to archive a WAL file.
 archive_mode            | on                          | Allows archiving of WAL files using archive_command.
 archive_timeout           | 0                          | Forces a switch to the next WAL file if a new file has not been started within N seconds.
 array_nulls             | on                          | Enable input of NULL elements in arrays.
...
 
test=# show work_mem;
 work_mem 
----------
 4MB
(1 row)
 
test=# \x
Expanded display is on.
test=# select * from pg_settings where name in ('work_mem')
test-# ;
-[ RECORD 1 ]---+----------------------------------------------------------------------------------------------------------------------
name      | work_mem
setting     | 4096
unit      | kB
category    | Resource Usage / Memory
short_desc   | Sets the maximum memory to be used for query workspaces.
extra_desc   | This much memory can be used by each internal sort operation and hash table before switching to temporary disk files.
context     | user
vartype     | integer
source     | default
min_val     | 64
max_val     | 2147483647
enumvals    | 
boot_val    | 4096
reset_val    | 4096
sourcefile   | 
sourceline   | 
pending_restart | f

3.修改pg的引數值

1.全域性修改pg的引數.

有些引數只有當pg服務重啟的時候才生效,典型的例子就是shared_buffers,定義了共享記憶體的大小.

許多引數在pg服務執行的時候就能修改.再更改之後像伺服器執行一個reload操作,強制pg重新讀取postgresql.conf,因此你只需要編輯postgresql.conf檔案,再執行 pg_ctl reload 即可 . 對於需要重啟的,在修改完postgresql後需要執行 pg_ctl restart

對於9.5以後的版本,可以通過檢視pg_file_settings檢視你設定的引數是否生效.例如如果你設定了一個引數需要重啟資料庫才能生效或者設定錯誤,那麼在此字典中會出現報錯.

test=# select * from pg_file_settings where error is not null;
      sourcefile       | sourceline | seqno |   name    | setting | applied |      error       
-----------------------------------+------------+-------+-----------------+---------+---------+------------------------------
 /data/pgdata/postgresql.auto.conf |     4 |  22 | max_connections | 10000  | f    | setting could not be applied
(1 row)

對於9.4以後的版本,你還可以使用 alter system 命令修改引數.使用alter system命令將修改postgresql.auto.conf檔案,而不是postgresql.conf,這樣可以很好的保護postgresql.conf檔案,加入你使用很多alter system命令後搞的一團糟,那麼你只需要刪除postgresql.auto.conf,再重新載入即可.

test=# show work_mem;
 work_mem 
----------
 4MB
(1 row)
test=# alter system set work_mem='8MB';
ALTER SYSTEM
test=# show work_mem;
 work_mem 
----------
 4MB
(1 row)

檢視postgresql.auto.conf:

[postgres@postgresql1 pgdata]$ cat postgresql.auto.conf 
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
work_mem = '8MB'

使用pg_ctl reload重新load配置檔案,再檢視引數值:

 
test=# show work_mem ;
 work_mem 
----------
 8MB
(1 row)

2.直接使用set命令,在會話層修改,修改之後將被用於未來的每一個事務,只對當前會話有效:

test=# 
test=# set work_mem='16MB';
SET
test=# show work_mem;
 work_mem 
----------
 16MB
(1 row)

我們開啟另外一個會話,檢視work_mem引數,可以發現work_mem還是4MB

postgres=# show work_mem;
 work_mem 
----------
 4MB
(1 row)

3.set命令後新增 local關鍵字,只在當前事務中修改,只在當前事務內有效:

test=# show work_mem;
 work_mem 
----------
 16MB
(1 row)
test=# begin;
BEGIN
test=# set local work_mem='8MB';
SET
test=# show work_mem;
 work_mem 
----------
 8MB
(1 row)
test=# commit;
COMMIT
test=# show work_mem;
 work_mem 
----------
 16MB

4.使用 reset恢復引數的預設值

再pg_settings字典reset_val欄位表示瞭如果使用reset,則此引數恢復的預設值為多少

使用 reset 引數名 來恢復某個引數的預設值,使用 reset all來恢復所有的引數值.

test=# show work_mem;
 work_mem 
----------
 16MB
(1 row)
test=# reset work_mem;
RESET
test=# show work_mem;
 work_mem 
----------
 4MB
(1 row)
 
test=# reset all;
RESET

5.為特定的使用者組設定引數

一.為特定的資料庫裡的所有的使用者設定引數,例如為test資料庫所有的連線設定work_mem為16MB:

test=# alter database test set work_mem='16MB';
ALTER DATABASE

二.為資料庫中的某個特定使用者設定引數.例如為brent使用者,設定work_mem為2MB:

postgres=# alter role brent set work_mem='2MB';
ALTER ROLE

經過測試發現,如果你同時為資料庫和使用者設定了特定引數,那麼以使用者為準.例如上面的,如果我用brent使用者連線到test資料庫,那麼我的work_mem應該為2MB:

postgres=# \c test brent
You are now connected to database "test" as user "brent".
test=> 
test=> 
test=> show work_mem;
 work_mem 
----------
 2MB

三.為某個特定使用者連線到特定的資料庫設定引數.例如為使用者brent在資料庫test中設定work_mem為8MB

test=# alter role brent in database test set work_mem='8MB';
ALTER ROLE

上面說的三種設定,優先順序遞增,也就是說,如果設定了1,2,3那麼就以第3個為準,2那麼就是以2為準,以此類推.

pg對此的實現方法和當用戶連線資料庫的時候,立刻手動執行set命令的效果完全相同

檢視你當前的引數值是從何處指定,可以通過查詢pg_setttings中的source欄位獲取,例如如果設定了database級別的引數.那麼查詢結果應該如下:

test=# select name,setting,source from pg_settings where name='work_mem';
  name  | setting | source 
----------+---------+----------
 work_mem | 16384  | database

其它的,例如設定了第三種:

test=# \c test brent
You are now connected to database "test" as user "brent".
test=> select name,source from pg_settings where name='work_mem';
  name  | setting |  source   
----------+---------+---------------
 work_mem | 8192  | database user

補充:postgresql重要引數解析及優化

1,max_connections 200

最大客戶端連線數。每個連線在後端都會對應相應的程序,耗費一定的記憶體資源。如果連線數上千,需要使用連線池工具。

2,shared_buffers 25% of total memory

資料庫用於快取資料的記憶體大小。該引數預設值很低(考慮不同的系統平臺),需要調整。不宜太大,很多實踐表明,大於1/3的記憶體會降低效能。

3,effective_cache_size 50%-75% of total memory

This is a guideline for how much memory you expect to be available in the OS and PostgreSQL buffer caches,not an allocation! 這個引數只在查詢優化器選擇時使用,並不是實際分配的記憶體,該引數越大,查詢優化器越傾向於選擇索引掃描。

4,checkpoint_segments 256 checkpoint_completion_target 0.9

checkponit_segments wal個數達到多少個數checkponit,還有一個引數checkponit_timeout,控制最長多長時間checkpoint。對於寫入比較大的資料庫,該值越大越好。但是值越大,執行恢復的時間越長。

checkpoint_completion_target 控制checkponit write 分散寫入,值越大越分散。預設值0.5,0.9是一個比較合適的值。

5,work_mem

用於排序,預設值即可。每個連線都會分配一定work_mem,這個是會實際分配的記憶體,不宜過大,預設值即可。如果要使用語句中有較大的排序操作,可以在會話級別設定該引數,set work_men = ‘2GB',提高執行速度。

6,maintanance_work_mem

維護性操作使用的記憶體。例如:vacuum,create index,alter table add foreign key,restoring database dumps.做這些操作時可以臨時設定該值大小,加快執行速度。set session maintanance_work_mem = ‘2GB';

7,random_page_cost (預設值 4) seq_page_cost(預設值 1)

設定優化器獲取一個隨機頁的cost,相比之下一個順序掃描頁的cost為1.

當使用較快的儲存,如raid arrays,scsi,ssd時,可以適當調低該值。有利於優化器懸著索引掃描。ssd 時,可以設定為2.

8,autovacuum

—maintenance_work_mem 1-2GB

—autovacuum_max_workers

如果有多個小型表,分配更多的workers,更少的mem。

大型表,更多的men,更少的workers。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援我們。如有錯誤或未考慮完全的地方,望不吝賜教。