加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何修改postgresql中一个表的oid

发布时间:2020-12-13 17:54:10 所属栏目:百科 来源:网络整理
导读:oid是一个系统的隐藏列。直接修改是不行的。 mysql=# update pg_class set oid = 99999 where oid=73728; ERROR: cannot assign to system column "oid" LINE 1: update pg_class set oid = 99999 where oid=73728; 但是我们可以将其删除 mysql=# delete fro

oid是一个系统的隐藏列。直接修改是不行的。
mysql=# update pg_class set oid = 99999 where oid=73728;
ERROR: cannot assign to system column "oid"
LINE 1: update pg_class set oid = 99999 where oid=73728;

但是我们可以将其删除
mysql=# delete from pg_class where oid=73728;
DELETE 1

在postgresql中有一个copy命令,有一个参数 with oids,可以将oid一起导入,利用这个特性,我们可以达到oid的修改功能。
1.首先新建一个文件,将数据放入,新的oid已修改
[mysql@pttest4 cxf]$ cat 2.dat
73740|test|2200|73729|10|0|73740|0|0|0|0|0|f|f|r|1|0|0|0|0|0|f|f|f|f|814|/N|/N
2.使用copy命令导入数据
copy pg_class from '/home/mysql/cxf/2.dat' null as E'//N' csv delimiter '|' oids;
3.修改其他表中的对应oid数据
update pg_attribute set attrelid ='73740' where attrelid ='73728';
update pg_depend set refobjid='73740' where refobjid ='73728';
update pg_type set typrelid ='73740' where typrelid = '73728';
ps:如果还涉及其他的数据字典,应一并修改掉

4.查询,可以看出oid已变更
mysql=# select * from test;
ERROR: could not open relation 1663/16386/73740: No such file or directory

mysql=# select oid from pg_class where relname ='test';
oid
-------
73740
(1 row)

5.将数据文件更名为新的oid
[mysql@pttest4 16386]$ mv 73728 73740
再查数据库,原有的那条数据还是可以访问到的。
mysql=# select * from test;
a
---
1
(1 row)


至此,oid修改成功。

修改oid引发的其他问题。由于当前系统的oid是73735,而我们变更的oid(73740)在这个之后,接着我们创建表的时候这个oid还会重用,这样有可能会造成oid错乱。如下
mysql=# create table helloworld(b varchar(1));
CREATE TABLE
mysql=# select oid from pg_class where relname='helloworld';
oid
-------
73735
(1 row)

mysql=# create table helloworld1(b varchar(1));
CREATE TABLE
mysql=# create table helloworld2(b varchar(1));
CREATE TABLE
mysql=# create table helloworld3(b varchar(1));
CREATE TABLE
mysql=# select oid from pg_class where relname like 'helloworld%';
oid
-------
73735
73737
73739
73741
(4 rows)

mysql=# select * from pg_type where oid = '73740';
typname | typnamespace | typowner | typlen | typbyval | typtype | typisdefined | typdelim | typrelid | typelem | typinput | typoutput | typreceive | typsend | typanalyze | typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typdefaultbin | typdefault
-------------+--------------+----------+--------+----------+---------+--------------+----------+----------+---------+-----------+------------+-------------+-------------+------------+----------+------------+------------+-------------+-----------+----------+---------------+------------
helloworld2 | 2200 | 10 | -1 | f | c | t |,| 73739 | 0 | record_in | record_out | record_recv | record_send | - | d | x | f | 0 | -1 | 0 | |
(1 row)

可以看到,新的pg_type的oid用到了73740。为了避免这种情况,我们可以用pg_resetxlog重置oid。如:pg_resetxlog -o 80000 ~/postgresql/data/这样子建表的时候oid就重80000开始了,避免了刚刚那个问题。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读