0Pricing
SQL Academy · Lesson

Capacity Planning and Bloat Audits

Forecast disk and IOPS growth, audit table and index bloat regularly, and plan upgrades before you run out.

Capacity Planning and Bloat Audits is a free SQL Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What to Forecast

To size your database for the next 6–12 months you need to project:

  • Disk usage (data + WAL + indexes)
  • IOPS demand
  • RAM working set
  • Connection count

Disk Growth

Trend recent growth:

SELECT pg_size_pretty(pg_database_size(current_database()));
SELECT pg_size_pretty(pg_total_relation_size(t.oid)) AS total,
       relname
FROM pg_class t
WHERE relkind = 'r'
ORDER BY pg_total_relation_size(t.oid) DESC LIMIT 20;

Per-Table Growth Tracking

Schedule a metrics-logging job and graph it:

INSERT INTO size_history (ts, tablename, size_bytes)
SELECT NOW(), relname, pg_total_relation_size(oid)
FROM pg_class WHERE relkind = 'r';

IOPS Estimation

Read I/O hot tables surface in pg_stat_user_tables:

SELECT relname, seq_tup_read, idx_tup_fetch,
       seq_tup_read + idx_tup_fetch AS total_reads
FROM pg_stat_user_tables
ORDER BY total_reads DESC LIMIT 20;

RAM Sizing

shared_buffers ≈ 25% of RAM. effective_cache_size ≈ 75% (planner hint, not allocation). work_mem per connection × connections shouldn't exceed available RAM.

Bloat Audit

Find the biggest dead-row offenders:

SELECT relname,
       n_live_tup,
       n_dead_tup,
       round(n_dead_tup::numeric / NULLIF(n_live_tup, 0), 2) AS dead_ratio,
       last_autovacuum
FROM pg_stat_user_tables
ORDER BY n_dead_tup DESC
LIMIT 20;

Index Bloat

Use pgstattuple or built-in tools:

CREATE EXTENSION pgstattuple;

SELECT relname,
       pg_size_pretty(pg_relation_size(indexrelid)) AS size,
       (pgstatindex(indexrelid::regclass)).leaf_fragmentation
FROM pg_stat_user_indexes
ORDER BY pg_relation_size(indexrelid) DESC LIMIT 20;

Unused Indexes

Find and drop them — they cost writes:

SELECT schemaname, relname, indexrelname,
       pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_user_indexes
WHERE idx_scan = 0
  AND indexrelname NOT LIKE '%_pkey'
ORDER BY pg_relation_size(indexrelid) DESC;

Connection Audit

How many clients are connected, what state:

SELECT datname, usename, application_name, state, COUNT(*)
FROM pg_stat_activity
GROUP BY 1,2,3,4 ORDER BY 5 DESC;

Long Transactions

The cause of vacuum starvation:

SELECT pid, state, xact_start, NOW() - xact_start AS xact_age, query
FROM pg_stat_activity
WHERE xact_start IS NOT NULL
ORDER BY xact_age DESC NULLS LAST LIMIT 20;

WAL and Archives

Monitor WAL generation rate to size archive storage:

SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), '0/0')) AS total_wal_generated;

Plan for Failover

Replica disk should match primary. Verify replica catch-up rate ≥ primary write rate.

Recap

Capacity planning is graphing the right metrics.

  • Per-table size growth
  • Dead-row ratio
  • Unused indexes
  • Long transactions blocking vacuum
  • Connection counts

Quick Check

Which view do you query to find the biggest dead-row offenders for vacuum planning?

Frequently asked questions

Is the “Capacity Planning and Bloat Audits” lesson free?

Yes — the full text of “Capacity Planning and Bloat Audits” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “Capacity Planning and Bloat Audits”?

Forecast disk and IOPS growth, audit table and index bloat regularly, and plan upgrades before you run out. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Capacity Planning and Bloat Audits” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. pg_stat_statements: Top Queries
  2. pgBadger for Log Analysis
  3. Connection Pooling: PgBouncer
  4. Capacity Planning and Bloat Audits
← Back to SQL Academy