博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何修改postgresql中一个表的oid
阅读量:4032 次
发布时间:2019-05-24

本文共 2547 字,大约阅读时间需要 8 分钟。

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

 

转载地址:http://jeebi.baihongyu.com/

你可能感兴趣的文章
Java 8新特性终极指南
查看>>
logback高级特性使用(二) 自定义Pattern模板
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
可扩展、高可用服务网络设计方案
查看>>
如何构建高扩展性网站
查看>>
微服务架构的设计模式
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++指针常量与常量指针详解
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>
c++类的操作符重载注意事项
查看>>
c++模板与泛型编程
查看>>
STL::deque以及由其实现的queue和stack
查看>>
CS4344驱动
查看>>
WAV文件解析
查看>>
DAC输出音乐2-解决pu pu 声
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
WPF UI&控件免费开源库
查看>>
QT打开项目提示no valid settings file could be found
查看>>
Win10+VS+ESP32环境搭建
查看>>