修复postgre的检查点文件
Easul Lv6

CentOS 下使用 docker 安装的 PostgreSQL 由于系统异常启动,导致 检查点文件(replorigin_checkpoint)损坏,无法启动数据库。
这里记录一下相关提示及修复步骤。

相关报错如下

找不到检查点的报错

1
2
find: ‘/var/lib/postgresql/data/pg_logical/replorigin_checkpoint’: No such file or directory
chown: cannot access '/var/lib/postgresql/data/pg_logical/replorigin_checkpoint': No such file or directory

检查点的的文件情况显示

1
-?????????  ? ?       ?        ?            ? replorigin_checkpoint

docker日志

1
2
3
4
5
6
7
8
9
10
11
2024-11-29 12:54:31.996 UTC [1] LOG:  starting PostgreSQL 13.5 (Debian 13.5-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2024-11-29 12:54:31.996 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2024-11-29 12:54:31.996 UTC [1] LOG: listening on IPv6 address "::", port 5432
2024-11-29 12:54:32.007 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2024-11-29 12:54:32.018 UTC [26] LOG: database system was interrupted; last known up at 2024-11-29 08:37:53 UTC
2024-11-29 12:54:33.995 UTC [26] LOG: invalid record length at 1/F8030578: wanted 24, got 0
2024-11-29 12:54:33.995 UTC [26] LOG: invalid primary checkpoint record
2024-11-29 12:54:33.995 UTC [26] PANIC: could not locate a valid checkpoint record
2024-11-29 12:54:34.214 UTC [1] LOG: startup process (PID 26) was terminated by signal 6: Aborted
2024-11-29 12:54:34.214 UTC [1] LOG: aborting startup due to startup process failure
2024-11-29 12:54:34.223 UTC [1] LOG: database system is shut down

相关恢复方法

BASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 因为 postgresql 是安装在 Docker 中的,从系统中修复,并没有安装相关数据库命令
# 所以先安装相关命令
# 基于 Debian/Ubuntu 的系统
sudo apt install postgresql postgresql-contrib
# 基于 CentOS/RHEL/Fedora 的系统
sudo dnf install postgresql postgresql-server
# 基于 Arch 的系统
sudo pacman -S postgresql
# 然后使用 pg_resetwal 进行修复
# 这个时候需要使用 postgres 用户,也就是能够操作数据库文件的用户
# 然后使用如下命令进行修复
# 如果用户不对可以换个别的用户,数据库文件的路径不对,也可以换一下文件路径
sudo -u postgres pg_resetwal -D /var/lib/postgresql/data
# 如果无法直接运行,可以切换到 postgres 的账户下再运行
sudo -i -u postgres
pg_resetwal -D /var/lib/postgresql/data

pg_resetwal 的一些解释
这个命令用于重置 PostgreSQLWrite-Ahead Log (WAL) 目录。
WALPostgreSQL 用于确保数据完整性和可靠性的一个重要机制。它记录了所有数据库的修改操作,以便在发生故障时能够进行恢复。
使用 pg_resetwal 命令可以执行以下操作:

  1. 清空 WAL 目录中的所有文件。这可以用于在数据库出现问题时,手动重置 WAL 以便重新启动数据库。
  2. 重置 WAL 的元数据信息,如日志序列号、检查点位置等。这有助于修复 WAL 相关的问题,比如在数据库崩溃或者恢复过程中出现的错误。

通常情况下,您只需要在数据库出现严重问题,无法正常启动时使用这个命令。它可以帮助您强制重置 WAL 目录,从而允许数据库重新启动。
但请注意,使用 pg_resetwal 命令会丢失最近的一些事务日志,因此可能会导致数据丢失。因此,在使用这个命令之前,请务必先备份您的数据库。

 评论