🐘 PostgreSQL Intermediate

What are table inheritance and partitioning in PostgreSQL?

Answer

Table partitioning (PostgreSQL 10+ declarative) splits a large table into smaller physical pieces while appearing as one logical table. Types: PARTITION BY RANGE (date ranges — most common), PARTITION BY LIST (specific values per partition — e.g., by country), PARTITION BY HASH (distribute evenly by hash). Create: CREATE TABLE orders (id BIGINT, order_date DATE, ...) PARTITION BY RANGE (order_date);. Add partition: CREATE TABLE orders_2024 PARTITION OF orders FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');. Benefits: partition pruning (query only relevant partitions), faster bulk deletes (DROP TABLE partition vs DELETE), parallel query per partition, improved vacuum efficiency. Index each partition independently.