---
title: "Check Object Sizes"
description: "Measure the disk space used by databases, tables, partitions, indexes, TOAST data and dead rows on a Managed PostgreSQL service with SQL."
url: https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/check-object-sizes/
product: "PostgreSQL"
section: product
last_updated: 2026-11-10
---
> For AI agents: the documentation index is at https://community.exoscale.com/llms.txt. Every page is available as markdown at `<page URL>index.md` or with `Accept: text/markdown`.

# Check Object Sizes

Measure how much space databases, tables, partitions, indexes, TOAST storage and dead rows occupy on your Managed PostgreSQL service.

Every size function returns bytes. Wrap it in `pg_size_pretty()` for display, but keep the raw
function in `ORDER BY`: sorting the text output puts `9 MB` after `10 GB`.

## Measure Database Size

```sql
SELECT datname                                   AS database_name,
       pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
WHERE datistemplate = false
ORDER BY pg_database_size(datname) DESC;
```

`avnadmin` can measure every database, including those it cannot connect to, because it is a
member of `pg_read_all_stats`. `defaultdb` is the database created with the service. `_aiven` is
used by the platform and must not be modified.

The sum of the database sizes is smaller than the disk space the service uses: WAL and temporary
files live on the same disk. For the whole picture, see
[Disk usage](https://community.exoscale.com/product/dbaas/service-specific/postgresql/overview/disk-usage/index.md).

## Measure Table Size

Four functions answer four different questions:

| Function | What it counts |
| --- | --- |
| `pg_relation_size(oid)` | The main data file of the relation only. No indexes, no TOAST. |
| `pg_table_size(oid)` | Main data, TOAST table, free space map and visibility map. No indexes. |
| `pg_indexes_size(oid)` | All indexes of the table. |
| `pg_total_relation_size(oid)` | `pg_table_size` plus `pg_indexes_size`. Use this one for "how big is this table". |

The twenty largest tables and materialized views of the current database:

```sql
SELECT n.nspname                                     AS schema_name,
       c.relname                                     AS table_name,
       pg_size_pretty(pg_total_relation_size(c.oid)) AS total,
       pg_size_pretty(pg_table_size(c.oid))          AS table_data,
       pg_size_pretty(pg_indexes_size(c.oid))        AS indexes,
       c.reltuples::bigint                           AS estimated_rows
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'm')
  AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(c.oid) DESC
LIMIT 20;
```

`reltuples` is the row estimate maintained by `VACUUM` and `ANALYZE`, not an exact count. For a
single table:

```sql
SELECT pg_size_pretty(pg_total_relation_size('public.orders'));
```

In `psql`, `\dt+ TABLE_NAME` shows the size reported by `pg_table_size()`, without indexes, and
`\l+` shows the size of each database.

### Partitioned Tables

A partitioned table stores no data itself: `pg_total_relation_size()` on the parent returns
`0 bytes`. Sum the whole partition tree instead, which also covers sub-partitions:

```sql
SELECT pg_size_pretty(sum(pg_total_relation_size(relid))) AS partitions_total
FROM pg_partition_tree('public.events');
```

Summing only the direct children listed in `pg_inherits` misses the data of sub-partitions.

## Measure Index Size

Per index, with the number of scans since statistics were last reset:

```sql
SELECT s.schemaname                                   AS schema_name,
       s.relname                                      AS table_name,
       s.indexrelname                                 AS index_name,
       pg_size_pretty(pg_relation_size(s.indexrelid)) AS index_size,
       s.idx_scan                                     AS index_scans
FROM pg_stat_user_indexes s
ORDER BY pg_relation_size(s.indexrelid) DESC
LIMIT 20;
```

Share of each table's footprint taken by indexes:

```sql
SELECT n.nspname                              AS schema_name,
       c.relname                              AS table_name,
       pg_size_pretty(pg_table_size(c.oid))   AS table_data,
       pg_size_pretty(pg_indexes_size(c.oid)) AS indexes,
       round(100.0 * pg_indexes_size(c.oid) / NULLIF(pg_table_size(c.oid), 0), 1) AS index_pct
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
  AND n.nspname NOT IN ('pg_catalog', 'information_schema')
  AND pg_table_size(c.oid) > 0
ORDER BY pg_indexes_size(c.oid) DESC
LIMIT 20;
```

To find indexes that are never used and can be dropped, see
[Optimize slow queries](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/optimize-slow-queries/index.md#drop-indexes-you-do-not-use).

## Measure TOAST Storage

Values too large to fit comfortably in a page are compressed and, if still too large, moved to the
table's TOAST table. `pg_table_size()` and `pg_total_relation_size()` include it,
`pg_relation_size()` does not:

```sql
SELECT n.nspname                                     AS schema_name,
       c.relname                                     AS table_name,
       pg_size_pretty(pg_relation_size(c.oid))       AS main_data,
       pg_size_pretty(pg_total_relation_size(t.oid)) AS toast_size
FROM pg_class c
JOIN pg_class t ON t.oid = c.reltoastrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(t.oid) DESC
LIMIT 10;
```

The compression method for new values is `default_toast_compression`, `lz4` on Managed PostgreSQL.
It can be changed for the service in `--pg-settings` (`lz4` or `pglz`) or for a session with `SET`.
Values already stored keep the method they were written with.

## Estimate Dead Rows

An `UPDATE` writes a new row version and a `DELETE` only marks the row dead. `VACUUM` makes that
space reusable by the same table, but usually does not shrink the files. The statistics give a
cheap estimate:

```sql
SELECT s.schemaname                                    AS schema_name,
       s.relname                                       AS table_name,
       s.n_live_tup                                    AS live_rows,
       s.n_dead_tup                                    AS dead_rows,
       round(100.0 * s.n_dead_tup / NULLIF(s.n_live_tup + s.n_dead_tup, 0), 1) AS dead_pct,
       pg_size_pretty(pg_total_relation_size(s.relid)) AS total_size,
       s.last_vacuum,
       s.last_autovacuum
FROM pg_stat_user_tables s
WHERE s.n_dead_tup > 0
ORDER BY s.n_dead_tup DESC
LIMIT 20;
```

A high `dead_pct` that does not come down after autovacuum runs usually means that a long open
transaction prevents cleanup. See
[Manage long-running queries](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-long-running-queries/index.md).

### Measure Exactly with pgstattuple

`pgstattuple` scans the relation and reports the real figures. `avnadmin` can create the extension
and call its functions:

```sql
CREATE EXTENSION IF NOT EXISTS pgstattuple;

SELECT pg_size_pretty(table_len)             AS total_size,
       tuple_count                           AS live_rows,
       round(tuple_percent::numeric, 1)      AS live_pct,
       dead_tuple_count                      AS dead_rows,
       round(dead_tuple_percent::numeric, 1) AS dead_pct,
       pg_size_pretty(free_space)            AS reusable_space
FROM pgstattuple('public.orders');
```

`pgstattuple_approx('public.orders')` skips the pages the visibility map marks as all-visible and
is much cheaper on a large, mostly static table. For B-tree indexes, `pgstatindex()` reports
`avg_leaf_density`, see
[Repair and rebuild indexes](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/repair-indexes/index.md#measure-index-bloat).

> [!WARNING]
> `pgstattuple()` and `pgstatindex()` read the whole relation. On a large table that is a lot of
> I/O and it displaces useful pages from the cache. Run them outside peak hours.

## Reclaim Space

| Operation | Shrinks the files | Blocks the table |
| --- | --- | --- |
| `VACUUM` | No, space becomes reusable by the same table | No |
| `VACUUM FULL` | Yes, the table and its indexes are rewritten | Yes, `ACCESS EXCLUSIVE` for the whole run |
| `REINDEX ... CONCURRENTLY` | Yes, for the rebuilt indexes | No, apart from short locks |
| `pg_repack` | Yes, the table is rewritten | Only briefly at the start and the end |

```sql
VACUUM (ANALYZE, VERBOSE) public.orders;
REINDEX TABLE CONCURRENTLY public.orders;
```

`VACUUM FULL`, `REINDEX` and `pg_repack` all write a complete new copy before releasing the old
one, so they need free disk roughly equal to the size of what they rebuild. For the procedures, see
[Repair and rebuild indexes](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/repair-indexes/index.md) and
[Use pg_repack](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/use-pg-repack/index.md).

To keep bloat from building up in the first place, make autovacuum visit large, busy tables more
often. The service-wide `autovacuum_vacuum_scale_factor` (default `0.2`, set in the `autovacuum`
object of `--pg-settings`) means a table is vacuumed once about 20% of its rows have changed. A
per-table override applies without a restart:

```sql
ALTER TABLE public.orders SET (autovacuum_vacuum_scale_factor = 0.02);
```

## Related Pages

- [Disk usage](https://community.exoscale.com/product/dbaas/service-specific/postgresql/overview/disk-usage/index.md)
- [Prevent a full disk on Managed PostgreSQL](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/prevent-full-disk/index.md)
- [Optimize slow queries](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/optimize-slow-queries/index.md)
- [Repair and rebuild indexes](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/repair-indexes/index.md)
- [Use pg_repack](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/use-pg-repack/index.md)
- [Database object size functions in the PostgreSQL documentation](https://www.postgresql.org/docs/18/functions-admin.html#FUNCTIONS-ADMIN-DBOBJECT) ↗

