Postgresql的日誌配置

背景

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

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

配置詳解

配置文件:postgresql.confide

配置1:日誌開啓與關閉

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

# 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.ui

# (change requires restart)spa

配置2:日誌滾動策略 

# These are only used if logging_collector is on:.net

#配置日誌目錄,默認爲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

相關文章
相關標籤/搜索