溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

postgresql錯誤日志收集的方法

發(fā)布時間:2020-07-20 14:50:36 來源:億速云 閱讀:331 作者:清晨 欄目:編程語言

小編給大家分享一下postgresql錯誤日志收集的方法,相信大部分人都還不怎么了解,因此分享這邊文章給大家學習,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去學習方法吧!

PG安裝完成后默認不會記錄日志,必須修改對應的(${PGDATA}/postgresql.conf)配置才可以,這里只介紹常用的日志配置。

1、logging_collector = on/off  ----  是否將日志重定向至文件中,默認是off(該配置修改后,需要重啟DB服務).

DB安裝完成,啟動的服務進程如下

[root@localhost ~]# ps -elf | grep postgres
S postgres  2385     1  0  80   0 - 66829 poll_s 12:41 ?        00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata
S postgres  2387  2385  0  80   0 - 66829 ep_pol 12:41 ?        00:00:00 postgres: checkpointer process        
S postgres  2388  2385  0  80   0 - 66829 ep_pol 12:41 ?        00:00:00 postgres: writer process              
S postgres  2389  2385  0  80   0 - 66870 ep_pol 12:41 ?        00:00:00 postgres: wal writer process          
S postgres  2390  2385  0  80   0 - 66952 ep_pol 12:41 ?        00:00:00 postgres: autovacuum launcher process   
S postgres  2391  2385  0  80   0 - 29643 ep_pol 12:41 ?        00:00:00 postgres: stats collector process

將此配置修改為on,并重啟DB服務,DB啟動過程中會提示將日志重定向${PGDATA}/pg_log中。

[root@localhost ~]# su -l postgres -c '/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata start'server starting
LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "pg_log".

 此時在運行的進程如下,與之前相比,多了一個logger進程。

[root@localhost ~]# ps -elf | grep postgres
S postgres  2448     1  0  80   0 - 66830 poll_s 12:50 ?        00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata
S postgres  2449  2448  0  80   0 - 29645 ep_pol 12:50 ?        00:00:00 postgres: logger process              
S postgres  2451  2448  0  80   0 - 66830 ep_pol 12:50 ?        00:00:00 postgres: checkpointer process        
S postgres  2452  2448  0  80   0 - 66830 ep_pol 12:50 ?        00:00:00 postgres: writer process              
S postgres  2453  2448  0  80   0 - 66871 ep_pol 12:50 ?        00:00:00 postgres: wal writer process          
S postgres  2454  2448  0  80   0 - 66953 ep_pol 12:50 ?        00:00:00 postgres: autovacuum launcher process   
S postgres  2455  2448  0  80   0 - 29644 ep_pol 12:50 ?        00:00:00 postgres: stats collector process

以下配置修改不需要重啟服務,只需重載配置

[root@localhost ~]# su -l postgres -c '/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata reload'

2、log_directory = 'pg_log' ---- 日志文件目錄,默認是PGDATA的相對路徑,即PGDATA的相對路徑,即{PGDATA}/pg_log,也可以改為絕對路徑

默認為${PGDATA}/pg_log,即集群目錄下,但是日志文件可能會非常多,建議將日志重定向到其他目錄或分區(qū)。

將此配置修改為/var/log/pg_log下,必須先創(chuàng)建此目錄,并修改權(quán)限,如

[root@localhost ~]# mkdir -p /var/log/pg_log
[root@localhost ~]# chown postgres:postgres /var/log/pg_log/[root@localhost ~]# chmod 700 /var/log/pg_log/

重啟DB服務后,日志將重定向至/var/log/pg_log下

[root@localhost ~]# su -l postgres -c '/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata start'
server starting
LOG:  redirecting log output to logging collector process
HINT:  Future log output will appear in directory "/var/log/pg_log".

[root@localhost ~]# ls /var/log/pg_log/
postgresql-2016-06-18_130611.log

3.log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' ---- 日志文件命名形式,使用默認即可

4. log_rotation_age = 1d ----  單個日志文件的生存期,默認1天,在日志文件大小沒有達到log_rotation_size時,一天只生成一個日志文件

5. log_rotation_size = 10MB  ---- 單個日志文件的大小,如果時間沒有超過log_rotation_age,一個日志文件最大只能到10M,否則將新生成一個日志文件。

6.log_truncate_on_rotation = off ---- 當日志文件已存在時,該配置如果為off,新生成的日志將在文件尾部追加,如果為on,則會覆蓋原來的日志。

7.log_lock_waits = off ---- 控制當一個會話等待時間超過deadlock_timeout而被鎖時是否產(chǎn)生一個日志信息。在判斷一個鎖等待是否會影響性能時是有用的,缺省是off。

8.log_statement = 'none' # none, ddl, mod, all ---- 控制記錄哪些SQL語句。none不記錄,ddl記錄所有數(shù)據(jù)定義命令,比如CREATE,ALTER,和DROP 語句。mod記錄所有ddl語句,加上數(shù)據(jù)修改語句INSERT,UPDATE等,all記錄所有執(zhí)行的語句,將此配置設置為all可跟蹤整個數(shù)據(jù)庫執(zhí)行的SQL語句。

9.log_duration = off ---- 記錄每條SQL語句執(zhí)行完成消耗的時間,將此配置設置為on,用于統(tǒng)計哪些SQL語句耗時較長。

10.log_min_duration_statement = -1 # -1 is disabled, 0 logs all statements and their durations, > 0 logs only statements running at least this number of milliseconds

-1表示不可用,0將記錄所有SQL語句和它們的耗時,>0只記錄那些耗時超過(或等于)這個值(ms)的SQL語句。個人更喜歡使用該配置來跟蹤那些耗時較長,可能存在性能問題的SQL語句。雖然使用log_statement和log_duration也能夠統(tǒng)計SQL語句及耗時,但是SQL語句和耗時統(tǒng)計結(jié)果可能相差很多行,或在不同的文件中,但是log_min_duration_statement會將SQL語句和耗時在同一行記錄,更方便閱讀。

11.log_connections = off ----是否記錄連接日志

12.log_disconnections = off ---- 是否記錄連接斷開日志

13.log_line_prefix = '%m %p %u %d %r ' ---- 日志輸出格式(%m,%p實際意義配置文件中有解釋),可根據(jù)自己需要設置(能夠記錄時間,用戶名稱,數(shù)據(jù)庫名稱,客戶端IP和端口,方便定位問題)

14.log_timezone = 'Asia/Shanghai' ---- 日志時區(qū),最好和服務器設置同一個時區(qū),方便問題定位

服務器時區(qū)設置

[root@localhost ~]# cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

以上是postgresql錯誤日志收集的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI