Partition types (range, list, hash) in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using partitions in a database, it is important to understand how the time to find data grows as the table gets bigger.
We want to know how different partition types affect the speed of searching and inserting data.
Analyze the time complexity of querying data from a partitioned table using different partition types.
-- Create a range partitioned table
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
sale_date DATE NOT NULL,
amount NUMERIC
) PARTITION BY RANGE (sale_date);
-- Create partitions for each year
CREATE TABLE sales_2022 PARTITION OF sales
FOR VALUES FROM ('2022-01-01') TO ('2023-01-01');
CREATE TABLE sales_2023 PARTITION OF sales
FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
-- Query data for a specific date
SELECT * FROM sales WHERE sale_date = '2023-06-15';
This code sets up a range partition on sale_date and queries data for one date.
Look at what repeats when the database searches or inserts data.
- Primary operation: Checking which partition holds the data by comparing partition keys.
- How many times: Depends on the number of partitions; the system checks partitions until it finds the right one.
As the number of partitions grows, the time to find the right partition changes differently for each type.
| Input Size (Partitions) | Range/List Partition Checks | Hash Partition Checks |
|---|---|---|
| 10 | Up to 10 checks | 1 check (direct) |
| 100 | Up to 100 checks | 1 check (direct) |
| 1000 | Up to 1000 checks | 1 check (direct) |
Pattern observation: Range and list partitions may require checking many partitions, growing linearly with the number of partitions. Hash partitions use a formula to jump directly to the right partition, so checks stay constant.
Time Complexity: O(n) for range and list partitions, O(1) for hash partitions
This means range and list partitions take longer as partitions increase, but hash partitions find data quickly no matter how many partitions exist.
[X] Wrong: "All partition types find data equally fast regardless of partition count."
[OK] Correct: Range and list partitions may need to check many partitions one by one, so their search time grows with more partitions. Hash partitions use a direct calculation to find the right partition quickly.
Understanding how partition types affect query speed helps you design databases that stay fast as data grows. This skill shows you can think about real-world data challenges and choose the right tools.
"What if we added an index on the partition key columns? How would that affect the time complexity of searching within partitions?"
Practice
Solution
Step 1: Understand partition types
RANGE partitions split data into continuous ranges, like dates or numeric intervals.Step 2: Match partition type to use case
Since the question asks about continuous ranges, RANGE partitioning fits best.Final Answer:
RANGE partitioning -> Option CQuick Check:
Continuous ranges = RANGE partitioning [OK]
- Confusing LIST with RANGE for continuous data
- Thinking HASH is for ordered ranges
- Assuming NONE is a valid partition type
Solution
Step 1: Identify correct PARTITION BY syntax
PostgreSQL syntax requires PARTITION BY followed by partition type and column in parentheses after table columns.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.Final Answer:
CREATE TABLE sales (id INT, region TEXT) PARTITION BY LIST (region); -> Option AQuick Check:
Correct syntax = CREATE TABLE sales (id INT, region TEXT) PARTITION BY LIST (region); [OK]
- Placing PARTITION BY before column definitions
- Using wrong partition type for LIST
- Missing parentheses around partition column
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?
Solution
Step 1: Understand RANGE partition boundaries
The orders_2023 partition accepts dates from 2023-01-01 up to but not including 2024-01-01.Step 2: Check the inserted date '2022-12-31'
This date is before the partition range, so no matching partition exists for it.Step 3: Behavior on no matching partition
PostgreSQL rejects inserts that don't fit any partition unless a default partition exists (none here).Final Answer:
The row is rejected with a constraint violation error -> Option BQuick Check:
Out-of-range insert = error [OK]
- Assuming automatic default partition insertion
- Thinking parent table stores unmatched rows
- Ignoring partition range boundaries
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');?Solution
Step 1: Check defined partitions
Partitions exist only for 'Sales' and 'HR' departments.Step 2: Check inserted value 'Marketing'
'Marketing' is not listed in any partition's VALUES list.Step 3: PostgreSQL behavior on unmatched LIST value
Without a default partition, insert fails with no matching partition error.Final Answer:
No partition found for value 'Marketing', insert fails -> Option AQuick Check:
Unlisted LIST value = insert failure [OK]
- Assuming insert goes to any partition by default
- Expecting parent table to store unmatched rows
- Confusing syntax error with runtime insert error
Solution
Step 1: Understand partitioning goals
The goal is even distribution across 4 partitions without caring about value ranges.Step 2: Match partition type to goal
HASH partitioning evenly distributes rows based on a hash function, ideal for this case.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.Final Answer:
Use HASH partitioning with 4 partitions. -> Option DQuick Check:
Even distribution = HASH partitioning [OK]
- Using RANGE or LIST when no value grouping needed
- Thinking indexes replace partitioning benefits
- Confusing HASH with LIST partitioning
