當(dāng)前位置:首頁 >  站長 >  數(shù)據(jù)庫 >  正文

postgresql 中的參數(shù)查看和修改方式

 2021-06-05 17:11  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價,好“米”不錯過

這篇文章主要介紹了postgresql 中的參數(shù)查看和修改方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。

1.查看參數(shù)文件的位置

使用show 命令查看,比較常用的show config_file.此還可以查看pg_settings數(shù)據(jù)字典.

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.查看當(dāng)前會話的參數(shù)值

可以使用show命令或者查看pg_settings字典.

使用show all可以查看全部的參數(shù)值.show 參數(shù)名查看指定參數(shù)

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的參數(shù)值

1.全局修改pg的參數(shù).

有些參數(shù)只有當(dāng)pg服務(wù)重啟的時候才生效,典型的例子就是shared_buffers,定義了共享內(nèi)存的大小.

許多參數(shù)在pg服務(wù)運(yùn)行的時候就能修改.再更改之后像服務(wù)器執(zhí)行一個reload操作,強(qiáng)制pg重新讀取postgresql.conf,因此你只需要編輯postgresql.conf文件,再執(zhí)行 pg_ctl reload 即可 . 對于需要重啟的,在修改完postgresql后需要執(zhí)行 pg_ctl restart

對于9.5以后的版本,可以通過查看pg_file_settings查看你設(shè)置的參數(shù)是否生效.例如如果你設(shè)置了一個參數(shù)需要重啟數(shù)據(jù)庫才能生效或者設(shè)置錯誤,那么在此字典中會出現(xiàn)報錯.

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 命令修改參數(shù).使用alter system命令將修改postgresql.auto.conf文件,而不是postgresql.conf,這樣可以很好的保護(hù)postgresql.conf文件,加入你使用很多alter system命令后搞的一團(tuán)糟,那么你只需要刪除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)

by the ALTER SYSTEM command.
work_mem = '8MB'

 

使用pg_ctl reload重新load配置文件,再查看參數(shù)值:

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

 

2.直接使用set命令,在會話層修改,修改之后將被用于未來的每一個事務(wù),只對當(dāng)前會話有效:

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

 

我們打開另外一個會話,查看work_mem參數(shù),可以發(fā)現(xiàn)work_mem還是4MB

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

 

3.set命令后添加 local關(guān)鍵字, 只在當(dāng)前事務(wù)中修改,只在當(dāng)前事務(wù)內(nèi)有效:

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恢復(fù)參數(shù)的默認(rèn)值

再pg_settings字典reset_val字段表示了如果使用reset,則此參數(shù)恢復(fù)的默認(rèn)值為多少

使用 reset 參數(shù)名 來恢復(fù)某個參數(shù)的默認(rèn)值,使用 reset all來恢復(fù)所有的參數(shù)值.

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.為特定的用戶組設(shè)置參數(shù)

一.為特定的數(shù)據(jù)庫里的所有的用戶設(shè)置參數(shù),例如為test數(shù)據(jù)庫所有的連接設(shè)置work_mem為16MB:

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

二.為數(shù)據(jù)庫中的某個特定用戶設(shè)置參數(shù).例如為brent用戶,設(shè)置work_mem為2MB:

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

經(jīng)過測試發(fā)現(xiàn),如果你同時為數(shù)據(jù)庫和用戶設(shè)置了特定參數(shù),那么以用戶為準(zhǔn).例如上面的,如果我用brent用戶連接到test數(shù)據(jù)庫,那么我的work_mem應(yīng)該為2MB:

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

 

三.為某個特定用戶連接到特定的數(shù)據(jù)庫設(shè)置參數(shù).例如為用戶brent在數(shù)據(jù)庫test中設(shè)置work_mem為8MB

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

 

上面說的三種設(shè)置,優(yōu)先級遞增,也就是說,如果設(shè)置了1,2,3那么就以第3個為準(zhǔn),如果設(shè)置了1,2那么就是以2為準(zhǔn),以此類推.

pg對此的實現(xiàn)方法和當(dāng)用戶連接數(shù)據(jù)庫的時候,立刻手動執(zhí)行set命令的效果完全相同

查看你當(dāng)前的參數(shù)值是從何處指定,可以通過查詢pg_setttings中的source字段獲取,例如如果設(shè)置了database級別的參數(shù).那么查詢結(jié)果應(yīng)該如下:

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

 

其它的,例如設(shè)置了第三種:

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

 

補(bǔ)充:postgresql重要參數(shù)解析及優(yōu)化

1,max_connections 200

最大客戶端連接數(shù)。每個連接在后端都會對應(yīng)相應(yīng)的進(jìn)程,耗費(fèi)一定的內(nèi)存資源。如果連接數(shù)上千,需要使用連接池工具。

2,shared_buffers 25% of total memory

數(shù)據(jù)庫用于緩存數(shù)據(jù)的內(nèi)存大小。該參數(shù)默認(rèn)值很低(考慮不同的系統(tǒng)平臺),需要調(diào)整。不宜太大,很多實踐表明,大于1/3的內(nèi)存會降低性能。

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! 這個參數(shù)只在查詢優(yōu)化器選擇時使用,并不是實際分配的內(nèi)存,該參數(shù)越大,查詢優(yōu)化器越傾向于選擇索引掃描。

4,checkpoint_segments 256 checkpoint_completion_target 0.9

checkponit_segments wal個數(shù)達(dá)到多少個數(shù)checkponit,還有一個參數(shù)checkponit_timeout,控制最長多長時間checkpoint。對于寫入比較大的數(shù)據(jù)庫,該值越大越好。但是值越大,執(zhí)行恢復(fù)的時間越長。

checkpoint_completion_target 控制checkponit write 分散寫入,值越大越分散。默認(rèn)值0.5,0.9是一個比較合適的值。

5,work_mem

用于排序,默認(rèn)值即可。每個連接都會分配一定work_mem,這個是會實際分配的內(nèi)存,不宜過大,默認(rèn)值即可。如果要使用語句中有較大的排序操作,可以在會話級別設(shè)置該參數(shù),set work_men = ‘2GB',提高執(zhí)行速度。

6,maintanance_work_mem

維護(hù)性操作使用的內(nèi)存。例如:vacuum ,create index,alter table add foreign key,restoring database dumps.做這些操作時可以臨時設(shè)置該值大小,加快執(zhí)行速度。set session maintanance_work_mem = ‘2GB';

7,random_page_cost (默認(rèn)值 4) seq_page_cost(默認(rèn)值 1)

設(shè)置優(yōu)化器獲取一個隨機(jī)頁的cost,相比之下一個順序掃描頁的cost為1.

當(dāng)使用較快的存儲,如raid arrays,scsi,ssd時,可以適當(dāng)調(diào)低該值。有利于優(yōu)化器懸著索引掃描。ssd 時,可以設(shè)置為2.

8,autovacuum

—maintenance_work_mem 1-2GB

—autovacuum_max_workers

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

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

文章來源:腳本之家

來源地址:https://www.jb51.net/article/204220.htm

申請創(chuàng)業(yè)報道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)文章

熱門排行

信息推薦