How do you implement partitioned tables efficiently in PostgreSQL?

Answer

Effective partitioning strategy: (1) Choose partition key wisely — most queries should filter by it to enable partition pruning (skipping irrelevant partitions). Range partitioning on created_at is most common for time-series data. (2) Create a default partition to catch out-of-range data: CREATE TABLE orders_default PARTITION OF orders DEFAULT;. (3) Create local indexes on each partition. (4) Use PARTITION PRUNING: verify with EXPLAIN that only relevant partitions are scanned. (5) Automate partition creation with pg_partman extension. (6) Drop old partitions instantly: DROP TABLE orders_2020 — far faster than DELETE. (7) Attach/detach partitions: ALTER TABLE orders DETACH PARTITION orders_2020 CONCURRENTLY; (v14+). Enable enable_partition_pruning = on (default). Use partition_pruning parameter in queries.