Question presented to candidate: "Our events table has a few billion rows, and most queries only look at the last few days. Someone suggested partitioning it. What is table partitioning, how does it work in PostgreSQL, and what does it actually buy us โ and cost us?"
What a strong answer should cover:
- Definition: one logical table stored as several physical tables (partitions), each holding the rows for one range, list of values or hash bucket of a partition key; queries and inserts still target the parent
- The three methods (RANGE, LIST, HASH) and when each fits; a DEFAULT partition for rows that match no partition
- The real benefits: partition pruning (scanning only the partitions the
WHEREclause can match), cheap retention by detaching or dropping a whole partition instead of a hugeDELETE, smaller per-partition indexes and maintenance - The costs and rules: primary keys and unique constraints must include the partition key, queries without the key scan every partition, too many partitions slow planning, partitions must be created ahead of the data
- How it differs from sharding (partitions live on one server) and from indexing (an index does not make old data cheap to drop)
- Engine facts: PostgreSQL declarative partitioning since version 10 (HASH and DEFAULT since 11); MySQL partitioned InnoDB tables cannot have foreign keys; SQLite has no partitioning
Clarifying questions expected:
- Which column do almost all queries filter on, and how is old data removed today?
- How many rows per day, and how long must data be kept?
Code / implementation expected: Optional โ a PARTITION BY RANGE table with monthly partitions, a pruned plan, and a detach-and-drop for retention.
Schema used by the examples: