postgresql/lightdb數據文件丟失和損壞恢復--系統表

文件刪除

rm -rf 1255  # pg_proc

select * from pg_class [order by oid];    ##不報錯

select * from pg_class where oid = 1255; ## 報錯

ERROR: could not open file "base/16397/1255": No such file or directory

zjh@test=# \d
ERROR: could not open file "base/16397/1255": No such file or directory

touch空文件

[zjh@hs-10-20-30-193 16397]$ ll | grep 1255
-rw------- 1 zjh zjh 24576 Aug 6 15:36 1255_fsm
-rw------- 1 zjh zjh 8192 Aug 6 15:36 1255_vm
[zjh@hs-10-20-30-193 16397]$ touch 1255
[zjh@hs-10-20-30-193 16397]$ ll | grep 1255
-rw-rw-r-- 1 zjh zjh 0 Sep 13 11:43 1255
-rw------- 1 zjh zjh 24576 Aug 6 15:36 1255_fsm
-rw------- 1 zjh zjh 8192 Aug 6 15:36 1255_vm

zjh@test=#      
select * from pg_class where oid=1255;
ERROR:  could not read block 3 in file "base/16397/1255": read only 0 of 8192 bytes
zjh@test=# \d
ERROR:  could not read block 3 in file "base/16397/1255": read only 0 of 8192 bytes
zjh@test=# select * from pg_proc;   -- 因爲系統表通過BKI初始化,所以定義存在
 oid | proname | pronamespace | proowner | prolang | procost | prorows | provariadic | prosupport | prokind | prosecdef | proleakproof | proisstrict | proretset | provolatile | proparallel | pronargs | pronargdefa
ults | prorettype | proaccess | proargtypes | proallargtypes | proargmodes | proargnames | proargdefaults | protrftypes | prosrc | probin | proconfig | proacl 
-----+---------+--------------+----------+---------+---------+---------+-------------+------------+---------+-----------+--------------+-------------+-----------+-------------+-------------+----------+------------
-----+------------+-----------+-------------+----------------+-------------+-------------+----------------+-------------+--------+--------+-----------+--------
(0 rows)

文件內容損壞(模擬裂頁 partial-write)

  

zjh@test=# select * from pg_proc limit 247;
ERROR:  cache lookup failed for type 0
zjh@test=# select * from pg_proc limit 246;
 oid  |       proname       | pronamespace | proowner | prolang | procost | prorows | provariadic |     prosupport      | prokind | prosecdef | proleakproof | proisstrict | proretset | provolatile | proparallel | 
pronargs | pronargdefaults | prorettype | proaccess |     proargtypes      | proallargtypes | proargmodes | proargnames | proargdefaults | protrftypes |       prosrc        | probin | proconfig | proacl 
------+---------------------+--------------+----------+---------+---------+---------+-------------+---------------------+---------+-----------+--------------+-------------+-----------+-------------+-------------+-
---------+-----------------+------------+-----------+----------------------+----------------+-------------+-------------+----------------+-------------+---------------------+--------+-----------+--------
 1242 | boolin              |           11 |       10 |      12 |       1 |       0 |           0 | -                   | f       | f         | f            | t           | f         | i           | s           | 
       1 |               0 |         16 | n         | 2275                 |                |             |             |                |             | boolin              |        |           | 
 1243 | boolout             |           11 |       10 |      12 |       1 |       0 |           0 | -                   | f       | f         | f            | t           | f         | i           | s           | 
       1 |               0 |       2275 | n         | 16                   |                |             |             |                |             | boolout             |        |           | 
 (5,40) |  293 | float8eq            |           11 |       10 |      12 |       1 |       0 |           0 | -                   | f       | f         | t            | t           | f         | i           | s    
       |        2 |               0 |         16 | n         | 701 701              |                |             |             |                |             | float8eq            |        |           | 
 (5,41) |  294 | float8ne            |           11 |       10 |      12 |       1 |       0 |           0 | -                   | f       | f         | t            | t           | f         | i           | s    
       |        2 |               0 |         16 | n         | 701 701              |                |             |             |                |             | float8ne            |        |           | 
 (6,3)  |  297 | float8gt            |           11 |       10 |      12 |       1 |       0 |           0 | -                   | f       | f         | t            | t           | f         | i           | s    
       |        2 |               0 |         16 | n         | 701 701              |                |             |             |                |             | float8gt            |        |           | 
 (6,4)  |  298 | float8ge            |           11 |       10 |      12 |       1 |       0 |           0 | -                   | f       | f         | t            | t           | f         | i           | s    
       |        2 |               0 |         16 | n         | 701 701              |                |             |             |                |             | float8ge            |        |           | 
(248 rows)

zjh@test=# select ctid,a.* from pg_proc a where ctid='(6,2)' limit 248;
ERROR:  cache lookup failed for type 0

  然後根據ctid過濾掉損壞的記錄,然後lt_dump/lt_restore重建庫即可。

  zjh@test=# select ctid,a.* from pg_proc a where ctid!='(6,2)';

  對於系統表,一般都是通過DDL導入的,所以可以考慮開啓log_statement = 'ddl'記錄所有執行過的DDL。這樣可以儘量減少DDL丟失。當然,如果對庫的依賴較少,可以從別的庫把數據拷貝回來。如下:

[zjh@hs-10-20-30-193 16397]$ ll | grep 1255
-rw-rw-r-- 1 zjh zjh 679936 Sep 13 11:58 1255
-rw------- 1 zjh zjh  24576 Aug  6 15:36 1255_fsm
-rw------- 1 zjh zjh   8192 Aug  6 15:36 1255_vm
[zjh@hs-10-20-30-193 16397]$ cd ../
1/         13591/     13592/     16397/     ltsql_tmp/ 
[zjh@hs-10-20-30-193 16397]$ cd ../13592/
[zjh@hs-10-20-30-193 13592]$ ll | grep 1255
-rw------- 1 zjh zjh     704512 Sep 11 00:13 1255
-rw------- 1 zjh zjh      24576 Sep 10 22:43 1255_fsm
-rw------- 1 zjh zjh       8192 Sep 10 17:46 1255_vm
[zjh@hs-10-20-30-193 13592]$ cd ../13591/
[zjh@hs-10-20-30-193 13591]$ ll | grep 1255
-rw------- 1 zjh zjh 679936 Aug  6 15:36 1255
-rw------- 1 zjh zjh  24576 Aug  6 15:36 1255_fsm
-rw------- 1 zjh zjh   8192 Aug  6 15:36 1255_vm

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章