8.PG的日志文件
Postgresql的日志分为运行日志、预写日志、事务日志和服务器日志。
1)运行日志
运行日志,在默认情况下是不开启的,通过主要的参数文件postgresql.conf的配置可以看到相关的参数设置。运行日志一般用来记录数据库服务器端与数据库的状态,如各种错误信息、定位慢日志、数据库的启动、关闭信息,发生检查点过于频繁的告警信息。
运行日志有.csv和.log格式,建议使用.csv格式,因为使用.csv格式可以按照大小和时间自动切割。
运行日志的参数:
#log_destination = 'stderr' # Valid values are combinations of # stderr, csvlog, jsonlog, syslog, and # eventlog, depending on platform. # csvlog and jsonlog require # logging_collector to be on. # This is used when logging to stderr: #logging_collector = off # Enable capturing of stderr, jsonlog, # and csvlog into log files. Required # to be on for csvlogs and jsonlogs. # (change requires restart) # These are only used if logging_collector is on: #log_directory = 'log' # directory where log files are written, # can be absolute or relative to PGDATA #log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern, # can include strftime() escapes #log_file_mode = 0600 # creation mode for log files, # begin with 0 to use octal notation #log_rotation_age = 1d # Automatic rotation of logfiles will # happen after that time. 0 disables. #log_rotation_size = 10MB # Automatic rotation of logfiles will # happen after that much log output. # 0 disables. #log_truncate_on_rotation = off # If on, an existing log file with the # same name as the new log file will be # truncated rather than appended to. # But such truncation only occurs on # time-driven rotation, not on restarts # or size-driven rotation. Default is # off, meaning append to existing files # in all cases.
2)预写日志
pg_xlog目录下记录的是PG的预写日志信息,预写日志是保证数据完整性的一种标准方法,简单来说,就是在pg中需要对数据文件进行修改时,必须先写入预写日志信息,即只有在预写日志记录完成持久化,并且刷新到永久存储中后,才能更改数据文件,根据这个原理就不需要再每次提交事务时都将数据刷新到磁盘中。
因为在数据库出现宕机发生数据丢失时,可以重新执行预写日志来达到恢复数据库的目的,所以也可以将预写日志称为重做日志--任何没有写到数据文件中的改动都可以根据日志记录进行重做。
在默认情况下,单个预写日志的大小是16MB,单个预写日志文件的大小由参数wal_segment_size决定。预写日志默认是在pg_wal目录下。它是由十六进制的24个字符组成,每8个字符为一组,每组的意义如下所示
00000001 00000000 00000001 时间线 逻辑ID 物理ID
每个预写日志写满之后,会自动切换到下一个预写日志文件,可以采用手工方式切换预写日志文件
# 查看当前已有的预写日志文件 postgres=# select * from pg_ls_waldir(); name | size | modification --------------------------+----------+------------------------ 000000010000000000000001 | 16777216 | 2023-12-23 23:42:04+08 (1 row) # 进行预写日志文件的手工切换 postgres=# select pg_switch_wal(); pg_switch_wal --------------- 0/1953EF0 (1 row) # 再次查看当前已有的预写日志 postgres=# select * from pg_ls_waldir(); name | size | modification --------------------------+----------+------------------------ 000000010000000000000002 | 16777216 | 2023-12-23 23:54:04+08 000000010000000000000001 | 16777216 | 2023-12-23 23:54:03+08 (2 rows) postgres=#
预写日志的几个重要参数
#wal_level = replica # minimal, replica, or logical # (change requires restart) #fsync = on # flush data to disk for crash safety max_wal_size = 1GB min_wal_size = 80MB
max_wal_size:当预写日志文件的大小超过参数max_wal_size设置的值时,将发生预写日志信息的覆盖,从而造成日志信息的丢失,因次,为了保证数据安全,建议在生产环境开启预写日志的归档模式。
查看预写日志的内容:
[postgres@iZwz909xeqcc7ouqk8528zZ pg_wal]$ pg_waldump 000000010000000000000004 rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn: 0/04000028, prev 0/030002E0, desc: CHECKPOINT_SHUTDOWN redo 0/4000028; tli 1; prev tli 1; fpw true; xid 0:749; oid 16397; multi 1; offset 0; oldest xid 723 in DB 1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; oldest running xid 0; shutdown pg_waldump: error: error in WAL record at 0/4000028: invalid record length at 0/40000A0: expected at least 24, got 0 [postgres@iZwz909xeqcc7ouqk8528zZ pg_wal]$
3)事务日志
pg_xact是事务日志,记录了事务的元数据,默认开启,事务日志的内容一般不能被直接读取。默认存储在$PGDATA/pg_xact/ 目录下。
4)服务器日志
如果使用pg_ctl启动时没有使用参数-l 来指定服务器日志,那么错误可能会输出到cmd前台。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)