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

相关文章
相关标签/搜索