Bird
Raised Fist0
PostgreSQLquery~20 mins

Partition types (range, list, hash) in PostgreSQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Partition Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of range partition query
Given a table sales partitioned by range on sale_date, what rows will be returned by this query?

SELECT * FROM sales WHERE sale_date < '2023-01-01';

Assume partitions are:
- sales_2022 for dates < '2023-01-01'
- sales_2023 for dates >= '2023-01-01'
PostgreSQL
CREATE TABLE sales (
  id SERIAL PRIMARY KEY,
  sale_date DATE NOT NULL,
  amount NUMERIC
) PARTITION BY RANGE (sale_date);

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');

-- Sample data inserted in sales_2022 and sales_2023 partitions
-- Query:
SELECT * FROM sales WHERE sale_date < '2023-01-01';
AReturns no rows because the query is invalid for partitioned tables
BReturns all rows with sale_date in 2023 only, from sales_2023 partition
CReturns all rows with sale_date in 2022 only, from sales_2022 partition
DReturns all rows from both partitions regardless of sale_date
Attempts:
2 left
💡 Hint
Think about how range partitions split data by date ranges.
🧠 Conceptual
intermediate
1:30remaining
Identifying partition type by definition
Which partition type is described by this definition?

"Data is divided into partitions based on a list of discrete values for a column."
ARange partitioning
BList partitioning
CHash partitioning
DComposite partitioning
Attempts:
2 left
💡 Hint
Think about partitions defined by specific values, not ranges or hashes.
📝 Syntax
advanced
2:30remaining
Correct syntax for hash partition creation
Which option shows the correct syntax to create a hash partitioned table users on column user_id with 4 partitions in PostgreSQL?
A
CREATE TABLE users (user_id INT, name TEXT) PARTITION BY HASH (user_id);
CREATE TABLE users_p0 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 0);
CREATE TABLE users_p1 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 1);
CREATE TABLE users_p2 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 2);
CREATE TABLE users_p3 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 3);
B
CREATE TABLE users (user_id INT, name TEXT) PARTITION BY HASH (user_id);
CREATE TABLE users_p0 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 1);
C
CREATE TABLE users (user_id INT, name TEXT) PARTITION BY HASH (user_id);
CREATE TABLE users_p0 PARTITION OF users FOR VALUES IN (0,1,2,3);
D
CREATE TABLE users (user_id INT, name TEXT) PARTITION BY HASH (user_id);
CREATE TABLE users_p0 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 0);
Attempts:
2 left
💡 Hint
Hash partitions require one partition per remainder value from 0 to modulus-1.
optimization
advanced
2:00remaining
Best partition type for querying by month
You have a large orders table with a order_date column. Most queries filter by month. Which partition type will optimize query performance best?
ARange partitioning on order_date by month ranges
BHash partitioning on order_date
CList partitioning on order_date with each day as a list value
DNo partitioning, use indexing instead
Attempts:
2 left
💡 Hint
Think about how to group continuous time periods efficiently.
🔧 Debug
expert
2:30remaining
Why does this partition insert fail?
Given this table creation:

CREATE TABLE logs (id SERIAL, log_level TEXT, message TEXT) PARTITION BY LIST (log_level);

And this partition:

CREATE TABLE logs_info PARTITION OF logs FOR VALUES IN ('INFO', 'DEBUG');

Why does this INSERT fail?

INSERT INTO logs (log_level, message) VALUES ('ERROR', 'test');
ABecause 'ERROR' is not a valid log_level value in the table schema
BBecause the INSERT syntax is invalid for partitioned tables
CBecause the partition logs_info includes 'ERROR' but the INSERT filters it out
DBecause there is no partition defined for 'ERROR' values, so INSERT fails with no matching partition error
Attempts:
2 left
💡 Hint
Consider what happens when a value is not assigned to any partition in list partitioning.

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