PG中恢复系统表案例
2023-06-13 09:12:46 时间
- 恢复系统表案例
- 处理方式
- 具体步骤
- 找出原User OID对应关系
- 创建一张中间表(my_authid)
- 关闭数据库替换pg_authid表对应的物理文件
- 启动数据库
- 具体步骤
- 处理方式
客户误操作将系统表pg_authid表删除,过后自己恢复了所有的User,但是OID是系统生成的已经与原来的不一样,需要修复
处理方式
由于系统表中OID全部都是原User OID与新User OID对不上,如果将系统表对应的OID全部更新为新的User OID工作量比较大,所以选择根据原User OID 重建pg_authid表
具体步骤
找出原User OID对应关系
由于系统目前状况psql中使用\l 或者\d 我们看到的Owner都会是Unknow状态,并且会显示出原User的OID,让客户配合梳理出这些对象对应的用户则可以得出原User OID对应关系:
- 原User OID对应关系
16384 | mintq
24824936 | xiangqd
3373 | pg_monitor
3374 | pg_read_all_settings
3375 | pg_read_all_stats
3377 | pg_stat_scan_tables
4200 | pg_signal_backend
10 | postgres
- 新User OID对应关系
postgres=# select oid , rolname from pg_authid;
oid | rolname
----------+----------------------
54036442 | pg_monitor
54036443 | pg_read_all_settings
54036444 | pg_read_all_stats
54036445 | pg_stat_scan_tables
54036446 | pg_signal_backend
54036447 | mintq
54036448 | rep
54036449 | xiangqd
54036441 | postgres
创建一张中间表(my_authid)
- 首先查看pg_authid表相关信息
postgres=# SELECT pg_relation_filepath('pg_authid');
pg_relation_filepath
----------------------
global/1260
(1 row)
postgres=# \d pg_authid
Table "pg_catalog.pg_authid"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+---------
rolname | name | | not null |
rolsuper | boolean | | not null |
rolinherit | boolean | | not null |
rolcreaterole | boolean | | not null |
rolcreatedb | boolean | | not null |
rolcanlogin | boolean | | not null |
rolreplication | boolean | | not null |
rolbypassrls | boolean | | not null |
rolconnlimit | integer | | not null |
rolpassword | text | | |
rolvaliduntil | timestamp with time zone | | |
Indexes:
"pg_authid_oid_index" UNIQUE, btree (oid), tablespace "pg_global"
"pg_authid_rolname_index" UNIQUE, btree (rolname), tablespace "pg_global"
Tablespace: "pg_global"
postgres=# SELECT pg_relation_filepath('pg_authid_oid_index');
pg_relation_filepath
----------------------
global/2677
(1 row)
postgres=# SELECT pg_relation_filepath('pg_authid_rolname_index');
pg_relation_filepath
----------------------
global/2676
(1 row)
postgres=# \d+ pg_authid_oid_index
Index "pg_catalog.pg_authid_oid_index"
Column | Type | Definition | Storage
--------+------+------------+---------
oid | oid | oid | plain
unique, btree, for table "pg_catalog.pg_authid"
Tablespace: "pg_global"
postgres=# \d+ pg_authid_rolname_index
Index "pg_catalog.pg_authid_rolname_index"
Column | Type | Definition | Storage
---------+---------+------------+---------
rolname | cstring | rolname | plain
unique, btree, for table "pg_catalog.pg_authid"
Tablespace: "pg_global"
- 将表中数据导出并创建my_authid表
copy pg_authid to '/pgsql/data/backup/pg_authid.txt' with (oids);
create table my_authid(like pg_authid) with oids;
create unique index my_authid_oid_index on my_authid(oid);
create unique index my_authid_rolname_index on my_authid(rolname);
- 导数数据到my_authid表中 导入之前我们要编辑pg_authid.txt文件将对应的OID修改为原User OID对应的关系,这里postgres用户我们新添加一行 (原User OID 也就是10),并将文件中原postgres用户名改为postgres2
copy my_authid from '/pgsql/data/backup/pg_authid.txt' with (oids);
VACUUM FULL FREEZE VERBOSE my_authid;
vacuum my_authid;
- 查看my_authid相关信息
SELECT pg_relation_filepath('my_authid'), pg_relation_filepath('my_authid_oid_index'), pg_relation_filepath('my_authid_rolname_index');
postgres=# SELECT pg_relation_filepath('my_authid'), pg_relation_filepath('my_authid_oid_index'), pg_relation_filepath('my_authid_rolname_index');
pg_relation_filepath | pg_relation_filepath | pg_relation_filepath
----------------------+----------------------+----------------------
base/13806/54036458 | base/13806/54036464 | base/13806/54036465
(1 row)
关闭数据库替换pg_authid表对应的物理文件
- 关闭数据
- 替换pg_authid表对应的物理文件
//整理出pg_authid表及索引与my_authid表物理文件对应管理
global/1260 => base/13806/54036458
global/2677 => base/13806/54036464
global/2676 => base/13806/54036465
//备份原pg_authid表及索引文件
mkdir backup
cp global/1260* ./backup/.
cp global/2677* ./backup/.
cp global/2676* ./backup/.
//将my_authid表物理文件及索引拷贝覆盖原pg_authid对应的文件及索引
cp base/13806/54036458 global/1260
cp base/13806/54036458_fsm global/1260_fsm
cp base/13806/54036458_vm global/1260_vm
cp base/13806/54036464 global/2677
cp base/13806/54036465 global/2676
- 检查观察文件时间及对比文件内容是否发生变化
ls -l global/1260*
ls -l backup/1260*
ls -l global/2677*
ls -l backup/2677*
ls -l global/2676*
ls -l backup/2676*
cmp global/1260 backup/1260
cmp global/2677 backup/2677
cmp global/2676 backup/2676
- 删除系统表cache文件
find . -name "pg_internal.init*"
find . -name "pg_internal.init*" |xargs rm
启动数据库
启动数据库查看数据库及表的owner是否正常,不在是Unknow状态
相关文章
- python实现K近邻算法案例
- 爬虫案例
- matic马蹄链(dapp)系统开发案例丨马蹄链matic智能合约开发功能详情
- 怎么让 echarts 图表动起来?定时器解决它 —— 大屏展示案例(动态仪表盘、动态柱状图)[通俗易懂]
- 热文回顾|【案例】吉利汽车的物流数智化转型实践
- 什么是MetaForce佛萨奇(2.0)系统开发案例(源码搭建)
- 案例 | 数字孪生5G智慧港口三维可视化系统
- 佛萨奇开发丨佛萨奇系统开发(2.0运营版)丨佛萨奇系统源码案例
- 用财务实战案例,理解分组依据的核心原理! | Power Query重点
- 元宇宙游戏链游系统开发丨元宇宙游戏链游系统源码案例开发
- NFT数字藏品系统开发源码搭建案例展示
- 【数字信号处理】线性时不变系统 LTI ( 判断某个系统是否是 “ 非时变 “ 系统 | 案例二 )
- 【数字信号处理】线性时不变系统 LTI ( 判断某个系统是否是 “ 非时变 “ 系统 | 案例三 )
- 【数字信号处理】线性常系数差分方程 ( 根据 “ 线性常系数差分方程 “ 与 “ 边界条件 “ 确定系统是否是 “ 线性时不变系统 “ 案例二 | 修改边界条件 | 使用递推方法证明 )
- 【HTML】HTML 注册表单案例 ② ( 表格中的内容设置 | 下拉列表 | 输入文本框 | 复选框 | 文本域 | 图片按钮 | 链接 )
- 深入Linux系统:实用案例精解(linux系统案例精解)
- 使用Redis缓存提升系统性能一个具体案例(redis缓存数据案例)