PostgreSQL时间线(timeline)和History File

说明:

在pg中,当我们进行了基于时间点的还原(PITR)后,数据库会启用新的时间线并继续进行操作。
但是,当我们进行基于时间点的还原后如果发现又出现错误,想要继续还原数据库该如何操作呢?如何还原到原先旧的时间线呢?
我们可以使用recovery_target_timeline参数来指定数据库还原到某一个时间线上。如果你还不清楚这个参数该如何使用,或者说压根不知道时间线是啥,那么请继续往下看。

PostgreSQL 时间线:

每当我们在数据库中完成一个事务时,所做的操作都会记录到$PGDATA/pg_wal目录下的wal日志文件中。

wal日志文件一般都是下面这种格式:
000000010000000000000001

当一个wal日志被写满后,便会创建新的wal日志000000010000000000000002,以此类推。

该文件中前8位,即:00000001表示的便是数据库的时间线。

从控制文件中也可以看到:
-bash-4.1$-> pg_controldata |grep TimeLineID
Latest checkpoint’s TimeLineID: 1
Latest checkpoint’s PrevTimeLineID: 1

每当我们进行基于时间点的还原后,时间线便会加1,并创建一个名为NewTimelineID.history的新文件。这个文件是干什么用的我们后面会介绍。

recovery_target_timeline是一个参数,它可以帮助我们将集群带入历史记录中的任何时间线,只要有效的基本备份和所有存档日志都到位。

我们来看看下面的例子:
首先,重新初始化一个新的数据库集群。

-bash-4.1$-> ls pg_wal
000000010000000000000001 archive_status

然后创建一张表并插入数据。

扫描二维码关注公众号,回复: 12653181 查看本文章
bill=# create table timeline(tid int, remarks varchar(1000));
CREATE TABLE
bill=# insert into timeline values('1','This is timeline id 1');
INSERT 0 1
bill=# checkpoint;
CHECKPOINT
bill=# select pg_switch_wal();
pg_switch_wal
---------------
0/15D4B70
(1 row)

刚刚插入的数据便记录在000000010000000000000001的wal日志中。

当wal日志写到000000010000000000000005时,进行一次完整的备份,接着再产生一些新的wal日志。
-bash-4.1$ ls -rlt
total 147460
-rw------- 1 postgres postgres 16777216 Nov 22 13:03 000000010000000000000001
-rw------- 1 postgres postgres 16777216 Nov 22 13:03 000000010000000000000002
-rw------- 1 postgres postgres 16777216 Nov 22 13:03 000000010000000000000003
-rw------- 1 postgres postgres 16777216 Nov 22 13:05 000000010000000000000004
-rw------- 1 postgres postgres 16777216 Nov 22 13:05 000000010000000000000005
-rw------- 1 postgres postgres 337 Nov 22 13:05 000000010000000000000005.00000028.backup
-rw------- 1 postgres postgres 16777216 Nov 22 13:06 000000010000000000000006
-rw------- 1 postgres postgres 16777216 Nov 22 13:06 000000010000000000000007

可以看到,现在最新的wal日志是000000010000000000000008

接着插入一条新的数据。

bill=# insert into timeline values('1','This is timeline id 1 after basebackup');
INSERT 0 1
bill=# checkpoint;
CHECKPOINT

-bash-4.1$ pg_waldump 000000010000000000000008 | grep INSERT
rmgr: Heap len (rec/tot): 54/ 214, tx:
487, lsn: 0/08000110, prev 0/080000D8, desc: INSERT off 2 flags 0x00,
blkref #0: rel 1663/13530/16384 blk 0 FPW

然后再产生几个wal日志,现在的情况如下:
-bash-4.1$ ls -rlt
total 311308
-rw------- 1 16777216 Nov 22 13:03 000000010000000000000001
-rw------- 1 16777216 Nov 22 13:03 000000010000000000000002
-rw------- 1 16777216 Nov 22 13:03 000000010000000000000003
-rw------- 1 16777216 Nov 22 13:05 000000010000000000000004
-rw------- 1 16777216 Nov 22 13:05 000000010000000000000005
-rw------- 1 337 Nov 22 13:05 000000010000000000000005.00000028.backup
-rw------- 1 16777216 Nov 22 13:06 000000010000000000000006
-rw------- 1 16777216 Nov 22 13:06 000000010000000000000007
-rw------- 1 16777216 Nov 22 13:07 000000010000000000000008
-rw------- 1 16777216 Nov 22 13:07 000000010000000000000009
-rw------- 1 16777216 Nov 22 13:09 00000001000000000000000A

如下图所示:
在这里插入图片描述

此时,在我插入第二条数据前,我想要把数据还原到000000010000000000000007这个点。

因此我在postgresql.conf文件中将恢复目标lsn设置为“ 0/07000060”。

接着进行还原,当我们还原之后,数据库切换到了新的时间线。
除此之外还有哪些改变呢?

  • 恢复结束是指数据库打开进行写入的点。
  • 创建了新的时间线的 history file文件,如00000002.history。
  • 前一个时间线上的部分WAL文件已被新时间线的ID复制。
  • 检查点记录写在新的时间线上。

日志中会记录下列信息:

LOG:  starting point-in-time recovery to WAL location (LSN) "0/7000060"
LOG:  restored log file "000000010000000000000005" from archive
LOG:  redo starts at 0/5000028
LOG:  consistent recovery state reached at 0/5000138
LOG:  database system is ready to accept read only connections
LOG:  restored log file "000000010000000000000006" from archive
LOG:  restored log file "000000010000000000000007" from archive
LOG:  recovery stopping after WAL location (LSN) "0/7000060"
LOG:  pausing at the end of recovery
HINT:  Execute pg_wal_replay_resume() to promote.

此时,PostgreSQL已在wal日志7处分支到新的时间线,并开始创建时间线ID为2的新wal日志。我们可以下wal日志目录下看到00000002.history文件。
该文件是可读文件,内容大致为:

1<parentTLI>    0/70000D8 <switchpoint>     after LSN 0/7000060<reason>

parentTLI                  ID of the parent timeline
switchpoint                XLogRecPtr of the WAL location where the switch happened
reason                       human-readable explanation of why the timeline was changed

接下来,我向wal日志00000002000000000000000A (0/A000060)中插入新的数据。

bill=# insert into timeline values('2','This is timeline id 2 correct');
INSERT 0 1

以及另一个wal日志00000002000000000000000D(0/D000000)中插入另一条数据。

bill=# insert into timeline values('2','This is timeline id 2 wrong at 0/D000000');
INSERT 0 1

在这里插入图片描述

这个时候,我在00000002000000000000000D的wal日志中执行了错误的操作,想要回退到时间线2的00000002000000000000000C处,那么我要如何操作呢,如果像前面一样只指定lsn那么怎么保证不会回退到时间线1中呢?

这个时候我们便可以通过指定recovery_target_timeline来实现。
在postgresql.conf文件中添加:

recovery_target_timeline = '2'
recovery_target_lsn = '0/0C000060'

接着,启动数据库,可以看到日志中:

LOG:  database system was interrupted; last known up at 2020-11-22 13:05:01 IST
LOG:  restored log file "<span style="color: rgb(255, 0, 0);" data-mce-style="color: #ff0000;">00000002.history</span>" from archive
cp: cannot stat `/u02/archivelogs/00000003.history': No such file or directory
LOG:  starting point-in-time recovery to WAL location (LSN) "0/C000060"
LOG:  restored log file "00000002.history" from archive
LOG:  restored log file "<span style="color: rgb(255, 0, 0);" data-mce-style="color: #ff0000;">000000010000000000000005</span>" from archive
LOG:  redo starts at 0/5000028
LOG:  consistent recovery state reached at 0/5000138
LOG:  database system is ready to accept read only connections
LOG:  restored log file "000000010000000000000006" from archive
LOG:  restored log file "000000020000000000000007" from archive
LOG:  restored log file "000000020000000000000008" from archive
LOG:  restored log file "000000020000000000000009" from archive
LOG:  restored log file "00000002000000000000000A" from archive
LOG:  restored log file "00000002000000000000000B" from archive
LOG:  restored log file "<span style="color: rgb(255, 0, 0);" data-mce-style="color: #ff0000;">00000002000000000000000C</span>" from archive
LOG:  recovery stopping after WAL location (LSN) "<span style="color: rgb(255, 0, 0);" data-mce-style="color: #ff0000;">0/C000060</span>"
LOG:  pausing at the end of recovery
HINT:  Execute pg_wal_replay_resume() to promote.
..
LOG:  redo done at 0/C000060
LOG:  last completed transaction was at log time 2020-11-22 13:15:29.696929+05:30

然后查询该表验证:

bill=# select * from timeline;
 tid |            remarks
-----+-------------------------------
   1 | This is timeline id 1
   2 | This is timeline id 2 correct
(2 rows)

在这里插入图片描述
此时可以看到新建了00000003.history文件,该文件内容如下:

-bash-4.1$ cat 00000003.history
1       0/70000D8       after LSN 0/7000060
 
 
2       0/C0000D8       after LSN 0/C000060

我们不难发现:
history file这个文件中记录的就是这个时间线是从哪个WAL位置开始生成的。

参考链接:
http://www.postgres.cn/docs/12/runtime-config-wal.html
PostgreSQL在线增量备份与PITR还原

猜你喜欢

转载自blog.csdn.net/weixin_39540651/article/details/111239341
今日推荐