Bird
Raised Fist0
PostgreSQLquery~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which partition type in PostgreSQL is best suited for dividing a table based on continuous ranges of values, such as dates or numbers?
easy
A. HASH partitioning
B. LIST partitioning
C. RANGE partitioning
D. NONE partitioning

Solution

  1. Step 1: Understand partition types

    RANGE partitions split data into continuous ranges, like dates or numeric intervals.
  2. Step 2: Match partition type to use case

    Since the question asks about continuous ranges, RANGE partitioning fits best.
  3. Final Answer:

    RANGE partitioning -> Option C
  4. Quick Check:

    Continuous ranges = RANGE partitioning [OK]
Hint: Continuous values? Choose RANGE partitioning [OK]
Common Mistakes:
  • Confusing LIST with RANGE for continuous data
  • Thinking HASH is for ordered ranges
  • Assuming NONE is a valid partition type
2. Which of the following is the correct syntax to create a LIST partitioned table in PostgreSQL?
easy
A. CREATE TABLE sales (id INT, region TEXT) PARTITION BY LIST (region);
B. CREATE TABLE sales PARTITION BY LIST region (id INT, region TEXT);
C. CREATE TABLE sales (id INT, region TEXT) PARTITION BY RANGE (region);
D. CREATE TABLE sales (id INT, region TEXT) PARTITION BY HASH (region);

Solution

  1. Step 1: Identify correct PARTITION BY syntax

    PostgreSQL syntax requires PARTITION BY followed by partition type and column in parentheses after table columns.
  2. Step 2: Check each option

    CREATE TABLE sales (id INT, region TEXT) PARTITION BY LIST (region); uses correct syntax: columns first, then PARTITION BY LIST (region). Options A, B, C have syntax errors or wrong partition type.
  3. Final Answer:

    CREATE TABLE sales (id INT, region TEXT) PARTITION BY LIST (region); -> Option A
  4. Quick Check:

    Correct syntax = CREATE TABLE sales (id INT, region TEXT) PARTITION BY LIST (region); [OK]
Hint: PARTITION BY type (column) after columns [OK]
Common Mistakes:
  • Placing PARTITION BY before column definitions
  • Using wrong partition type for LIST
  • Missing parentheses around partition column
3. Given the following partitioned table and inserts:
CREATE TABLE orders (
  order_id INT,
  order_date DATE
) PARTITION BY RANGE (order_date);

CREATE TABLE orders_2023 PARTITION OF orders
  FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

INSERT INTO orders VALUES (1, '2023-06-15');
INSERT INTO orders VALUES (2, '2022-12-31');

What will happen when the second insert is executed?
medium
A. The row is inserted into orders_2023 partition
B. The row is rejected with a constraint violation error
C. The row is inserted into a default partition automatically
D. The row is inserted into the parent table without partition

Solution

  1. Step 1: Understand RANGE partition boundaries

    The orders_2023 partition accepts dates from 2023-01-01 up to but not including 2024-01-01.
  2. Step 2: Check the inserted date '2022-12-31'

    This date is before the partition range, so no matching partition exists for it.
  3. Step 3: Behavior on no matching partition

    PostgreSQL rejects inserts that don't fit any partition unless a default partition exists (none here).
  4. Final Answer:

    The row is rejected with a constraint violation error -> Option B
  5. Quick Check:

    Out-of-range insert = error [OK]
Hint: Out-of-range insert without default partition causes error [OK]
Common Mistakes:
  • Assuming automatic default partition insertion
  • Thinking parent table stores unmatched rows
  • Ignoring partition range boundaries
4. Consider this partitioned table creation:
CREATE TABLE employees (
  emp_id INT,
  department TEXT
) PARTITION BY LIST (department);

CREATE TABLE employees_sales PARTITION OF employees FOR VALUES IN ('Sales');
CREATE TABLE employees_hr PARTITION OF employees FOR VALUES IN ('HR');

Which error will occur if you try to insert INSERT INTO employees VALUES (1, 'Marketing');?
medium
A. No partition found for value 'Marketing', insert fails
B. Syntax error due to missing partition
C. Row inserted into employees_sales partition by default
D. Row inserted into parent table without partition

Solution

  1. Step 1: Check defined partitions

    Partitions exist only for 'Sales' and 'HR' departments.
  2. Step 2: Check inserted value 'Marketing'

    'Marketing' is not listed in any partition's VALUES list.
  3. Step 3: PostgreSQL behavior on unmatched LIST value

    Without a default partition, insert fails with no matching partition error.
  4. Final Answer:

    No partition found for value 'Marketing', insert fails -> Option A
  5. Quick Check:

    Unlisted LIST value = insert failure [OK]
Hint: LIST partition needs matching value or default partition [OK]
Common Mistakes:
  • Assuming insert goes to any partition by default
  • Expecting parent table to store unmatched rows
  • Confusing syntax error with runtime insert error
5. You want to evenly distribute a large table's rows across 4 partitions to improve query performance without caring about specific value ranges. Which partition type and setup is best in PostgreSQL?
hard
A. Use no partitioning and rely on indexes.
B. Use LIST partitioning with 4 specific values.
C. Use RANGE partitioning on a numeric column with 4 ranges.
D. Use HASH partitioning with 4 partitions.

Solution

  1. Step 1: Understand partitioning goals

    The goal is even distribution across 4 partitions without caring about value ranges.
  2. Step 2: Match partition type to goal

    HASH partitioning evenly distributes rows based on a hash function, ideal for this case.
  3. Step 3: Evaluate other options

    RANGE and LIST require specific ranges or values, not suitable for even spread without criteria. No partitioning misses distribution benefits.
  4. Final Answer:

    Use HASH partitioning with 4 partitions. -> Option D
  5. Quick Check:

    Even distribution = HASH partitioning [OK]
Hint: Even spread without ranges? Choose HASH partitioning [OK]
Common Mistakes:
  • Using RANGE or LIST when no value grouping needed
  • Thinking indexes replace partitioning benefits
  • Confusing HASH with LIST partitioning