Postgresql的日誌配置

背景

  公司的項目中使用了postgresql(簡稱pg)作爲其數據庫管理系統,前兩天環境突然崩潰了,頁面無法打開。經過排查,我發現是數據庫所在機器磁盤滿了,通過目錄和文件排序,原來是pg的日誌太多(大約保留了大半年的日誌在磁盤上沒有被清理)。

  我看了下pg的日誌配置,發現基本都是用的默認配置,日誌滾動沒有開啓,於是乎做了下相關配置優化後對pg進行重啓,最後看了pg的日誌滾動,恢復正常了。以下是我梳理的關於pg的日誌配置項。

配置詳解

配置文件:postgresql.conf

配置1:日誌開啓與關閉

默認爲off,設置爲on則pg可以記錄相關日誌,建議打開,否則在數據庫出現異常時候,沒有日誌來定位具體問題

# This is used when logging to stderr:

logging_collector = on # Enable capturing of stderr and csvlog

# into log files. Required to be on for

# csvlogs.

# (change requires restart)

配置2:日誌滾動策略 

# These are only used if logging_collector is on:

#配置日誌目錄,默認爲pg_log即可

log_directory = 'pg_log' # directory where log files are written,

# can be absolute or relative to PGDATA

#pg日誌文件名及其擴展名,默認即可

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,

# can include strftime() escapes

#pg日誌文件的權限,默認即可

log_file_mode = 0600 # creation mode for log files,

#開啓日誌滾動階段,這裏需要設置爲on

log_truncate_on_rotation = on # If on, an existing log file with the

#日誌保留天數,這裏看實際環境,如果是測試建議1d,如果是生產環境建議7d

log_rotation_age = 1d # Automatic rotation of logfiles will

#單個日誌大小,默認100MB即可,比較標準的配置


配置3:日誌打印時機 

#發送給客戶端的消息級別,建議warning即可,日誌等級越低,打印的內容越多,性能上越有損耗

client_min_messages = warning # values in order of decreasing detail:

# debug5

# debug4

# debug3

# debug2

# debug1

# log

# notice

# warning

# error

#寫到數據庫日誌文件中的消息的級別,建議warning即可,日誌等級越低,打印的內容越多,性能上越有損耗

log_min_messages = warning # values in order of decreasing detail:

# debug5

# debug4

# debug3

# debug2

# debug1

# info

# notice

# warning

# error

# log

# fatal

# panic

#是否記錄導致數據庫出現錯誤的SQL語句,建議warning即可,日誌等級越低,打印的內容越多,性能上越有損耗

log_min_error_statement = error # values in order of decreasing detail:

# debug5

# debug4

# debug3

# debug2

# debug1

# info

# notice

# warning

# error

# log

# fatal

# panic (effectively off)

配置4:數據庫統計監控

#log_statement_stats爲on則會開啓log_parser_stats,log_planner_stats,log_executor_stats這三個選項,生產環境不建議開啓,建議測試環境開啓,用於定位問題。

#log_parser_stats = off

#log_planner_stats = off

#log_executor_stats = off

#log_statement_stats = off 

配置5:慢sql記錄配置

#執行sql時間爲2s以上的sql都會被記錄下來

log_min_duration_statement = 2s

以上配置再修改完之後,均需要重啓pg生效。


博主:測試生財

座右銘:專注測試與自動化,致力提高研發效能;通過測試精進完成原始積累,通過讀書理財奔向財務自由。

csdn:https://blog.csdn.net/ccgshigao

博客園:https://www.cnblogs.com/qa-freeroad/

51cto:https://blog.51cto.com/14900374


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章