合 PG之闪回数据库
误操作恢复的文章参考:
- PG中通过pg_waldump来分析pg_wal日志:https://www.dbaup.com/pgzhongtongguopg_waldumplaifenxipg_walrizhi.html
- PG日志挖掘工具之WalMiner:https://www.dbaup.com/pgrizhiwajuegongjuzhiwalminer.html
- PG工具pg_resetwal介绍:https://www.dbaup.com/postgresql11xiugaiwal-segsizedaxiao.html
在Oracle中,若发生重大的误操作,那么我们可以使用flashback database
命令来把数据库整体闪回到过去的误操作的时间点,当然前提是需要打开数据库的闪回功能。
在PG中,能否也可以执行类型的操作呢,答案是肯定的。
闪回数据库过程示例
误操作恢复的过程命令参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | \c lhrdb select pg_switch_wal(); select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn()); create table f_lhr (id int, name varchar(20)); insert into f_lhr (id,name) values (1,'l'); insert into f_lhr (id,name) values (2,'h'); insert into f_lhr (id,name) values (3,'r'); -- 误操作 delete from f_lhr where id =2 ; select * from f_lhr; create table f_lhr2 (id int, name varchar(20)); insert into f_lhr (id,name) values (1,'l'); select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn()); pg_waldump 0000000100000002000000B2 | grep DELETE pg_ctl stop pg_resetwal -x 452490 -D /pg13/pgdata/ pg_ctl start |
1、误操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | C:\Users\lhrxxt>psql -U postgres -h192.168.1.35 -p15433 Password for user postgres: psql (13.3) Type "help" for help. postgres=# select now(); now ------------------------------- 2021-09-28 08:48:11.150389+08 (1 row) postgres=# \c lhrdb You are now connected to database "lhrdb" as user "postgres". lhrdb=# lhrdb=# lhrdb=# select pg_switch_wal(); pg_switch_wal --------------- 2/B100C498 (1 row) lhrdb=# select pg_current_wal_lsn(),pg_walfile_name(pg_current_wal_lsn()),pg_walfile_name_offset(pg_current_wal_lsn()); pg_current_wal_lsn | pg_walfile_name | pg_walfile_name_offset --------------------+--------------------------+------------------------------- 2/B2000060 | 0000000100000002000000B2 | (0000000100000002000000B2,96) (1 row) lhrdb=# create table f_lhr (id int, name varchar(20)); CREATE TABLE lhrdb=# insert into f_lhr (id,name) values (1,'l'); INSERT 0 1 lhrdb=# insert into f_lhr (id,name) values (2,'h'); INSERT 0 1 lhrdb=# insert into f_lhr (id,name) values (3,'r'); INSERT 0 1 lhrdb=# -- 误操作 lhrdb=# delete from f_lhr where id =2 ; DELETE 1 lhrdb=# select * from f_lhr; id | name ----+------ 1 | l 3 | r (2 rows) lhrdb=# create table f_lhr2 (id int, name varchar(20)); CREATE TABLE lhrdb=# insert into f_lhr2 (id,name) values (1,'l'); INSERT 0 1 |
2、查找误操作的事务号
首先可以根据时间来判断大致是哪几个WAL日志文件,再进行逐个日志分析查找,本次实验的WAL日志为“0000000100000002000000B2”,根据得到当前的事务号以及日志文件,开始寻找恢复日志的数据范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | [pg13@lhrpg pg_wal]$ pg_waldump 0000000100000002000000B2 rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 2/B2000028, prev 2/B100C480, desc: RUNNING_XACTS nextXid 452486 latestCompletedXid 452485 oldestRunningXid 452486 rmgr: Storage len (rec/tot): 42/ 42, tx: 0, lsn: 2/B2000060, prev 2/B2000028, desc: CREATE base/16484/41318 rmgr: Heap len (rec/tot): 54/ 1878, tx: 452486, lsn: 2/B2000090, prev 2/B2000060, desc: INSERT off 10 flags 0x00, blkref #0: rel 1663/16484/1247 blk 10 FPW rmgr: Btree len (rec/tot): 53/ 2153, tx: 452486, lsn: 2/B20007E8, prev 2/B2000090, desc: INSERT_LEAF off 103, blkref #0: rel 1663/16484/2703 blk 2 FPW rmgr: Btree len (rec/tot): 53/ 6845, tx: 452486, lsn: 2/B2001058, prev 2/B20007E8, desc: INSERT_LEAF off 161, blkref #0: rel 1663/16484/2704 blk 1 FPW rmgr: Heap len (rec/tot): 54/ 2358, tx: 452486, lsn: 2/B2002B30, prev 2/B2001058, desc: INSERT off 38 flags 0x00, blkref #0: rel 1663/16484/2608 blk 58 FPW rmgr: Btree len (rec/tot): 53/ 6217, tx: 452486, lsn: 2/B2003468, prev 2/B2002B30, desc: INSERT_LEAF off 219, blkref #0: rel 1663/16484/2673 blk 24 FPW rmgr: Btree len (rec/tot): 53/ 6133, tx: 452486, lsn: 2/B2004CD0, prev 2/B2003468, desc: INSERT_LEAF off 216, blkref #0: rel 1663/16484/2674 blk 43 FPW rmgr: Heap len (rec/tot): 207/ 207, tx: 452486, lsn: 2/B20064E0, prev 2/B2004CD0, desc: INSERT off 11 flags 0x00, blkref #0: rel 1663/16484/1247 blk 10 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B20065B0, prev 2/B20064E0, desc: INSERT_LEAF off 103, blkref #0: rel 1663/16484/2703 blk 2 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B20065F0, prev 2/B20065B0, desc: INSERT_LEAF off 22, blkref #0: rel 1663/16484/2704 blk 1 rmgr: Heap len (rec/tot): 80/ 80, tx: 452486, lsn: 2/B2006638, prev 2/B20065F0, desc: INSERT off 39 flags 0x00, blkref #0: rel 1663/16484/2608 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B2006688, prev 2/B2006638, desc: INSERT_LEAF off 219, blkref #0: rel 1663/16484/2673 blk 24 rmgr: Btree len (rec/tot): 53/ 2969, tx: 452486, lsn: 2/B20066D0, prev 2/B2006688, desc: INSERT_LEAF off 103, blkref #0: rel 1663/16484/2674 blk 41 FPW rmgr: Heap len (rec/tot): 54/ 6474, tx: 452486, lsn: 2/B2007270, prev 2/B20066D0, desc: INSERT off 34 flags 0x00, blkref #0: rel 1663/16484/1259 blk 0 FPW rmgr: Btree len (rec/tot): 53/ 3233, tx: 452486, lsn: 2/B2008BD8, prev 2/B2007270, desc: INSERT_LEAF off 157, blkref #0: rel 1663/16484/2662 blk 2 FPW rmgr: Btree len (rec/tot): 53/ 6157, tx: 452486, lsn: 2/B2009880, prev 2/B2008BD8, desc: INSERT_LEAF off 34, blkref #0: rel 1663/16484/2663 blk 1 FPW rmgr: Btree len (rec/tot): 53/ 1833, tx: 452486, lsn: 2/B200B0A8, prev 2/B2009880, desc: INSERT_LEAF off 87, blkref #0: rel 1663/16484/3455 blk 4 FPW rmgr: Heap len (rec/tot): 54/ 966, tx: 452486, lsn: 2/B200B7D8, prev 2/B200B0A8, desc: INSERT off 6 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 FPW rmgr: Btree len (rec/tot): 53/ 2089, tx: 452486, lsn: 2/B200BBA0, prev 2/B200B7D8, desc: INSERT_LEAF off 71, blkref #0: rel 1663/16484/2658 blk 16 FPW rmgr: Btree len (rec/tot): 53/ 5933, tx: 452486, lsn: 2/B200C3E8, prev 2/B200BBA0, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 FPW rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200DB18, prev 2/B200C3E8, desc: INSERT off 7 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200DBC8, prev 2/B200DB18, desc: INSERT_LEAF off 72, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200DC10, prev 2/B200DBC8, desc: INSERT_LEAF off 293, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200DC50, prev 2/B200DC10, desc: INSERT off 8 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200DD00, prev 2/B200DC50, desc: INSERT_LEAF off 71, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200DD48, prev 2/B200DD00, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200DD88, prev 2/B200DD48, desc: INSERT off 9 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200DE38, prev 2/B200DD88, desc: INSERT_LEAF off 74, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200DE80, prev 2/B200DE38, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200DEC0, prev 2/B200DE80, desc: INSERT off 10 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200DF70, prev 2/B200DEC0, desc: INSERT_LEAF off 71, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200DFB8, prev 2/B200DF70, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200DFF8, prev 2/B200DFB8, desc: INSERT off 11 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200E0C0, prev 2/B200DFF8, desc: INSERT_LEAF off 75, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200E108, prev 2/B200E0C0, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200E148, prev 2/B200E108, desc: INSERT off 12 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200E1F8, prev 2/B200E148, desc: INSERT_LEAF off 71, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200E240, prev 2/B200E1F8, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 175/ 175, tx: 452486, lsn: 2/B200E280, prev 2/B200E240, desc: INSERT off 13 flags 0x00, blkref #0: rel 1663/16484/1249 blk 58 rmgr: Btree len (rec/tot): 72/ 72, tx: 452486, lsn: 2/B200E330, prev 2/B200E280, desc: INSERT_LEAF off 76, blkref #0: rel 1663/16484/2658 blk 16 rmgr: Btree len (rec/tot): 64/ 64, tx: 452486, lsn: 2/B200E378, prev 2/B200E330, desc: INSERT_LEAF off 292, blkref #0: rel 1663/16484/2659 blk 10 rmgr: Heap len (rec/tot): 80/ 80, tx: 452486, lsn: 2/B200E3B8, prev 2/B200E378, desc: INSERT off 40 flags 0x00, blkref #0: rel 1663/16484/2608 blk 58 rmgr: Btree len (rec/tot): 53/ 3781, tx: 452486, lsn: 2/B200E408, prev 2/B200E3B8, desc: INSERT_LEAF off 119, blkref #0: rel 1663/16484/2673 blk 33 FPW rmgr: Btree len (rec/tot): 53/ 7813, tx: 452486, lsn: 2/B200F2D0, prev 2/B200E408, desc: INSERT_LEAF off 31, blkref #0: rel 1663/16484/2674 blk 37 FPW rmgr: Standby len (rec/tot): 42/ 42, tx: 452486, lsn: 2/B2011170, prev 2/B200F2D0, desc: LOCK xid 452486 db 16484 rel 41318 rmgr: Transaction len (rec/tot): 437/ 437, tx: 452486, lsn: 2/B20111A0, prev 2/B2011170, desc: COMMIT 2021-09-28 09:00:23.823535 CST; inval msgs: catcache 75 catcache 74 catcache 75 catcache 74 catcache 50 catcache 49 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 41318 rmgr: Heap len (rec/tot): 61/ 61, tx: 452487, lsn: 2/B2011358, prev 2/B20111A0, desc: INSERT+INIT off 1 flags 0x00, blkref #0: rel 1663/16484/41318 blk 0 rmgr: Transaction len (rec/tot): 34/ 34, tx: 452487, lsn: 2/B2011398, prev 2/B2011358, desc: COMMIT 2021-09-28 09:00:23.845814 CST rmgr: Heap len (rec/tot): 61/ 61, tx: 452488, lsn: 2/B20113C0, prev 2/B2011398, desc: INSERT off 2 flags 0x00, blkref #0: rel 1663/16484/41318 blk 0 rmgr: Transaction len (rec/tot): 34/ 34, tx: 452488, lsn: 2/B2011400, prev 2/B20113C0, desc: COMMIT 2021-09-28 09:00:23.851271 CST rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 2/B2011428, prev 2/B2011400, desc: RUNNING_XACTS nextXid 452489 latestCompletedXid 452488 oldestRunningXid 452489 rmgr: Heap len (rec/tot): 61/ 61, tx: 452489, lsn: 2/B2011460, prev 2/B2011428, desc: INSERT off 3 flags 0x00, blkref #0: rel 1663/16484/41318 blk 0 rmgr: Transaction len (rec/tot): 34/ 34, tx: 452489, lsn: 2/B20114A0, prev 2/B2011460, desc: COMMIT 2021-09-28 09:00:25.727906 CST rmgr: Heap len (rec/tot): 54/ 54, tx: 452490, lsn: 2/B20114C8, prev 2/B20114A0, desc: DELETE off 2 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/16484/41318 blk 0 rmgr: Transaction len (rec/tot): 34/ 34, tx: 452490, lsn: 2/B2011500, prev 2/B20114C8, desc: COMMIT 2021-09-28 09:00:36.627123 CST rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 2/B2011528, prev 2/B2011500, desc: RUNNING_XACTS nextXid 452491 latestCompletedXid 452490 oldestRunningXid 452491 rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 2/B2011560, prev 2/B2011528, desc: RUNNING_XACTS nextXid 452491 latestCompletedXid 452490 oldestRunningXid 452491 rmgr: XLOG len (rec/tot): 114/ 114, tx: 0, lsn: 2/B2011598, prev 2/B2011560, desc: CHECKPOINT_ONLINE redo 2/B2011560; tli 1; prev tli 1; fpw true; xid 0:452491; oid 49453; multi 1; offset 0; oldest xid 478 in DB 1; oldest multi 1 in DB 1; oldest/newest commit timestamp xid: 0/0; oldest running xid 452491; online rmgr: Standby len (rec/tot): 50/ 50, tx: 0, lsn: 2/B2011610, prev 2/B2011598, desc: RUNNING_XACTS nextXid 452491 latestCompletedXid 452490 oldestRunningXid 452491 pg_waldump: fatal: error in WAL record at 2/B2011610: invalid record length at 2/B2011648: wanted 24, got 0 [pg13@lhrpg pg_wal]$ [pg13@lhrpg pg_wal]$ pg_waldump 0000000100000002000000B2 | grep DELETE pg_waldump: fatal: error in WAL record at 2/B2011610: invalid record length at 2/B2011648: wanted 24, got 0 rmgr: Heap len (rec/tot): 54/ 54, tx: 452490, lsn: 2/B20114C8, prev 2/B20114A0, desc: DELETE off 2 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/16484/41318 blk 0 [pg13@lhrpg pg_wal]$ |
可以找到本次误操作的事务号为“452490”。
“CREATE base/16484/41318”为新建表的操作,表示数据库oid为16484,表的oid为41318。