|
| 1 | +# Check for table bloat |
| 2 | + |
| 3 | +## Why tables become bloated |
| 4 | + |
| 5 | +Frequent UPDATE and DELETE operations can cause a noticeable increase in table size, |
| 6 | +because old row versions [are not removed immediately](https://www.postgresql.org/docs/17/routine-vacuuming.html). |
| 7 | +Non-blocking cleanup marks these obsolete versions as deleted, and they can later be reused for adding new rows, |
| 8 | +but the physical space is returned to the system only if these deleted rows were at the end of the table. |
| 9 | + |
| 10 | +## Why you should keep an eye on table bloat |
| 11 | + |
| 12 | +Although obsolete records are gradually processed by the autovacuum daemon, the table size will remain too large and the table sparse. |
| 13 | +This leads to decreased performance, because scanning the table becomes slower. |
| 14 | +Therefore, it is important to track sharp changes in table size if the data is updated frequently. |
| 15 | +Data about too rapid growth of a table's size may also indicate that autovacuum is configured incorrectly and these settings need to be changed. |
| 16 | + |
| 17 | +## SQL query |
| 18 | + |
| 19 | +- [bloated_tables.sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/bloated_tables.sql) |
| 20 | + |
| 21 | +## Check type |
| 22 | + |
| 23 | +- **runtime** (requires accumulated statistics) |
| 24 | + |
| 25 | +## Support for partitioned tables |
| 26 | + |
| 27 | +Partitioned tables are supported. The bloat percentage is calculated for each partition separately. |
| 28 | + |
| 29 | +## How this check works |
| 30 | + |
| 31 | +To run the query, the user needs read permissions on the tables being checked. |
| 32 | + |
| 33 | +### Principle of operation |
| 34 | + |
| 35 | +A SQL query is executed against the tables of the `pg_catalog` system schema. They contain statistical information about the main objects: |
| 36 | +tables, indexes, columns. |
| 37 | + |
| 38 | +First, the query gathers data about the tables. It checks whether statistics are available for the table. |
| 39 | + |
| 40 | +Then, based on this data, it determines the size of a single tuple and the total number of pages used by the table. |
| 41 | +Next, it estimates the number of pages that the table should use, and compares it with the actual number of pages. |
| 42 | +Finally, it calculates the table's bloat in bytes (the difference in pages multiplied by the block size) and as a percentage. |
| 43 | +If it exceeds the specified value (10% by default), the table is considered bloated. |
| 44 | + |
| 45 | +## Reproduction script |
| 46 | + |
| 47 | +```sql |
| 48 | +create schema if not exists demo; |
| 49 | + |
| 50 | +-- For ordinary (non-partitioned) tables |
| 51 | + |
| 52 | +create table if not exists demo.orders( |
| 53 | + id bigint primary key generated always as identity, |
| 54 | + user_id bigint not null, |
| 55 | + shop_id bigint not null, |
| 56 | + status int not null, |
| 57 | + created_at timestamptz not null default current_timestamp |
| 58 | +); |
| 59 | + |
| 60 | +create table if not exists demo.order_item( |
| 61 | + id bigint primary key generated always as identity, |
| 62 | + order_id bigint not null references demo.orders (id), |
| 63 | + price decimal(22, 2) not null default 0, |
| 64 | + amount int not null default 0, |
| 65 | + sku varchar(255) not null, |
| 66 | + warehouse_id int |
| 67 | +); |
| 68 | + |
| 69 | +create index if not exists idx_order_item_order_id |
| 70 | + on demo.order_item (order_id); |
| 71 | + |
| 72 | +create index if not exists idx_order_item_warehouse_id_without_nulls |
| 73 | + on demo.order_item (warehouse_id) where warehouse_id is not null; |
| 74 | + |
| 75 | +-- Populating with data |
| 76 | + |
| 77 | +insert into demo.orders (user_id, shop_id, status) |
| 78 | +select |
| 79 | + (ids.id % 10) + 1 as user_id, |
| 80 | + (ids.id % 4) + 1 as shop_id, |
| 81 | + 1 as status |
| 82 | +from generate_series(1, 10000) ids (id); |
| 83 | + |
| 84 | +insert into demo.order_item (order_id, price, amount, sku) |
| 85 | +select |
| 86 | + id as order_id, |
| 87 | + (random() + 1) * 1000.0 as price, |
| 88 | + (random() * 10) + 1 as amount, |
| 89 | + md5(random()::text) as sku |
| 90 | +from demo.orders; |
| 91 | + |
| 92 | +insert into demo.order_item (order_id, price, amount, sku) |
| 93 | +select |
| 94 | + id as order_id, |
| 95 | + (random() + 1) * 2000.0 as price, |
| 96 | + (random() * 5) + 1 as amount, |
| 97 | + md5((random() + 1)::text) as sku |
| 98 | +from demo.orders where id % 2 = 0; |
| 99 | + |
| 100 | +-- collect statistics |
| 101 | +vacuum analyze demo.orders, demo.order_item; |
| 102 | + |
| 103 | +-- update the status of several orders |
| 104 | +update demo.orders |
| 105 | +set status = 2 -- paid order |
| 106 | +where |
| 107 | + status = 1 -- new order |
| 108 | + and id in ( |
| 109 | + select id from demo.orders where id % 4 = 0 order by id limit 10000); |
| 110 | + |
| 111 | +update demo.order_item |
| 112 | +set warehouse_id = case when order_id % 8 = 0 then 1 else 2 end |
| 113 | +where |
| 114 | + warehouse_id is null |
| 115 | + and order_id in ( |
| 116 | + select id from demo.orders |
| 117 | + where |
| 118 | + status = 2 |
| 119 | + and created_at >= current_timestamp - interval '1 day'); |
| 120 | + |
| 121 | +-- collect statistics |
| 122 | +vacuum analyze demo.orders, demo.order_item; |
| 123 | + |
| 124 | +-- For partitioned tables |
| 125 | + |
| 126 | +create table if not exists demo.orders_partitioned( |
| 127 | + id bigint not null generated always as identity, |
| 128 | + user_id bigint not null, |
| 129 | + shop_id bigint not null, |
| 130 | + status int not null, |
| 131 | + created_at timestamptz not null default current_timestamp, |
| 132 | + primary key (id, created_at) |
| 133 | +) partition by range (created_at); |
| 134 | + |
| 135 | +create table if not exists demo.orders_default |
| 136 | + partition of demo.orders_partitioned default; |
| 137 | + |
| 138 | +create table if not exists demo.order_item_partitioned( |
| 139 | + id bigint generated always as identity, |
| 140 | + order_id bigint not null, |
| 141 | + created_at timestamptz not null, |
| 142 | + price decimal(22, 2) not null default 0, |
| 143 | + amount int not null default 0, |
| 144 | + sku varchar(255) not null, |
| 145 | + warehouse_id int, |
| 146 | + primary key (id, created_at), |
| 147 | + constraint fk_order_item_order_id foreign key (order_id, created_at) |
| 148 | + references demo.orders_partitioned (id, created_at) |
| 149 | +) partition by range (created_at); |
| 150 | + |
| 151 | +create index if not exists idx_order_item_partitioned_order_id |
| 152 | + on demo.order_item_partitioned (order_id); |
| 153 | + |
| 154 | +create index if not exists idx_order_item_partitioned_warehouse_id_without_nulls |
| 155 | + on demo.order_item_partitioned (warehouse_id) where warehouse_id is not null; |
| 156 | + |
| 157 | +create table if not exists demo.order_item_default |
| 158 | + partition of demo.order_item_partitioned default; |
| 159 | + |
| 160 | +-- Populating with data |
| 161 | + |
| 162 | +insert into demo.orders_partitioned (user_id, shop_id, status) |
| 163 | +select (ids.id % 10) + 1 as user_id, |
| 164 | + (ids.id % 4) + 1 as shop_id, |
| 165 | + 1 as status |
| 166 | +from generate_series(1, 10000) ids (id); |
| 167 | + |
| 168 | +insert into demo.order_item_partitioned (order_id, created_at, price, amount, sku) |
| 169 | +select id as order_id, created_at, |
| 170 | + (random() + 1) * 1000.0 as price, |
| 171 | + (random() * 10) + 1 as amount, |
| 172 | + md5(random()::text) as sku |
| 173 | +from demo.orders_partitioned; |
| 174 | + |
| 175 | +insert into demo.order_item_partitioned (order_id, created_at, price, amount, sku) |
| 176 | +select id as order_id, created_at, |
| 177 | + (random() + 1) * 2000.0 as price, |
| 178 | + (random() * 5) + 1 as amount, |
| 179 | + md5((random() + 1)::text) as sku |
| 180 | +from demo.orders_partitioned |
| 181 | +where id % 2 = 0; |
| 182 | + |
| 183 | +-- collect statistics |
| 184 | +vacuum analyze demo.orders_partitioned, demo.order_item_partitioned; |
| 185 | + |
| 186 | +-- update the status of several orders |
| 187 | +update demo.orders_partitioned |
| 188 | +set status = 2 -- paid order |
| 189 | +where status = 1 -- new order |
| 190 | + and id in (select id |
| 191 | + from demo.orders_partitioned |
| 192 | + where id % 4 = 0 |
| 193 | + order by id |
| 194 | + limit 10000); |
| 195 | + |
| 196 | +update demo.order_item_partitioned |
| 197 | +set warehouse_id = case when order_id % 8 = 0 then 1 else 2 end |
| 198 | +where warehouse_id is null |
| 199 | + and order_id in (select id |
| 200 | + from demo.orders_partitioned |
| 201 | + where status = 2 |
| 202 | + and created_at >= current_timestamp - interval '1 day'); |
| 203 | + |
| 204 | +-- collect statistics |
| 205 | +vacuum analyze demo.orders_partitioned, demo.order_item_partitioned; |
| 206 | +``` |
| 207 | + |
| 208 | +## How to fix |
| 209 | + |
| 210 | +1. Regularly run cleanup (vacuum). Make sure autovacuum is working and its parameters are configured properly. |
| 211 | + Cleanup allows space on pages to be freed efficiently and reused for new row versions. |
| 212 | +2. If a particular table in the database can be locked for a long time (from several minutes to several hours depending on the table size), |
| 213 | + then it is acceptable to fully rebuild the table with the [vacuum full](https://www.postgresql.org/docs/18/sql-vacuum.html) command. |
| 214 | + This completely removes the bloat and frees up disk space. |
| 215 | +3. If a long lock and the associated downtime are not acceptable, |
| 216 | + then consider using the [pg_repack](https://github.com/reorg/pg_repack) extension. |
0 commit comments