0
0
PostgreSQLquery~10 mins

Partition types (range, list, hash) in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Partition types (range, list, hash)
Start: Create Partitioned Table
Choose Partition Type
Range
Define Ranges
Create Partitions
Insert Data Routed to Correct Partition
Query Data Efficiently
You start by creating a partitioned table, then pick one partition type: range, list, or hash. Each type defines how data is split into partitions. Data inserts go to the right partition, making queries faster.
Execution Sample
PostgreSQL
CREATE TABLE sales (
  id INT,
  sale_date DATE,
  region TEXT
) PARTITION BY RANGE (sale_date);

CREATE TABLE sales_2023 PARTITION OF sales
  FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
This creates a sales table partitioned by date ranges, then creates a partition for all sales in 2023.
Execution Table
StepActionPartition TypePartition DefinitionData Routing ExampleResult
1Create main table with partitioningRangePARTITION BY RANGE (sale_date)N/ATable 'sales' created as partitioned
2Create partition for 2023RangeFOR VALUES FROM ('2023-01-01') TO ('2024-01-01')N/APartition 'sales_2023' created
3Insert row with sale_date = '2023-06-15'RangeN/AFalls into 2023 partitionRow stored in 'sales_2023'
4Create main table with list partitioningListPARTITION BY LIST (region)N/ATable 'customers' created as partitioned
5Create partition for region 'North'ListFOR VALUES IN ('North')N/APartition 'customers_north' created
6Insert row with region = 'North'ListN/AFalls into 'North' partitionRow stored in 'customers_north'
7Create main table with hash partitioningHashPARTITION BY HASH (id) PARTITIONS 4N/ATable 'orders' created as partitioned
8Create 4 hash partitionsHashFOR VALUES WITH (MODULUS 4, REMAINDER 0) FOR VALUES WITH (MODULUS 4, REMAINDER 1) FOR VALUES WITH (MODULUS 4, REMAINDER 2) FOR VALUES WITH (MODULUS 4, REMAINDER 3)N/APartitions 'orders_0' to 'orders_3' created
9Insert row with id = 7HashN/AHash(id) mod 4 = 3Row stored in 'orders_3'
10Query dataAllN/AQuery optimizer targets relevant partitionsFaster query execution
11EndN/AN/AN/APartitioning setup complete
💡 All partitions created and data routed correctly; queries can now use partitions for efficiency.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8Final
Partitioned TableNonesales (Range)sales, customers (Range, List)sales, customers, orders (Range, List, Hash)All 3 tables partitioned
Partitions Created01 (sales_2023)2 (sales_2023, customers_north)6 (sales_2023, customers_north, orders_0..orders_3)6 partitions total
Data Insert ExampleN/ARow with sale_date=2023-06-15 in sales_2023Row with region='North' in customers_northRow with id=7 in orders_3Rows routed to correct partitions
Key Moments - 3 Insights
Why do we need to define ranges or lists when creating partitions?
Because PostgreSQL needs clear rules to decide which partition each row belongs to. See execution_table rows 2 and 5 where partitions are defined with ranges or lists.
How does hash partitioning decide where to put data?
It uses a hash function on the partition key and divides by the number of partitions. For example, in row 9, id=7 goes to partition 3 because hash(7) mod 4 = 3.
Can data be stored outside partitions in a partitioned table?
No, all data must fit into one of the defined partitions. If no partition matches, insert fails. This is why defining partitions correctly is important.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 3. Where is a row with sale_date = '2023-06-15' stored?
AIn the partition 'sales_2023'
BIn the main table 'sales'
CIn a hash partition
DIt is rejected because no partition matches
💡 Hint
Check the 'Data Routing Example' and 'Result' columns in row 3 of execution_table.
According to variable_tracker, how many partitions exist after step 8?
A3
B6
C4
D8
💡 Hint
Look at the 'Partitions Created' row after step 8 in variable_tracker.
If you insert a row with id=10 into the hash partitioned table with 4 partitions, which partition will it go to?
Aorders_4
Borders_3
Corders_2
Dorders_1
💡 Hint
Recall hash partitioning uses hash(id) mod number_of_partitions; see example in execution_table row 9.
Concept Snapshot
Partition types in PostgreSQL:
- RANGE: splits data by ranges of values (e.g., dates)
- LIST: splits data by specific values (e.g., regions)
- HASH: splits data by hashing a key (e.g., id)
Define partitions with FOR VALUES clause.
Data inserts go to matching partition.
Queries can target partitions for speed.
Full Transcript
Partitioning in PostgreSQL means splitting a big table into smaller pieces called partitions. You first create a main table and choose a partition type: range, list, or hash. Range partitions split data by value ranges, like dates. List partitions split by specific values, like regions. Hash partitions split by a hash function on a key, like an id. You create partitions with rules that tell PostgreSQL where to put data. When you insert data, PostgreSQL routes it to the right partition. This helps queries run faster because they only look at relevant partitions. The execution table shows steps creating tables and partitions, inserting data, and how data is routed. The variable tracker shows how many partitions exist and where data goes. Key moments explain why defining partitions is important and how hash partitioning works. The quiz tests understanding of data routing and partition counts. The snapshot summarizes the types and usage of partitions.