標籤:
PG安裝完成後預設不會記錄日誌,必須修改對應的(${PGDATA}/postgresql.conf)配置才可以,這裡只介紹常用的日誌配置。
1.logging_collector = on/off ---- 是否將日誌重新導向至檔案中,預設是off(該配置修改後,需要重啟DB服務)
DB安裝完成,啟動的服務進程如下
[[email protected] ~]# ps -elf | grep postgres0 S postgres 2385 1 0 80 0 - 66829 poll_s 12:41 ? 00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata1 S postgres 2387 2385 0 80 0 - 66829 ep_pol 12:41 ? 00:00:00 postgres: checkpointer process 1 S postgres 2388 2385 0 80 0 - 66829 ep_pol 12:41 ? 00:00:00 postgres: writer process 1 S postgres 2389 2385 0 80 0 - 66870 ep_pol 12:41 ? 00:00:00 postgres: wal writer process 1 S postgres 2390 2385 0 80 0 - 66952 ep_pol 12:41 ? 00:00:00 postgres: autovacuum launcher process 1 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中。
[[email protected] ~]# su -l postgres -c ‘/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata start‘server startingLOG: redirecting log output to logging collector processHINT: Future log output will appear in directory "pg_log".
此時在啟動並執行進程如下,與之前相比,多了一個logger進程。
[[email protected] ~]# ps -elf | grep postgres0 S postgres 2448 1 0 80 0 - 66830 poll_s 12:50 ? 00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata1 S postgres 2449 2448 0 80 0 - 29645 ep_pol 12:50 ? 00:00:00 postgres: logger process 1 S postgres 2451 2448 0 80 0 - 66830 ep_pol 12:50 ? 00:00:00 postgres: checkpointer process 1 S postgres 2452 2448 0 80 0 - 66830 ep_pol 12:50 ? 00:00:00 postgres: writer process 1 S postgres 2453 2448 0 80 0 - 66871 ep_pol 12:50 ? 00:00:00 postgres: wal writer process 1 S postgres 2454 2448 0 80 0 - 66953 ep_pol 12:50 ? 00:00:00 postgres: autovacuum launcher process 1 S postgres 2455 2448 0 80 0 - 29644 ep_pol 12:50 ? 00:00:00 postgres: stats collector process
以下配置修改不需要重啟服務,只需重載配置
[[email protected] ~]# su -l postgres -c ‘/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata reload‘
2.log_directory = ‘pg_log‘ ---- 記錄檔目錄,預設是${PGDATA}的相對路徑,即${PGDATA}/pg_log,也可以改為絕對路徑
預設為${PGDATA}/pg_log,即叢集目錄下,但是記錄檔可能會非常多,建議將日誌重新導向到其他目錄或分區。
將此配置修改為/var/log/pg_log下,必須先建立此目錄,並修改許可權,如
[[email protected] ~]# mkdir -p /var/log/pg_log[[email protected] ~]# chown postgres:postgres /var/log/pg_log/[[email protected] ~]# chmod 700 /var/log/pg_log/
重啟DB服務後,日誌將重新導向至/var/log/pg_log下
[[email protected] ~]# su -l postgres -c ‘/opt/pg9.6/bin/pg_ctl -D /mnt/pgdata start‘server startingLOG: redirecting log output to logging collector processHINT: Future log output will appear in directory "/var/log/pg_log".[[email protected] ~]# 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而被鎖時是否產生一個日誌資訊。在判斷一個鎖等待是否會影響效能時是有用的,預設是off。
8.log_statement = ‘none‘ # none, ddl, mod, all ---- 控制記錄哪些SQL語句。none不記錄,ddl記錄所有資料定義命令,比如CREATE,ALTER,和DROP 語句。mod記錄所有ddl語句,加上資料修改語句INSERT,UPDATE等,all記錄所有執行的語句,將此配置設定為all可跟蹤整個資料庫執行的SQL語句。
9.log_duration = off ---- 記錄每條SQL語句執行完成消耗的時間,將此配置設定為on,用於統計哪些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也能夠統計SQL語句及耗時,但是SQL語句和耗時統計結果可能相差很多行,或在不同的檔案中,但是log_min_duration_statement會將SQL語句和耗時在同一行記錄,更方便閱讀。
11.log_connections = off ----是否記錄串連日誌
12.log_disconnections = off ---- 是否記錄串連斷開日誌
13.log_line_prefix = ‘%m %p %u %d %r ‘ ---- 日誌輸出格式(%m,%p實際意義設定檔中有解釋),可根據自己需要設定(能夠記錄時間,使用者名稱稱,資料庫名稱,用戶端IP和連接埠,方便定位問題)
14.log_timezone = ‘Asia/Shanghai‘ ---- 日誌時區,最好和伺服器設定同一個時區,方便問題定位
伺服器時區設定
[[email protected] ~]# cp -rf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Postgresql日誌收集