0
0
PostgreSQLquery~20 mins

Hash partitioning for distribution in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hash Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of hash partition selection
Given a table users partitioned by hash on user_id into 4 partitions, what is the output of the following query?

SELECT tableoid::regclass, user_id FROM users WHERE user_id = 7;

Assume user_id 7 is inserted.
PostgreSQL
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);

INSERT INTO users VALUES (7, 'Alice');

SELECT tableoid::regclass, user_id FROM users WHERE user_id = 7;
Ausers_p0 | 7
Busers_p1 | 7
Cusers_p2 | 7
Dusers_p3 | 7
Attempts:
2 left
💡 Hint
Hash partitioning uses the hash of the key modulo the number of partitions to decide placement.
🧠 Conceptual
intermediate
1:30remaining
Understanding hash partition modulus and remainder
In PostgreSQL hash partitioning, what do the MODULUS and REMAINDER values specify for a partition?
AMODULUS is the hash function used; REMAINDER is the key column name
BMODULUS is the maximum value of the key; REMAINDER is the minimum value of the key
CMODULUS is the total number of partitions; REMAINDER is the partition number for rows where hash(key) % MODULUS = REMAINDER
DMODULUS is the number of rows per partition; REMAINDER is the partition size in MB
Attempts:
2 left
💡 Hint
Think about how hash(key) % number_of_partitions decides placement.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in hash partition creation
Which option contains a syntax error when creating a hash partition in PostgreSQL?
PostgreSQL
CREATE TABLE sales (id INT, amount NUMERIC) PARTITION BY HASH (id);

-- Partition creation options below
ACREATE TABLE sales_p3 PARTITION OF sales FOR VALUES WITH (MODULUS 4, REMAINDER 3);
BCREATE TABLE sales_p1 PARTITION OF sales FOR VALUES WITH (MODULUS 4 REMAINDER 1);
CCREATE TABLE sales_p2 PARTITION OF sales FOR VALUES WITH (MODULUS 4, REMAINDER 2);
DCREATE TABLE sales_p0 PARTITION OF sales FOR VALUES WITH (MODULUS 4, REMAINDER 0);
Attempts:
2 left
💡 Hint
Check punctuation between MODULUS and REMAINDER.
optimization
advanced
2:00remaining
Optimizing query performance on hash partitioned table
You have a hash partitioned table orders on order_id with 8 partitions. Which query will most efficiently retrieve the row with order_id = 12345?
ASELECT * FROM orders WHERE order_id = 12345;
BSELECT * FROM orders WHERE order_id IN (SELECT order_id FROM orders WHERE order_id = 12345);
CSELECT * FROM orders WHERE order_id % 8 = 1 AND order_id = 12345;
DSELECT * FROM orders WHERE order_id > 10000;
Attempts:
2 left
💡 Hint
PostgreSQL automatically routes queries on partition keys to the correct partition.
🔧 Debug
expert
2:30remaining
Why does this hash partitioned table insert fail?
Given the table and partitions:

CREATE TABLE logs (log_id INT, message TEXT) PARTITION BY HASH (log_id);
CREATE TABLE logs_p0 PARTITION OF logs FOR VALUES WITH (MODULUS 3, REMAINDER 0);
CREATE TABLE logs_p1 PARTITION OF logs FOR VALUES WITH (MODULUS 3, REMAINDER 1);
CREATE TABLE logs_p2 PARTITION OF logs FOR VALUES WITH (MODULUS 3, REMAINDER 2);

Why does INSERT INTO logs VALUES (5, 'error'); fail?
APartition logs_p2 has a different MODULUS (4) than the parent table's MODULUS (3), causing a partition mismatch error.
BThe value 5 is out of range for all partitions, so no partition matches.
CThe INSERT statement is missing a column list, causing a syntax error.
DPartition logs_p1 is missing, so the insert fails.
Attempts:
2 left
💡 Hint
All partitions must have the same MODULUS value for hash partitioning.