前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PG中恢复系统表案例

PG中恢复系统表案例

原创
作者头像
donghy
修改2022-09-17 15:59:59
8600
修改2022-09-17 15:59:59
举报
文章被收录于专栏:懒人的记录懒人的记录
  • 恢复系统表案例
    • 处理方式
      • 具体步骤
        • 找出原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对应关系
代码语言:javascript
复制
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对应关系
代码语言:javascript
复制
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表相关信息
代码语言:javascript
复制
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表
代码语言:javascript
复制
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
代码语言:javascript
复制
copy my_authid from '/pgsql/data/backup/pg_authid.txt' with (oids);
VACUUM FULL FREEZE VERBOSE my_authid;
vacuum my_authid;
  • 查看my_authid相关信息
代码语言:javascript
复制
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表对应的物理文件
代码语言:javascript
复制
//整理出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 
  • 检查观察文件时间及对比文件内容是否发生变化
代码语言:javascript
复制
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文件
代码语言:javascript
复制
find . -name "pg_internal.init*"
find . -name "pg_internal.init*" |xargs rm
启动数据库

启动数据库查看数据库及表的owner是否正常,不在是Unknow状态

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 处理方式
    • 具体步骤
      • 找出原User OID对应关系
      • 创建一张中间表(my_authid)
      • 关闭数据库替换pg_authid表对应的物理文件
      • 启动数据库
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档