Challenge - 5 Problems
Hash Partitioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of hash partition selection
Given a table
Assume
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;
Attempts:
2 left
💡 Hint
Hash partitioning uses the hash of the key modulo the number of partitions to decide placement.
✗ Incorrect
The hash of 7 modulo 4 is 3, so the row goes to partition users_p3. The query shows the partition table name and user_id.
🧠 Conceptual
intermediate1:30remaining
Understanding hash partition modulus and remainder
In PostgreSQL hash partitioning, what do the
MODULUS and REMAINDER values specify for a partition?Attempts:
2 left
💡 Hint
Think about how hash(key) % number_of_partitions decides placement.
✗ Incorrect
MODULUS defines how many partitions exist. REMAINDER defines which partition a row belongs to based on the hash value modulo MODULUS.
📝 Syntax
advanced2: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
Attempts:
2 left
💡 Hint
Check punctuation between MODULUS and REMAINDER.
✗ Incorrect
Option B is missing a comma between MODULUS 4 and REMAINDER 1, causing a syntax error.
❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
PostgreSQL automatically routes queries on partition keys to the correct partition.
✗ Incorrect
Query C uses the partition key in WHERE clause, so PostgreSQL prunes partitions and queries only the relevant one. Option A is redundant and less efficient. Options A and D scan multiple partitions.
🔧 Debug
expert2:30remaining
Why does this hash partitioned table insert fail?
Given the table and partitions:
Why does
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?Attempts:
2 left
💡 Hint
All partitions must have the same MODULUS value for hash partitioning.
✗ Incorrect
Partition logs_p2 uses MODULUS 4, which conflicts with the parent's MODULUS 3. This causes PostgreSQL to reject inserts that hash to remainder 2 because no valid partition exists for that remainder under MODULUS 3.