在pg中,当我们需要修改表的某个字段时,如果该字段刚好被视图引用,必须先将引用的对象删除,才能修改对应的字段。

例如:

bill=# create table test_t (id int, info text, crt_time timestamp, c1 varchar(10));
CREATE TABLE
bill=# create index idx_test_t on test_t(c1);
CREATE INDEX
bill=# create view v_test_t as select id,c1 from test_t;
CREATE VIEW
bill=# alter table test_t alter column c1 type varchar(32);
psql: ERROR:  cannot alter type of a column used by a view or rule
DETAIL:  rule _RETURN on view v_test_t depends on column "c1"

不过这个情况在oracle中并不存在:

SQL> create table test_t (id int, info varchar2(100), crt_time timestamp, c1 varchar(10));Table created.SQL> create index idx_test_t on test_t(c1);  Index created.SQL> create view v_test_t as select id,c1 from test_t;  View created.SQL> alter table test_t modify(c1 varchar(32));Table altered.

那么我们在pg中该如何去修改被视图引用的表的字段呢?
pg中支持将DDL语句封装在事务中处理,所以从删除依赖,到修改字段,再到重建依赖,都可以封装在一个事务中完成。

例子:
不过这种方法需要注意:

  1. DDL是需要对表加排它锁的,排它锁与所有其他锁冲突,因此建议在事务开始时设置锁超时参数,避免问题。
  2. 如果修改字段涉及到rewrite table(例如int改到text),那么表很大时间会很久。如果需要很久,意味着需要长时间持有排它锁(堵塞也是比较严重的)。
begin;  -- 开始事务  set local lock_timeout = '1s';  -- 设置锁超时  drop view v_test_t;  -- 删除依赖视图  alter table test_t alter column c1 type varchar(32);  -- 修改字段长度  create view v_test_t as select id,c1 from test_t;  -- 创建视图  end;  -- 结束事务

除此之外我们还可以通过修改pg中元数据表的方式去实现。
因为pg的定义都记录在元数据中,所以某些操作,可以直接修改元数据来实现。比如从numeric低精度修改到高精度,从字符串短长度修改到长长度。
但是不建议这么做,直接修改元数据存在隐患,甚至可能对数据库造成不可修复的伤害。

例子:
1、首先查看将要修改的C1字段的pg_attribute元信息

bill=# select attrelid::regclass,* from pg_attribute where attname='c1'; attrelid  | attrelid | attname | atttypid | attstattarget | attlen | attnum | attndims | attcacheoff | atttypmod | attbyval | attstorage | attalign | attnotnull | atthasdef | atthasmissing | attiden
tity | attgenerated | attisdropped | attislocal | attinhcount | attcollation | attacl | attoptions | attfdwoptions | attmissingval
------------+----------+---------+----------+---------------+--------+--------+----------+-------------+-----------+----------+------------+----------+------------+-----------+---------------+--------
-----+--------------+--------------+------------+-------------+--------------+--------+------------+---------------+---------------test_t     |   200125 | c1      |     1043 |            -1 |     -1 |      4 |        0 |          -1 |        36 | f        | x          | i        | f          | f         | f             |        |              | f            | t          |           0 |          100 |        |            |               | idx_test_t |   200136 | c1      |     1043 |            -1 |     -1 |      1 |        0 |          -1 |        36 | f        | x          | i        | f          | f         | f             |        |              | f            | t          |           0 |          100 |        |            |               | v_test_t   |   200137 | c1      |     1043 |            -1 |     -1 |      2 |        0 |          -1 |        36 | f        | x          | i        | f          | f         | f             |        |              | f            | t          |           0 |          100 |        |            |               |
(3 rows)

在修改时,需要将这三个atttypmod一起修改掉。
变长字段的长度为4字节头+实际长度,所以36表示可以存储32个字符。

2、修改为varchar(64)这样操作

bill=# update pg_attribute set atttypmod=68 where attname='c1' and attrelid in (200125,200136,200137);
UPDATE 3

3、查看更新后的结构

bill=# \d+ test_t Table "public.test_t"Column  |            Type             | Collation | Nullable | Default | Storage  | Stats target | Description
----------+-----------------------------+-----------+----------+---------+----------+--------------+-------------id       | integer                     |           |          |         | plain    |              | info     | text                        |           |          |         | extended |              | crt_time | timestamp without time zone |           |          |         | plain    |              | c1       | character varying(64)       |           |          |         | extended |              |
Indexes:"idx_test_t" btree (c1)
Access method: heapbill=# \d+ v_test_t  View "public.v_test_t"Column |         Type          | Collation | Nullable | Default | Storage  | Description
--------+-----------------------+-----------+----------+---------+----------+-------------id     | integer               |           |          |         | plain    | c1     | character varying(64) |           |          |         | extended |
View definition:SELECT test_t.id,test_t.c1FROM test_t;bill=# \d+ idx_test_tIndex "public.idx_test_t"Column |         Type          | Key? | Definition | Storage  | Stats target
--------+-----------------------+------+------------+----------+--------------c1     | character varying(64) | yes  | c1         | extended |
btree, for table "public.test_t"

PostgreSQL修改被视图引用的表的字段相关推荐

  1. GaussDB(DWS)应用实战:对被视图引用的表进行DDL操作

    摘要:GaussDB(DWS)是从Postgres演进过来的,像Postgres一样,如果表被视图引用的话,特定场景下,部分DDL操作是不能直接执行的. 背景说明 GaussDB(DWS)是从Post ...

  2. ABAP在Eclipse中做abap cds视图(marc表增强字段增强)

    1.安装eclipse 地址:https://www.eclipse.org/downloads/ 2.添加HANA数据库 填入以下网址: http://tools.hana.ondemand.com ...

  3. postgresql 修改表结构 alter table xxx alter column yyy type varchar(19)

    postgresql 修改表结构:修改t_cash表list_id 字段类型为varchar(19) alter table t_cash alter column list_Id type varc ...

  4. oracle 用户表数目,表大小,视图数目及表空间等查询增加修改删除操作

    oracle 用户表数目,表大小,视图数目及表空间等查询增加修改删除操作 查看当前用户的缺省表空间 SQL>select username,default_tablespace fromuser ...

  5. postgresql 修改表的字段由NOT NULL修改为NULL

    postgresql 修改表的字段由NOT NULL修改为NULL: alter table mytable alter test drop not null; 参考地址:http://www.buf ...

  6. postgresql 修改表字段_PostgreSQL 修改表字段常用命令操作

    --数据库.模式.表名 "identities"."Test"."tab_test" --修改字段名 ALTER TABLE "i ...

  7. postgreSQL 修改、创建表空间

    1.表空间的概念 PostgreSQL中的表空间允许在文件系统中定义用来存放表示数据库对象的文件的位置.在PostgreSQL中表空间实际上就是给表指定一个存储目录. 2.表空间的作用 官方解释: 通 ...

  8. informix clob转oracle 乱码_Oracle 视图-序列-权限-表-事务

    一.视图 视图(view),称为虚表,在数据库中不存在实体. 视图本质上是对物理表(基表)的一种数据保护.让开发者或者用户只能看到基表中的部分数据. 1.1 创建视图 创建视图的语法 1.2 使用视图 ...

  9. oracle 物化视图、中间表的方案

    物化视图 有个项目因为有比较多的查询汇总,考虑到速度,所以使用了物化视图.简单的把用到的给整理了下.先看简单创建语句: create materialized view mv_materialized ...

最新文章

  1. Tungsten Fabric SDN — 报文转发流程
  2. 全球及中国医用腋拐行业竞争格局及供需前景预测报告2021年版
  3. 循环发ajax请求,在循环中发送jquery ajax请求
  4. LeetCode 977. 有序数组的平方
  5. jdbc 批量insert_JDBC相关知识解答
  6. 必须使用初始化列表的情况
  7. Memcached概述
  8. 衔接UI线程和管理后台工作线程的类(多线程、异步调用)[转]
  9. UEFI+GPT安装Win10和RHEL6.5双系统
  10. 阿里面试官最新分享的Java面试宝典,含8大核心内容讲解
  11. html mint ui,移动端UI库对比 vant mint-ui
  12. 君正X1000串口流控bug
  13. 【Multisim仿真】利用运算放大器产生方波、三角波发生器
  14. 4 个方法养成大神级 “反内耗“ 体质
  15. 华为手机各代系拆机图
  16. oracle用plsql导出dmp文件
  17. 关于“Error: Net gdfx_temp0, which fans out to ***:inst4|BIN, cannot be assigned more than”错误
  18. [mysql安装教程]解决ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘ (10061)问题
  19. MathType6.9安装后,office16打开报错(DLL找不到)的解决方法
  20. [附源码]SSM计算机毕业设计网上鞋店管理系统JAVA

热门文章

  1. SCT2650STER,可以实现低成本升降压
  2. python操作微信手机端下载_【python】自动化连接和操作手机微信
  3. 零信任与区块链:身份的中心化和去中心化
  4. SQL Server错误代码大全及解释
  5. Java源码之HashMap
  6. l2正则化python_L1、L2正则化的区别
  7. 小程序公众号共服务器,公众号和小程序用户互通,无需unionid解决方案
  8. ACIS,Parasolid两种CAD几何内核的优劣势对比
  9. SVN服务端使用教程
  10. Cache数据库ECP梳理