如何检查TOAST是否正在postgres中的特定表上工作

jin*_*dal 12 sql postgresql data-compression

我有一个包含两个文本字段的表,其中包含大量文本.出于某种原因,我们的桌子开始成倍增长.我怀疑TOAST(postgres中的文本字段压缩)不能自动运行.在我们的表定义中,我们没有定义任何强制压缩这些字段的子句.有没有办法检查压缩是否在该表上工作?

Mik*_*ll' 12

来自文档...

如果表的任何列都是TOAST-able,则该表将具有关联的TOAST表,其OID存储在表的pg_class.reltoastrelid条目中.外部TOASTed值保留在TOAST表中,如下面更详细描述的.

因此,您可以通过查询pg_class系统目录来确定是否存在TOAST表.这应该让你接近你正在寻找的东西.

select t1.oid, t1.relname, t1.relkind, t2.relkind, t2.relpages, t2.reltuples
from pg_class t1
inner join pg_class t2
on t1.reltoastrelid = t2.oid
where t1.relkind = 'r'
  and t2.relkind = 't';
Run Code Online (Sandbox Code Playgroud)

在psql中,您可以使用\d+.我将以pg_class系统目录为例; 你会使用自己的表名.

sandbox=# \d+ pg_class
     Column     |   Type    | Modifiers | Storage  | Stats target | Description 
----------------+-----------+-----------+----------+--------------+-------------
 relname        | name      | not null  | plain    |              | 
 relnamespace   | oid       | not null  | plain    |              | 
 [snip]
 relacl         | aclitem[] |           | extended |              | 
 reloptions     | text[]    |           | extended |              | 
Run Code Online (Sandbox Code Playgroud)

在存储"扩展"的情况下,PostgreSQL将尝试通过先压缩,然后通过存储数据来减少行大小.如果存储是"主要"(未显示),PostgreSQL将尝试压缩.

在您的特定情况下,您可能会发现监视大小随时间变化很有用.您可以使用此查询,并保存结果以供以后分析.

select table_catalog, table_schema, table_name, 
       pg_total_relation_size(table_catalog || '.' || table_schema|| '.' || table_name) as pg_total_relation_size,
       pg_relation_size(table_catalog || '.' || table_schema|| '.' || table_name) as pg_relation_size,
       pg_table_size(table_catalog || '.' || table_schema|| '.' || table_name) as pg_table_size
from information_schema.tables
Run Code Online (Sandbox Code Playgroud)

PostgreSQL管理功能详细介绍了每个功能在计算中包含的内容.