【PG恢复被打了删除标记的列】

环境 PostgreSQL 12.1

一、创建测试数据

postgres=# create table ysl1(id int,name char(10));
CREATE TABLE
postgres=# insert into ysl1 values(1,'qwe');
INSERT 0 1
postgres=# insert into ysl1 values(2,'q32');
INSERT 0 1

postgres=# \dt+ ysl1
                      List of relations
 Schema | Name | Type  |  Owner   |    Size    | Description
--------+------+-------+----------+------------+-------------
 public | ysl1 | table | postgres | 8192 bytes |
(1 row)

postgres=#  select oid from pg_class where relname='ysl1';
  oid
-------
 65547
(1 row)

二、查看PG_ATTRIBUTE中存储的表的相关列

PG_ATTRIBUTE系统表存储关于表字段的信息。

select * from pg_attribute where attrelid=65547;

三、删除列

postgres=# alter table ysl1 drop column name;
ALTER TABLE

postgres=# select * from ysl1;
 id
----
  1
  2
(2 rows)

postgres=# \d ysl1
                Table "public.ysl1"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |


postgres=# select * from pg_attribute where attrelid=65547;

删除列后,查看到这个列对应的行的attname变为了 …pg.dropped.2…,且字段类型也从原有数值变为了0,attisdropped也从f变为了空,被打上了删除标记。

列被打上了标记,但是他的物理数据其实还存在表里,只需要根据pg_attribute里attrelid,attnum的找到对应的列的行,将attname,atttypid,attisdropped更新一下,就可以恢复。

名称类型描述
attrelidoid此字段所属表
attnumsmallint字段编号

名称类型描述
attnamename字段名。
atttypidoid字段类型。
attisdroppedBoolean这个字段已经被删除了,不再有效。一个已经删除的字段物理上仍然存在表中,但会被分析器忽略,因此不能再通过SQL访问。

四、恢复被删除的列

update pg_attribute set attisdropped='f',attname='name',atttypid=1042 where attrelid=65547 and attnum =2; 

执行如上语句恢复被删除的列,将原有被标记的列的名字复原,以及指定其字段类型,和清除删除标记。

注意:atttypid字段类型需要和原来的相对应,否则尽管数据恢复了,数据也是错的,可能原来相应的存储的字母变为数字等等。

postgres=# update pg_attribute set attisdropped='f',attname='name',atttypid=1042 where attrelid=65547 and attnum =2;
UPDATE 1
postgres=# \d ysl1
                   Table "public.ysl1"
 Column |     Type      | Collation | Nullable | Default
--------+---------------+-----------+----------+---------
 id     | integer       |           |          |
 name   | character(10) |           |          |

postgres=# select * from ysl1;
 id |    name
----+------------
  1 | qwe
  2 | q32
(2 rows)

可以看到数据恢复了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小怪兽ysl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值