Postgresql的日志配置教程詳解
背景
公司的項(xiàng)目中使用了postgresql(簡(jiǎn)稱pg)作為其數(shù)據(jù)庫(kù)管理系統(tǒng),前兩天環(huán)境突然崩潰了,頁(yè)面無法打開。經(jīng)過排查,我發(fā)現(xiàn)是數(shù)據(jù)庫(kù)所在機(jī)器磁盤滿了,通過目錄和文件排序,原來是pg的日志太多(大約保留了大半年的日志在磁盤上沒有被清理)。
我看了下pg的日志配置,發(fā)現(xiàn)基本都是用的默認(rèn)配置,日志滾動(dòng)沒有開啟,于是乎做了下相關(guān)配置優(yōu)化后對(duì)pg進(jìn)行重啟,最后看了pg的日志滾動(dòng),恢復(fù)正常了。以下是我梳理的關(guān)于pg的日志配置項(xiàng)。
配置詳解
配置文件:postgresql.conf
配置1:日志開啟與關(guān)閉
默認(rèn)為off,設(shè)置為on則pg可以記錄相關(guān)日志,建議打開,否則在數(shù)據(jù)庫(kù)出現(xiàn)異常時(shí)候,沒有日志來定位具體問題
# 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:日志滾動(dòng)策略
# These are only used if logging_collector is on: #配置日志目錄,默認(rèn)為pg_log即可 log_directory = 'pg_log' # directory where log files are written, # can be absolute or relative to PGDATA #pg日志文件名及其擴(kuò)展名,默認(rèn)即可 log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern, # can include strftime() escapes #pg日志文件的權(quán)限,默認(rèn)即可 log_file_mode = 0600 # creation mode for log files, #開啟日志滾動(dòng)階段,這里需要設(shè)置為on log_truncate_on_rotation =on# If on, an existing log file with the #日志保留天數(shù),這里看實(shí)際環(huán)境,如果是測(cè)試建議1d,如果是生產(chǎn)環(huán)境建議7d log_rotation_age = 1d # Automatic rotation of logfiles will #單個(gè)日志大小,默認(rèn)100MB即可,比較標(biāo)準(zhǔn)的配置
配置3:日志打印時(shí)機(jī)
#發(fā)送給客戶端的消息級(jí)別,建議warning即可,日志等級(jí)越低,打印的內(nèi)容越多,性能上越有損耗 client_min_messages = warning # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # log # notice # warning # error #寫到數(shù)據(jù)庫(kù)日志文件中的消息的級(jí)別,建議warning即可,日志等級(jí)越低,打印的內(nèi)容越多,性能上越有損耗 log_min_messages = warning # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # info # notice # warning # error # log # fatal # panic #是否記錄導(dǎo)致數(shù)據(jù)庫(kù)出現(xiàn)錯(cuò)誤的SQL語(yǔ)句,建議warning即可,日志等級(jí)越低,打印的內(nèi)容越多,性能上越有損耗 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:數(shù)據(jù)庫(kù)統(tǒng)計(jì)監(jiān)控
#log_statement_stats為on則會(huì)開啟log_parser_stats,log_planner_stats,log_executor_stats這三個(gè)選項(xiàng),生產(chǎn)環(huán)境不建議開啟,建議測(cè)試環(huán)境開啟,用于定位問題。 #log_parser_stats = off #log_planner_stats = off #log_executor_stats = off #log_statement_stats = off
配置5:慢sql記錄配置
#執(zhí)行sql時(shí)間為2s以上的sql都會(huì)被記錄下來 log_min_duration_statement = 2s
以上配置再修改完之后,均需要重啟pg生效。
到此這篇關(guān)于Postgresql的日志配置的文章就介紹到這了,更多相關(guān)Postgresql日志配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PostgreSQL生成列實(shí)現(xiàn)過程介紹
PostgreSQL 12 增加新的特性——生成列(Generated Columns),也就是計(jì)算列。在之前版本也可以實(shí)現(xiàn),但需要定義函數(shù)和觸發(fā)器,利用該功能可以更容易使用并可以提升性能。生成列是給表指定計(jì)算列,其數(shù)據(jù)可以根據(jù)其他列數(shù)據(jù)自動(dòng)生成,當(dāng)原數(shù)據(jù)更新時(shí)其自動(dòng)更新2023-01-01PostgreSQL教程(十四):數(shù)據(jù)庫(kù)維護(hù)
這篇文章主要介紹了PostgreSQL教程(十四):數(shù)據(jù)庫(kù)維護(hù),本文講解了恢復(fù)磁盤空間、更新規(guī)劃器統(tǒng)計(jì)、VACUUM和ANALYZE的示例、定期重建索引等內(nèi)容,需要的朋友可以參考下2015-05-05使用PostgreSQL創(chuàng)建高級(jí)搜索引擎的代碼示例
本文我們將探索PostgreSQL中的全文搜索功能,并研究我們能夠復(fù)制多少典型搜索引擎功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2023-07-07PostgreSQL流復(fù)制參數(shù)max_wal_senders的用法說明
這篇文章主要介紹了PostgreSQL流復(fù)制參數(shù)max_wal_senders的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12