gpt4 book ai didi

postgresql - 查找 postgres 9 中哪些行或列正在使用 TOAST(并减少 TOAST 使用)?

转载 作者:行者123 更新时间:2023-12-03 03:37:02 27 4
gpt4 key购买 nike

我继承了一个具有大得离谱的 TOAST 表的系统的维护工作。对于大约 400 MB 的表和索引大小,Toast 为 631 GB

clientportal=# SELECT oid, table_schema, table_name,                                                                                        
total_bytes,
pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a order by total_bytes desc;


oid | table_schema | table_name | total_bytes | total | index | toast | table
---------+--------------------+------------------------------------------+--------------+------------+------------+------------+------------
17202 | public | phones | 678297559040 | 632 GB | 119 MB | 631 GB | 280 MB

使用对所有表执行 pg_size_pretty(sum(pg_column_size($columnName))) 的查询,我看不出哪一列可能使用那么多。所以我可能遇到了该函数的限制或误解:

-[ RECORD 1 ]---------+-----------
access_info | 153 kB
activation_date | 1291 kB
autocreate | 835 kB
btn_id | 3409 kB
callerid | 5954 kB
calling_firstname | 1 bytes
calling_lastname | 1 bytes
connectiontype_id | 1 bytes
created_at | 6985 kB
did_supplier_id | 17 kB
e911_notes | 274 kB
e911_tested | 70 bytes
e911_tested_by_id |
e911_tested_date |
email | 2431 kB
extension | 2020 kB
firstname | 152 kB
foreign_pid | 16 kB
foreign_userid | 10 kB
human_validation_date | 176 bytes
huntgroup | 873 kB
id | 3492 kB
incoming_toll | 873 kB
ipaddress | 1 bytes
lastname | 4406 bytes
line_of_business | 305 kB
local_rate_plan_id | 6264 bytes
national_rate_plan_id |
notes | 114 MB
phone_mac_addr | 5041 kB
phone_number | 9656 kB
phone_type_id |
rate_center_id | 4 bytes
service_type | 3786 bytes
sip_password | 22 MB
sip_username | 8919 kB
site_id | 3480 kB
termination_date | 2217 kB
updated_at | 6985 kB
virtualdid | 873 kB
voicemail_number | 1268 kB
voicemailportal | 873 kB
world_rate_plan_id |

我怀疑 sip_passwordnotes 列正在将该行发送到 toast。这并不能证明我希望将这些列设置为 null 会减少 TOAST 使用量,但经过研究,我假设这种情况不会发生,或者需要 VACCUM FULL

  1. 有没有办法确定哪一列或哪一行正在使用那么多 TOAST 空间?
  2. 将一些文本列设置为 null 后,我需要运行什么来减小 TOAST 大小?

更新:如果 \d+ 很重要(但它并不能完全回答问题):

clientportal=# \d+ phones
Table "public.phones"
Column | Type | Modifiers | Storage | Stats target | Description
-----------------------+-----------------------------+-----------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('phones_id_seq'::regclass) | plain | |
created_at | timestamp without time zone | | plain | |
updated_at | timestamp without time zone | | plain | |
site_id | integer | | plain | |
btn_id | integer | | plain | |
phone_number | character varying(255) | | extended | |
extension | character varying(255) | | extended | |
voicemail_number | character varying(255) | | extended | |
access_info | character varying(255) | | extended | |
notes | text | | extended | |
activation_date | date | | plain | |
termination_date | date | | plain | |
phone_mac_addr | character varying(255) | | extended | |
phone_type_id | integer | | plain | |
sip_username | character varying(255) | | extended | |
e911_notes | character varying(255) | | extended | |
e911_tested | boolean | | plain | |
e911_tested_by_id | integer | | plain | |
e911_tested_date | date | | plain | |
did_supplier_id | integer | | plain | |
email | character varying(255) | | extended | |
ipaddress | character varying(255) | | extended | |
connectiontype_id | character varying(255) | | extended | |
sip_password | text | | extended | |
virtualdid | boolean | default false | plain | |
voicemailportal | boolean | default false | plain | |
callerid | character varying(255) | default NULL::character varying | extended | |
autocreate | boolean | default false | plain | |
huntgroup | boolean | default false | plain | |
firstname | text | | extended | |
lastname | text | | extended | |
calling_firstname | text | | extended | |
calling_lastname | text | | extended | |
foreign_userId | text | | extended | |
local_rate_plan_id | integer | | plain | |
national_rate_plan_id | integer | | plain | |
world_rate_plan_id | integer | | plain | |
foreign_pid | integer | | plain | |
rate_center_id | integer | | plain | |
human_validation_date | timestamp without time zone | | plain | |
service_type | character varying | | extended | |
line_of_business | character varying | | extended | |
incoming_toll | boolean | default false | plain | |
Indexes:
"phones_pkey" PRIMARY KEY, btree (id)
"index_phones_on_id" UNIQUE, btree (id)
"index_phones_on_btnid" btree (btn_id)
"index_phones_on_extension" btree (extension)
"index_phones_on_foreign_pid" btree (foreign_pid)

最佳答案

pg_column_size测量 toast 值的大小,仅测量 toast 指针的大小。

但是您可以通过将其输出与使用 length() 获得的实际列大小进行比较来利用这一点。

可能是 varchar(255) 列,但主要嫌疑人是 textcharacter Varying(没有长度)列.

如果你没有发现这样的情况,那么 toast 表也有可能出现膨胀。使用 pgstattuple 扩展来测量它。

关于postgresql - 查找 postgres 9 中哪些行或列正在使用 TOAST(并减少 TOAST 使用)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096852/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com