Oracle vs PostgreSQL,研发注意事项(3)- 事务回滚之UPDATE操作解析

Oracle事务的回滚,通过回滚段保存原有数据实现,但,PG没有回滚段!以下以Update操作为例,说明PG实现机制上存在的空间暴涨问题。

在执行Update时,Oracle就地更新,如出现原block空间不足的情况,通过link的方式链接至新block上(不精确,大体表述);PG的Update,不是原地更新,而是保留原有数据,通过新增新的tuple(数据行)保存新增数据,原有数据通过Vacuum机制清理。Vacuum机制需要满足MVCC(多版本并发控制)的要求,在某些情况下,不会清理“垃圾”数据,在事务繁忙的时候导致会导致数据表空间不断增长。

--------------------------- Session A

-- 开启事务

begin;

-- 查询当前事务

select txid_current();

txid_current

--------------

      1500987

(1 row)

-- 什么都不做,会导致Vacuum不能清理“垃圾”数据

--------------------------- Session B

-- 开启事务

begin;

select txid_current();

txid_current

--------------

      1500988

(1 row)

-- 创建表&插入100数据

drop table if exists t1;

create table t1(id int,c1 varchar(50));

insert into t1 select generate_series(1,100),'#TESTDATA#';

------------------- 以上操作省略输出

select txid_current();

txid_current

--------------

      1500988

(1 row)

-- 提交事务

end;

-- 查看数据表

select ctid, xmin, xmax, cmin, cmax,id from t1;

testdb=# select ctid, xmin, xmax, cmin, cmax,id from t1;

  ctid  |  xmin  | xmax | cmin | cmax | id 

---------+---------+------+------+------+-----

(0,1)  | 1500988 |    0 |    4 |    4 |  1

(0,2)  | 1500988 |    0 |    4 |    4 |  2

(0,3)  | 1500988 |    0 |    4 |    4 |  3

(0,4)  | 1500988 |    0 |    4 |    4 |  4

......

-- 查看数据占用空间

\set v_tablename t1

SELECT pg_size_pretty( pg_total_relation_size(:'v_tablename') );

testdb=# SELECT pg_size_pretty( pg_total_relation_size(:'v_tablename') );

pg_size_pretty

----------------

8192 bytes

(1 row)


-- 使用pgbench进行压力测试,不断更新数据

cat update.sql

\set rowid random(1,100)

begin;

update t1 set c1=c1||:rowid where id= :rowid;

end;

pgbench -c 2 -C -f ./update.sql -j 1 -n -T 600 -U xdb testdb


-- 一段时间后查看数据占用空间

SELECT pg_size_pretty( pg_total_relation_size(:'v_tablename') );

testdb=# SELECT pg_size_pretty( pg_total_relation_size(:'v_tablename') );

pg_size_pretty

----------------

  1344 kB

(1 row)

从原来的8192 Bytes变成了1344 KB,空间“暴涨”。

究其原因,是因为PG的MVCC实现机制导致的:如果存在某个事务,在更新数据前开启,那么更新数据时前后的数据都要存储,无论更新多少次都要存储!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2158216/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/6906/viewspace-2158216/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值