你好,游客 登录 注册 搜索
背景:
阅读新闻

PostgreSQL中TOAST功能

[日期:2015-12-13] 来源:Linux社区  作者:Linux [字体:大 中 小]

PostgreSQL中的TOAST的内容是存储在另一个object中的,与原表的OID不一样。比较像Oracle中的LOB类型存储,(默认使用 out-of-line storage 是存储在lobsegment里的, Inline Storage 存储在表的空间里)。

 如果表中有某些字段使用TOAST进行存储,那么,通过普通的pg_relation_size('表名')查询不到TOAST字段所占用的空间。如果要查询TOAST字段所占用的空间,可以先查询出TOAST字段对应的OID,再通过pg_relation_size(OID)的方式查询出TOAST字段所占用的空间。

--以下实验验证pg_relation_size('表名')查询不到TOAST字段所占用的空间

testdb=> create table t(id int, remark text);
 CREATE TABLE
 testdb=> insert into t(id) select random() * 100 from generate_series(1,500);
 INSERT 0 500

 testdb=> select oid,relname,reltoastrelid from pg_class where relname = 't';
  oid  | relname | reltoastrelid
 -------+---------+---------------
  24679 | t      |        24682
 (1 row)

 testdb=> select pg_size_pretty(pg_relation_size(24679));
  pg_size_pretty
 ----------------
  24 kB
 (1 row)

 testdb=> select pg_size_pretty(pg_relation_size(24682));
  pg_size_pretty
 ----------------
  0 bytes
 (1 row)
 TOAST字段所占用的空间现在是0

 testdb=> select pg_size_pretty(pg_total_relation_size('t'));
  pg_size_pretty
 ----------------
  56 kB
 (1 row)

 testdb=> insert into t(remark) select repeat(md5(random()::text), 10000) from generate_series(1,1000);
 INSERT 0 1000
 testdb=> select pg_size_pretty(pg_relation_size(24679));
  pg_size_pretty
 ----------------
  72 kB
 (1 row)

 testdb=> select pg_size_pretty(pg_relation_size(24682));
  pg_size_pretty
 ----------------
  4000 kB
 (1 row)
 TOAST字段所占用的空间已经变为4000kB

 testdb=> select pg_size_pretty(pg_total_relation_size('t'));
  pg_size_pretty
 ----------------
  4184 kB
 (1 row)
使用pg_total_relation_size查出的结果是包括TOAST字段所占用的空间的。

testdb=> create index idx_id_id on t(id);
 CREATE INDEX

 testdb=> select pg_size_pretty(pg_relation_size(24679));
  pg_size_pretty
 ----------------
  72 kB
 (1 row)

 testdb=> select pg_size_pretty(pg_relation_size(24682));
  pg_size_pretty
 ----------------
  4000 kB
 (1 row)

 testdb=> select pg_size_pretty(pg_total_relation_size('t'));
  pg_size_pretty
 ----------------
  4240 kB
 (1 row)

增加索引后,OID为24679和24682的大小都不变,而pg_total_relation_size的大小增加了,所以pg_total_relation_size的大小是包括了索引所占用的空间的。

------------------------------------华丽丽的分割线------------------------------------

Ubuntu Server 14.04 下安装 PostgreSQL 9.3.5 数据库  http://www.linuxidc.com/Linux/2015-12/126175.htm

CentOS 6.3环境下yum安装PostgreSQL 9.3 http://www.linuxidc.com/Linux/2014-05/101787.htm

PostgreSQL缓存详述 http://www.linuxidc.com/Linux/2013-07/87778.htm

Windows平台编译 PostgreSQL http://www.linuxidc.com/Linux/2013-05/85114.htm

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装 http://www.linuxidc.com/Linux/2013-04/83564.htm

Ubuntu上的phppgAdmin安装及配置 http://www.linuxidc.com/Linux/2011-08/40520.htm

CentOS平台下安装PostgreSQL9.3 http://www.linuxidc.com/Linux/2014-05/101723.htm

PostgreSQL配置Streaming Replication集群 http://www.linuxidc.com/Linux/2014-05/101724.htm

如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin  http://www.linuxidc.com/Linux/2014-12/110108.htm

------------------------------------华丽丽的分割线------------------------------------

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-12/126192.htm

linux
相关资讯       PostgreSQL TOAST 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款