0
0
PostgreSQLquery~20 mins

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

Choose your learning style9 modes available
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.