基于事务的恢复
备份 ,先做一个备份 ,然后进行DML操作。使用备份+WAL来进行还原到某一个DML时刻。
-- 对数据库做一个备份

pg_basebackup -D /postgres/pg15/backups/ -Ft -Pv -U postgres -h 192.168.2.101 -p5432 -Rpostgres@pg15:/postgres/pg15/backups$ pg_basebackup -D /postgres/pg15/backups/ -Ft -Pv -U postgres -h 192.168.2.101 -p5432 -R
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/51000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_190536"
80913/80913 kB (100%), 2/2 tablespaces
pg_basebackup: write-ahead log end point: 0/51000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: renaming backup_manifest.tmp to backup_manifest
pg_basebackup: base backup completed
postgres@pg15:/postgres/pg15/backups$ 

创建测试用的表
-- 创建表t2

create table t2 (id int,time date);
insert into t2 values(1,now());
drop table t2;test=# create table t2 (id int,time date);
CREATE TABLE
test=#
test=# insert into t2 values(1,now());
INSERT 0 1
test=# test=# drop table t2;
DROP TABLE
test=#

-- 创建表t2

create table t2 (id int,time timestamp);
insert into t2 values(1,now());
insert into t2 values(1,now());test=# select * from t2;id |            time
----+----------------------------1 | 2023-05-05 10:42:49.5502781 | 2023-05-05 10:43:45.365331
(2 rows)test=# 

--做一个checkpoint

test=# checkpoint;
CHECKPOINT
test=# 

-- 查看wal dump。 看到t2 drop的时候,drop的时候 事务是866 恢复到事务 865 (可以看到在事务 866 的时候,drop掉了表)。
可以看到rels: pg_tblspc/16389/PG_15_202209061/16388/24717 。通过这些信息,可以使用oid2name来查找到对应的对象 。

rmgr: Transaction len (rec/tot):    437/   437, tx:        864, lsn: 0/52018D88, prev 0/52018D58, desc: COMMIT 2023-05-05 10:42:07.978885 CST; inval msgs: catcache 80 catcache 79 catcache 80 catcache 79 catcache 55 catcache 54 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 snapshot 2608 relcache 24717
rmgr: Standby     len (rec/tot):     50/    50, tx:          0, lsn: 0/52018F40, prev 0/52018D88, desc: RUNNING_XACTS nextXid 865 latestCompletedXid 864 oldestRunningXid 865
rmgr: Heap        len (rec/tot):     63/    63, tx:        865, lsn: 0/52018F78, prev 0/52018F40, desc: INSERT+INIT off 1 flags 0x00, blkref #0: rel 16389/16388/24717 blk 0
rmgr: Transaction len (rec/tot):     34/    34, tx:        865, lsn: 0/52018FB8, prev 0/52018F78, desc: COMMIT 2023-05-05 10:42:16.677862 CST
rmgr: Standby     len (rec/tot):     42/    42, tx:        866, lsn: 0/52018FE0, prev 0/52018FB8, desc: LOCK xid 866 db 16388 rel 24717
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019010, prev 0/52018FE0, desc: DELETE off 39 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1247 blk 14
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019048, prev 0/52019010, desc: DELETE off 16 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/2608 blk 14
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019080, prev 0/52019048, desc: DELETE off 38 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1247 blk 14
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/520190B8, prev 0/52019080, desc: DELETE off 15 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/2608 blk 14
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/520190F0, prev 0/520190B8, desc: DELETE off 71 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019128, prev 0/520190F0, desc: DELETE off 70 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019160, prev 0/52019128, desc: DELETE off 69 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019198, prev 0/52019160, desc: DELETE off 68 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/520191D0, prev 0/52019198, desc: DELETE off 67 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019208, prev 0/520191D0, desc: DELETE off 66 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019240, prev 0/52019208, desc: DELETE off 64 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/52019278, prev 0/52019240, desc: DELETE off 65 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1249 blk 58
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/520192B0, prev 0/52019278, desc: DELETE off 36 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/1259 blk 5
rmgr: Heap        len (rec/tot):     54/    54, tx:        866, lsn: 0/520192E8, prev 0/520192B0, desc: DELETE off 17 flags 0x00 KEYS_UPDATED , blkref #0: rel 16389/16388/2608 blk 14
rmgr: Transaction len (rec/tot):    501/   501, tx:        866, lsn: 0/52019320, prev 0/520192E8, desc: COMMIT 2023-05-05 10:42:22.887717 CST; rels: pg_tblspc/16389/PG_15_202209061/16388/24717; dropped stats: 2/16388/24717; inval msgs: catcache 80 catcache 79 catcache 80 catcache 79 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 55 catcache 54 snapshot 2608 snapshot 2608 relcache 24717 snapshot 2608

在t2插入数据的时候,可以看到插入了两条,恢复到插入到第一条后的状态,也就是事务867。第二条插入的事务是868.

rmgr: Btree       len (rec/tot):     64/    64, tx:        867, lsn: 0/5201C270, prev 0/5201C228, desc: INSERT_LEAF off 316, blkref #0: rel 16389/16388/2659 blk 10
rmgr: Heap2       len (rec/tot):     85/    85, tx:        867, lsn: 0/5201C2B0, prev 0/5201C270, desc: MULTI_INSERT 1 tuples flags 0x02, blkref #0: rel 16389/16388/2608 blk 14                         <<<<<<<<<<<<<<<<<<<<<<< 插入的第一条记录
rmgr: Btree       len (rec/tot):     72/    72, tx:        867, lsn: 0/5201C308, prev 0/5201C2B0, desc: INSERT_LEAF off 151, blkref #0: rel 16389/16388/2673 blk 9
rmgr: Btree       len (rec/tot):     72/    72, tx:        867, lsn: 0/5201C350, prev 0/5201C308, desc: INSERT_LEAF off 195, blkref #0: rel 16389/16388/2674 blk 7
rmgr: Standby     len (rec/tot):     42/    42, tx:        867, lsn: 0/5201C398, prev 0/5201C350, desc: LOCK xid 867 db 16388 rel 24720
rmgr: Transaction len (rec/tot):    437/   437, tx:        867, lsn: 0/5201C3C8, prev 0/5201C398, desc: COMMIT 2023-05-05 10:42:44.346425 CST; inval msgs: catcache 80 catcache 79 catcache 80 catcache 79 catcache 55 catcache 54 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 snapshot 2608 relcache 24720
rmgr: Heap        len (rec/tot):     71/    71, tx:        868, lsn: 0/5201C580, prev 0/5201C3C8, desc: INSERT+INIT off 1 flags 0x00, blkref #0: rel 16389/16388/24720 blk 0                             <<<<<<<<<<<<<<<<<<<<<<<<<插入的第二条记录
rmgr: Transaction len (rec/tot):     34/    34, tx:        868, lsn: 0/5201C5C8, prev 0/5201C580, desc: COMMIT 2023-05-05 10:42:49.560245 CST
rmgr: Standby     len (rec/tot):     50/    50, tx:          0, lsn: 0/5201C5F0, prev 0/5201C5C8, desc: RUNNING_XACTS nextXid 869 latestCompletedXid 868 oldestRunningXid 869
rmgr: Heap        len (rec/tot):     71/    71, tx:        869, lsn: 0/5201C628, prev 0/5201C5F0, desc: INSERT off 2 flags 0x00, blkref #0: rel 16389/16388/24720 blk 0
rmgr: Transaction len (rec/tot):     34/    34, tx:        869, lsn: 0/5201C670, prev 0/5201C628, desc: COMMIT 2023-05-05 10:43:45.365542 CST
rmgr: Standby     len (rec/tot):     50/    50, tx:          0, lsn: 0/5201C698, prev 0/5201C670, desc: RUNNING_XACTS nextXid 870 latestCompletedXid 869 oldestRunningXid 870
postgres@pg15:/postgres/pg15/data/pg_wal$ 

--通过oid2name查看表空间,数据库,表的oid。这些oid和上面pg_waldump中的内容吻合。(除过第一次删除的表t2,因为第一次删除的表t2已经不存在了,新建立的表t2)

postgres@pg15:/postgres/pg15/data/pg_wal$ oid2name -s
All tablespaces:Oid  Tablespace Name
------------------------1663       pg_default1664        pg_global16389         test_tbs                          <<<<<<<<<<<<<<<<<<<   表空间oid  16389
postgres@pg15:/postgres/pg15/data/pg_wal$
postgres@pg15:/postgres/pg15/data/pg_wal$ oid2name
All databases:Oid  Database Name  Tablespace
----------------------------------5       postgres  pg_default24653      postgres1  pg_default24675      postgres2  pg_default4      template0  pg_default1      template1  pg_default16388           test    test_tbs               <<<<<<<<<<<<<<<<<<<<<   数据库oid 16388
postgres@pg15:/postgres/pg15/data/pg_wal$
postgres@pg15:/postgres/pg15/data/pg_wal$ oid2name -d test -t t2
From database "test":Filenode  Table Name
----------------------24720          t2                           <<<<<<<<<<<<<<<<<<<<  表t2的oid  24720
postgres@pg15:/postgres/pg15/data/pg_wal$

-- 删除掉data目录 进行恢复,恢复到表t2被drop的状态

-- 将backups里面的备份,拷贝到合适的位置(原来的位置,其实这三个文件的位置,还是在DATA下),进行解压

postgres@pg15:/postgres/pg15/backups$ ls
16389.tar  backup_manifest  base.tar  pg_wal.tar
postgres@pg15:/postgres/pg15/backups$

--修改postgresql.auto.conf 和standby.signal 。因为不是使用备库,所以吧standby.signal改为recovery.signal

vi postgresql.auto.conf
restore_command = 'cp /postgres/pg15/archive_wals/%f    %p'
recovery_target_xid='865'mv standby.signal recovery.signal 

-- 启动库

pg_ctl start
postgres@pg15:/postgres/pg15/data$ pg_ctl start
waiting for server to start....2023-05-05 10:55:29.778 CST [190844] LOG:  redirecting log output to logging collector process
2023-05-05 10:55:29.778 CST [190844] HINT:  Future log output will appear in directory "log".done
server started
postgres@pg15:/postgres/pg15/data$ 

-- 验证数据

test=# \c test
You are now connected to database "test" as user "postgres".
test=#
test=#
test=# select * from t2;id |    time
----+------------1 | 2023-05-05
(1 row)test=# 

-- 再次恢复 ,恢复到第一次插入数据的状态,867 ,还原拿出来没有数据,只有表.

restore_command = 'cp /postgres/pg15/archive_wals/%f    %p'
recovery_target_xid='867'postgres@pg15:/postgres/pg15/data$ pg_ctl start
waiting for server to start....2023-05-05 11:13:32.695 CST [191174] LOG:  redirecting log output to logging collector process
2023-05-05 11:13:32.695 CST [191174] HINT:  Future log output will appear in directory "log".done
server started
postgres@pg15:/postgres/pg15/data$ test=# select * from t2;id | time
----+------
(0 rows)test=# 

-- 还原到868

restore_command = 'cp /postgres/pg15/archive_wals/%f    %p'
recovery_target_xid='868'postgres@pg15:/postgres/pg15/data$ pg_ctl start
waiting for server to start....2023-05-05 11:18:36.021 CST [191285] LOG:  redirecting log output to logging collector process
2023-05-05 11:18:36.021 CST [191285] HINT:  Future log output will appear in directory "log".done
server started
postgres@pg15:/postgres/pg15/data$test=# \c test
You are now connected to database "test" as user "postgres".
test=# select * from t2;id |            time
----+----------------------------1 | 2023-05-05 10:42:49.550278
(1 row)test=# 

http://www.postgres.cn/docs/14/runtime-config-wal.html
恢复目标:
默认情况下,恢复将会一直恢复到 WAL 日志的末尾。下面的参数可以被用来指定一个更早的停止点。 在recovery_target、recovery_target_lsn、recovery_target_name、recovery_target_time和 recovery_target_xid 中, 最多只能使用一个,如果在配置文件中使用了多个,将会产生一个错误。这个参数只能在服务器启动时设置。

END。

postgres不完全恢复相关推荐

  1. postgres默认安装后有哪些表_Greenplum 行存、列存,堆表、AO表的原理和选择

    行存和列存的原理 什么时候选择行存 什么时候选择列存 堆表和AO表的原理 什么时候选择堆表 什么时候选择AO表 测试对比行存deform和列存的性能差别 如何查看表的存储结构 Greenplum支持行 ...

  2. 人工手动冷备不完全恢复介绍(purge表不完全恢复)

    不完全恢复 不完全恢复的基本类型: 1)基于时间点 (until time): 使整个数据库恢复到过去的一个时间点前 2)基于scn (until change): 使整个数据库恢复到过去的某个SCN ...

  3. postgres语法_SQL Create Table解释了MySQL和Postgres的语法示例

    postgres语法 A table is a group of data stored in a database. 表是存储在数据库中的一组数据. To create a table in a d ...

  4. linux安装配置postgres及使用dblink

    好久不写东西,一直在看些开源的东西,下面贴下linux上安装配置postgres及使用dblink的操作参考,以供读者和自己今后参考: 1.下载源码:postgresql-9.3.2.tar.gz 2 ...

  5. postgres 入门

    为什么80%的码农都做不了架构师?>>>    安装 BASH 命令行 一.psql 命令行 登录 postgres库 su - postgres # 切换用户,同时切换了 bash ...

  6. postgres 物理格式

    2019独角兽企业重金招聘Python工程师标准>>> Introduction to PostgreSQL physical storage November 9, 2015 I ...

  7. Postgres多版本控制

    高并发控制肯定是数据必须达到的一个标准, 在并发操作中,对于同一个数据,同时读和写的两个回话有可能产生不一致,所以出现了在高并发情况下如何保持性能又保持一致出现了MVCC,多版本并发 实现MVCC的方 ...

  8. 用备份控制文件做不完全恢复下的完全恢复(数据文件备份旧--新建表空间--控制文件备份次新--日志归档文件新)...

    为什么会使用备份的控制文件? 实际工作中主要有两种情况: 第一种:当前控制文件全部损坏,而数据文件备份,控制文件备份及当前日志处于不同SCN版本,它们之间又增加过表空间(数据文件). 第二种:当前控制 ...

  9. 在Postgres中为插入语句生成UUID?

    本文翻译自:Generating a UUID in Postgres for Insert statement? My question is rather simple. 我的问题很简单. I'm ...

最新文章

  1. 仿小米简约Calculator
  2. MySQL自定义函数(CREATE FUNCTION)
  3. ebs 供应商地点信息_EBS标准的查看供应商地址
  4. linux生产环境下安装anaconda总结
  5. 作者:刘强(1990-),男,中国科学院自动化研究所博士生
  6. Python选修课第二届Turtle绘图大赛
  7. django mysql 修改密码_Django更改sqlite数据库使用mysql数据库 - 李金龙
  8. scvmm管理hyper-v克隆主机(二)
  9. java nio netty_Netty精粹之JAVA NIO开发需要知道的
  10. 【笔记】Python算法教程(1)
  11. win10安装虚拟机vm遇到的坑
  12. 风控建模基础笔记(一)
  13. 动词ing形式的5种用法_动词ing形式的用法及变化规则
  14. linux 网站图片无法加载失败怎么办,网页无法加载图片怎么办?解决网页图片无法显示的方法...
  15. 070-JAVA项目实训:仿QQ即时通讯软件系列讲座五(讲解用户注册功能)
  16. heaps 和 priority queue堆和优先队列的定义和数据结构表示
  17. 基于springboot小型车队管理系统 毕业设计-附源码061709
  18. 经典日内策略:ORB突破策略(期货)
  19. 基于PHP音乐网站平台系统设计与实现 开题报告
  20. 网络编程二-LINUX网络IO模型

热门文章

  1. 2022业界广告推荐技术最新进展与趋势:阿里、腾讯、字节、微软
  2. c语言换装游戏源代码,C语言游戏源代码
  3. CPU, GPU, TPU, NPU, DPU介绍
  4. 程序员的十个等级 你属于哪个等级
  5. mvn -v 不是内部或外部命令
  6. 故障模型哪家强?PDR 模型来帮忙
  7. 赵一新:多元数据解构城市和交通发展规律
  8. pe系统作为共享服务器,如何制作服务器winpe操作系统(制作带raid卡驱动的winpe系统)...
  9. office2003无序列号安装
  10. 整车控制单元(VCU)