Postgresql日志收集
PG安裝完成后默認不會記錄日志,必須修改對應的(${PGDATA}/postgresql.conf)配置才可以,這里只介紹常用的日志配置。
1.logging_collector = on/off ---- 是否將日志重定向至文件中,默認是off(該配置修改后,需要重啟DB服務)
DB安裝完成,啟動的服務進程如下
[root@localhost ~]# ps -elf | grep postgres 0 S postgres 2385 1 0 80 0 - 66829 poll_s 12:41 ? 00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata 1 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中。
[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 0 S postgres 2448 1 0 80 0 - 66830 poll_s 12:50 ? 00:00:00 /opt/pg9.6/bin/postgres -D /mnt/pgdata 1 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
以下配置修改不需要重啟服務,只需重載配置
[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,即集群目錄下,但是日志文件可能會非常多,建議將日志重定向到其他目錄或分區。
將此配置修改為/var/log/pg_log下,必須先創建此目錄,並修改權限,如
[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而被鎖時是否產生一個日志信息。在判斷一個鎖等待是否會影響性能時是有用的,缺省是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' ---- 日志時區,最好和服務器設置同一個時區,方便問題定位