如何修改postgresql中一个表的oid

前端之家收集整理的这篇文章主要介绍了如何修改postgresql中一个表的oid前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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开始了,避免了刚刚那个问题。

原文链接:https://www.f2er.com/postgresql/197028.html

猜你在找的Postgre SQL相关文章