ALTER TABLE rewrites

alter一个表的列的时候会出现rewrites表:
比如添加一个有默认值的新列会rewrites重写表(即先vacuum full此表): http://my.oschina.net/Kenyon/blog/99757
ctid来查看有默认值的新列:

digoal=# select ctid,* from foo;

ctid | fooid | foosubid | fooname | col1 | col2 -------+-------+----------+---------+------+------ (0,1) | 1 | 2 | three | | 90 (0,2) | 4 | 5 | six | | 90 (0,3) | 5 | 5 | two | | 90 (0,4) | 6 | 6 | rrr | | 90 (0,5) | 7 | 7 | rrr | | 90 (5 rows)

digoal=# update foo set fooname='vvvvv' where fooid=6; UPDATE 1 digoal=# select ctid,* from foo; ctid | fooid | foosubid | fooname | col1 | col2 -------+-------+----------+---------+------+------ (0,1) | 1 | 2 | three | | 90 (0,2) | 4 | 5 | six | | 90 (0,3) | 5 | 5 | two | | 90 (0,5) | 7 | 7 | rrr | | 90 (0,6) | 6 | 6 | vvvvv | | 90 (5 rows) digoal=# alter table foo add col3 int default 22; ALTER TABLE digoal=# select ctid,* from foo; ctid | fooid | foosubid | fooname | col1 | col2 | col3 -------+-------+----------+---------+------+------+------ (0,1) | 1 | 2 | three | | 90 | 22 (0,2) | 4 | 5 | six | | 90 | 22 (0,3) | 5 | 5 | two | | 90 | 22 (0,4) | 7 | 7 | rrr | | 90 | 22 (0,5) | 6 | 6 | vvvvv | | 90 | 22 (5 rows)

ctid来查看无默认值的新列:

digoal=# update foo set fooname='vvvvv' where fooid=6; UPDATE 1 digoal=# select ctid,* from foo; ctid | fooid | foosubid | fooname | col1 | col2 | col3 | col4 -------+-------+----------+---------+------+------+------+------ (0,1) | 1 | 2 | three | | 90 | 22 | 202 (0,2) | 4 | 5 | six | | 90 | 22 | 202 (0,3) | 5 | 5 | two | | 90 | 22 | 202 (0,4) | 7 | 7 | rrr | | 90 | 22 | 202 (0,6) | 6 | 6 | vvvvv | | 90 | 22 | 202 (5 rows) digoal=# alter table foo add col5 int; ALTER TABLE digoal=# select ctid,* from foo; ctid | fooid | foosubid | fooname | col1 | col2 | col3 | col4 | col5 -------+-------+----------+---------+------+------+------+------+------ (0,1) | 1 | 2 | three | | 90 | 22 | 202 | (0,2) | 4 | 5 | six | | 90 | 22 | 202 | (0,3) | 5 | 5 | two | | 90 | 22 | 202 | (0,4) | 7 | 7 | rrr | | 90 | 22 | 202 | (0,6) | 6 | 6 | vvvvv | | 90 | 22 | 202 | (5 rows)


避免rewrites 重写的原则如下: http://wiki.postgresql.org/wiki/What's_new_in_PostgreSQL_9.2

A table won't get rewritten anymore during an ALTER TABLE when changing the type of a column in the following cases:

  • varchar(x) to varchar(y) when y>=x. It works too if going from varchar(x) to varchar or text (no size limitation)
  • numeric(x,z) to numeric(y,z) when y>=x, or to numeric without specifier
  • varbit(x) to varbit(y) when y>=x, or to varbit without specifier
  • timestamp(x) to timestamp(y) when y>=x or timestamp without specifier
  • timestamptz(x) to timestamptz(y) when y>=x or timestamptz without specifier
  • interval(x) to interval(y) when y>=x or interval without specifier
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值