PG 被删除列的恢复

PG删除列后,只是做了一个标记,并没有真正回收空间
举例:
hank=> select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16), 64-bit

hank=>\c hank hank
hank=> \d t1
          Table "hank.t1"
 Column |     Type      | Modifiers 
--------+---------------+-----------
 a      | integer       | 
 b      | character(20) | 

hank=> select * from t1;
 a  |          b           
----+----------------------
  2 | wang                
  3 | li                  
 20 | hank                
 20 | hank                
 20 | hank                


 hank=> select oid from pg_class where relname='t1';
  oid  
-------
 74219

 hank=> select * from pg_attribute where attrelid=74219 and attname='b';
 attrelid | attname | atttypid | attstattarget | attlen | attnum | attndims | attcacheoff | atttypmod | attbyval | attstorage | attalign | attnotnull 
| atthasdef | attisdropped | attislocal | attinhcount | attcollation | attacl | attoptions | attfdwoptions 
----------+---------+----------+---------------+--------+--------+----------+-------------+-----------+----------+------------+----------+------------
+-----------+--------------+------------+-------------+--------------+--------+------------+---------------
    74219 | b       |     1042 |            -1 |     -1 |      2 |        0 |          -1 |        24 | f        | x          | i        | f          
| f         | f            | t          |           0 |          100 |        |            | 


hank=> alter table t1 drop column  b;

hank=> select * from pg_attribute where attrelid=74219;
 attrelid |           attname            | atttypid | attstattarget | attlen | attnum | attndims | attcacheoff | atttypmod | attbyval | attstorage | a
ttalign | attnotnull | atthasdef | attisdropped | attislocal | attinhcount | attcollation | attacl | attoptions | attfdwoptions 
----------+------------------------------+----------+---------------+--------+--------+----------+-------------+-----------+----------+------------+--
--------+------------+-----------+--------------+------------+-------------+--------------+--------+------------+---------------
    74219 | tableoid                     |       26 |             0 |      4 |     -7 |        0 |          -1 |        -1 | t        | p          | i
        | t          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | cmax                         |       29 |             0 |      4 |     -6 |        0 |          -1 |        -1 | t        | p          | i
        | t          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | xmax                         |       28 |             0 |      4 |     -5 |        0 |          -1 |        -1 | t        | p          | i
        | t          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | cmin                         |       29 |             0 |      4 |     -4 |        0 |          -1 |        -1 | t        | p          | i
        | t          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | xmin                         |       28 |             0 |      4 |     -3 |        0 |          -1 |        -1 | t        | p          | i
        | t          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | ctid                         |       27 |             0 |      6 |     -1 |        0 |          -1 |        -1 | f        | p          | s
        | t          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | a                            |       23 |            -1 |      4 |      1 |        0 |          -1 |        -1 | t        | p          | i
        | f          | f         | f            | t          |           0 |            0 |        |            | 
    74219 | ........pg.dropped.2........ |        0 |             0 |     -1 |      2 |        0 |          -1 |        24 | f        | x          | i
        | f          | f         | t            | t          |           0 |          100 |        |            | 

只要更新一下字典表即可恢复
hank=> \c hank postgres
You are now connected to database "hank" as user "postgres".
hank=# update pg_attribute set attisdropped='f',attname='b',atttypid=1042 where attrelid=74219 and attnum = 2;
UPDATE 1

hank=# \d hank.t1
          Table "hank.t1"
 Column |     Type      | Modifiers 
--------+---------------+-----------
 a      | integer       | 
 b      | character(20) | 


hank=# select * from hank.t1;
 a  |          b           
----+----------------------
  2 | wang                
  3 | li                  
 20 | hank                
 20 | hank                
 20 | hank                
(5 rows)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值