6 tips to optimize storage costs for your Postgres databases
Six ways to shrink Postgres storage spend: drop unused indexes (found via pg_stat_user_indexes and pg_index), avoid full database copies for dev and staging by using Neon's copy-on-write branching, choose smaller data types like SMALLINT, order columns largest to smallest to cut alignment padding, shorten Neon's history retention window, and tune autovacuum to limit bloat. Shorter retention saves storage but narrows the point-in-time recovery window. For bloat, look in pg_stat_activity for long-running transactions that block autovacuum.
Related posts
- Postgres Bloat Minimization
Supabase mediumAdeptPostgres stores table rows in 8KB heap pages and writes a new row version on every update, so dead versions accumulate until vacuum reclaims them and updates the free-space and visibility maps. Bloat usually means autovacuum never started, because the default 20% scale factor is too high for large tables, or it started but couldn't finish because of long-running idle transactions or SHARE UPDATE EXCLUSIVE locks. The fixes include lowering per-table scale factors, setting idle_in_transaction_session_timeout, cancelling stuck backends, and raising autovacuum_max_workers or autovacuum_work_mem. VACUUM FULL or pg_repack returns space to the filesystem after mass deletes; VACUUM FULL takes an exclusive lock and pg_repack does not.
- What gets TOASTed in Postgres
PlanetScale mediumAdeptPostgres TOAST kicks in once a row exceeds 2,032 bytes, compressing variable-length values largest-first and moving them into chunked pg_toast tables (1,996-byte chunks, 18-byte in-row pointers) when compression isn't enough. Column storage strategies (PLAIN, EXTENDED, EXTERNAL, MAIN) control which of those happens, and TOAST differs from large objects in automation, the 1 GB limit, and deletion semantics. Comparing pglz (4 KB window, requires 25% savings) with LZ4 (64 KB window, single-candidate lookup, much faster) shows LZ4 catches more distant repeats and loads faster, while pglz compressed repetitive JSON smaller (19 vs 24 MB) and serves prefix reads like left(body, 200) far faster because Postgres can bound how many chunks it needs. EXTERNAL is the better choice for already-compressed data and for bytea values read from the middle (0.6 ms vs 43 ms LZ4 and 116 ms pglz).
- Performance tips for Neon Postgres
Neon mediumAdept
Neon's serverless Postgres follows mostly standard Postgres tuning, plus a few Neon-specific checks. Size compute at roughly 100 connections per GB of RAM, use pooling and autoscaling, then find slow queries with pg_stat_statements and EXPLAIN (ANALYZE, BUFFERS). Fix them with indexes, REINDEX and autovacuum tuning against bloat, and by keeping the local file cache hit ratio (shown in neon_stat_file_cache) above 99%. For faster ingests, batch inserts, drop unused indexes, offload reads to ephemeral read replicas, and test changes safely on a database branch.
- Postgres Support Recap: Investigating Postgres Query Performance
Neon mediumAdept
Three starting points for diagnosing slow Postgres queries: use pg_stat_statements to find long-running queries by mean execution time and then EXPLAIN ANALYZE them, check the cache hit ratio (in Neon via the neon_stat_file_cache view, since Neon extends shared buffers with a local file cache), and look for table and index bloat caused by MVCC dead tuples. A hit ratio below 99% suggests the working set doesn't fit in memory, which calls for more shared_buffers or a larger Neon compute. Bloat is reduced with VACUUM, VACUUM FULL or REINDEX, all of which take locks. PgHero, run via Docker, gives a free dashboard alternative.
- Anatomy of a (Postgres) search engine
PlanetScale mediumAdeptInverted indexes power full-text search by mapping each term to a sorted, compressed postings list of document IDs, with optional positional data for phrase/span queries and frequency statistics for BM25 scoring. Postings can be delta-encoded or stored as bitmaps for fast SIMD intersection, and block-level statistics let top-k queries skip whole blocks. Indexes are built as immutable segments plus a mutable segment, with deletes handled by tombstones and periodic merges that trade heavy I/O against per-query overhead. Inside Postgres, such an index must map to ctids, be WAL-logged, reuse freed pages, run merges in background workers, and cooperate with VACUUM.