天天看點

【轉自譚峰】PostgreSQL9.5:新增參數設定神器:pg_file_settings 視圖

來源于:

http://francs3.blog.163.com/blog/static/405767272015711115245190/

9.5 版本新增 pg_file_settings 視圖,可謂參數設定的神器,為啥這麼說呢? 因為 postgresql.conf 參數值調整後,有些 reload 後就生效了,有些需要重新開機服務才生效,如果你設定的參數值是非法的, pg_ctl reload 指令也不報錯,這時很讓人尴尬,reload 後還得連到資料庫裡去 show 參數值,确認參數值是否生效,9.5 版本新增 pg_file_settings 視圖,讓這項工作容易很多。

一 關于 pg_file_settings 視圖

--pg_file_settings 視圖
postgres=# \d pg_file_settings 
View "pg_catalog.pg_file_settings"
   Column   |  Type   | Modifiers 
------------+---------+-----------
 sourcefile | text    | 
 sourceline | integer | 
 seqno      | integer | 
 name       | text    | 
 setting    | text    | 
 applied    | boolean | 
 error      | te
 
postgres=# select * from pg_file_settings limit 3;
               sourcefile               | sourceline | seqno |       name       | setting | applied | error 
----------------------------------------+------------+-------+------------------+---------+---------+-------
 /database/pg95/pg_root/postgresql.conf |         59 |     1 | listen_addresses | *       | t       | 
 /database/pg95/pg_root/postgresql.conf |         63 |     2 | port             | 1931    | t       | 
 /database/pg95/pg_root/postgresql.conf |         64 |     3 | max_connections  | 100     | t       | 
(3 rows)
           

備注:sourcefile: 配置檔案名稱

            sourceline:配置參數位于配置檔案的行數

            seqno:     配置參數的序列号

            name:       參數名稱

            setting:   參數目前設定值

            applied:   參數設定成功與否标志,設定成功為 t

            error:      如果非空,表示此參數被 applied 時的報錯資訊

      接下來做兩項測試: 測試一: 設定  log_statement 參數成非法值; 測試二: 設定需要重新開機服務才生效的參數。

  二 測試一: 設定  log_statement 參數成非法值       --設定 log_statement 參數成非法值

[[email protected] pg_root]$ grep "log_statement =" postgresql.conf
log_statement = 'ddd'                   # none, ddl, mod, all 
           

--reload 配置

[[email protected] pg_root]$ pg_ctl reload
server signaled
           

備注: postgresql.conf 參數調整後,不管設定成功與否, pg_ctl reload 參數是不輸出相關資訊的,9.5 版本之前要确認參數是否成功有兩種方法,一種是檢視相關 pg_log , 例如

--檢視報錯日志

[[email protected] pg_log]$ grep "log_statement" postgresql-2015-08-11_000000.csv
2015-08-11 10:36:36.177 CST,,,31634,,55c83dc1.7b92,9,,2015-08-10 13:59:29 CST,,0,LOG,22023,"invalid value for parameter ""log_statement"": ""ddd""",,"Available values: none, ddl, mod, all.",,,,,,,""
2015-08-11 10:43:25.063 CST,,,31634,,55c83dc1.7b92,12,,2015-08-10 13:59:29 CST,,0,LOG,22023,"invalid value for parameter ""log_statement"": ""ddd""",,"Available values: none, ddl, mod, all.",,,,,,,""
           

--另一種方式是連接配接到資料庫中檢視

[[email protected] pg_log]$ psql -c "show log_statement"
 log_statement 
---------------
 none
(1 row)
           

備注:可見 log_statement 參數設定無效。 9.5 版本之後,可以通過 pg_file_settings 視圖檢視。

--檢視 pg_file_settings 中的錯誤資訊

postgres=# select * from pg_file_settings where error is not null;
-[ RECORD 1 ]--------------------------------------
sourcefile | /database/pg95/pg_root/postgresql.conf
sourceline | 441
seqno      | 32
name       | log_statement
setting    | ddd
applied    | f
error      | setting could not be applied
           

備注:錯誤資訊簡單明了。

三 測試二: 設定需要重新開機服務才生效的參數--修改 max_connections 參數

[[email protected] pg_root]$ grep "max_connections =" postgresql.conf
max_connections = 100                   # (change requires restart)
           

備注: max_connections 參數預設為 100,這個參數設定後需要重新開機才生效,我們把它設定成 200。

[[email protected] pg_root]$ grep "max_connections =" postgresql.conf
max_connections = 200                   # (change requires restart)

[[email protected] pg_root]$ pg_ctl reload
server signaled
           

--資料庫檢視

postgres=# select * from pg_file_settings where name='max_connections';
-[ RECORD 1 ]--------------------------------------
sourcefile | /database/pg95/pg_root/postgresql.conf
sourceline | 64
seqno      | 3
name       | max_connections
setting    | 200
applied    | f
error      | setting could not be applied
           

備注:max_connections 參數的 ERROR 資訊很明顯了,新增這個視圖将讓參數設定這項工作變得容易 。

四 參考

  • pg_file_settings

繼續閱讀