您的位置:首页 > 数据库

Postgresql的日志配置

2021-01-06 08:44 796 查看

背景

  公司的项目中使用了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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: