PostgreSQL中TOAST功能

 如果表中有某些字段使用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的大小是包括了索引所占用的空间的。

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

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

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

猜你喜欢

转载自www.linuxidc.com/Linux/2015-12/126192.htm