100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
SQL & Relational Databases
60 minbeginner

Table partitioning — range, list and hash

Table partitioning divides a large logical table into smaller physical storage units called partitions, each containing a subset of the rows. From the application's perspective, the partitioned table appears as a single table — queries, inserts, and updates reference the parent table name. Internally, PostgreSQL routes each row to the correct child partition based on the partition key column's value. Partitioning enables three critical performance benefits: partition pruning (queries that filter on the partition key scan only relevant partitions), partition-wise aggregation (parallel computation across partitions), and efficient data lifecycle management (dropping an entire partition is instant, while deleting millions of rows is slow).

PostgreSQL offers three partitioning strategies. Range partitioning assigns rows to partitions based on a range of values on the partition key — typically a date range (one partition per month, one per year). List partitioning assigns rows based on a specific set of discrete values — one partition per country, one per format type. Hash partitioning distributes rows across a fixed number of partitions based on the hash of the partition key — providing even data distribution without natural range or category boundaries. Each strategy is correct for different data distribution patterns and query access patterns.

For data engineers, partitioning is a critical technique for managing very large tables (hundreds of millions to billions of rows) that would be impractical to maintain without it. Time-series fact tables — delivery logs, match events, transaction records — are the most common candidates for range partitioning by date. Dimension tables segmented by region or country benefit from list partitioning. Distributed user activity tables with uniform access patterns benefit from hash partitioning. Designing the correct partitioning strategy at schema creation time is far cheaper than repartitioning after a table has grown to billions of rows.

Analogy🏏Cricket
🏏 Think of it like cricket: A SELECT query is precisely how a selection committee picks a playing XI. FROM is the full list of centrally contracted players — the raw pool. WHERE is the fitness and eligibility screen: injured or unavailable players are removed before anyone debates merit, and the fewer names that survive this screen, the faster the meeting goes — exactly why a good WHERE clause matters more than anything downstream. ORDER BY is ranking the survivors by recent form, then by experience as the tiebreaker. LIMIT 11 takes the top of that ranked list and stops. The committee never ranks the entire national player pool and then discards thousands of names — and neither should your query force the database to sort millions of rows it will immediately throw away. The order of operations is the whole game: filter first, sort what remains, take only what you need.
Lesson 17 of 32
0% complete