Hash partitioning helps split a big table into smaller parts based on a hash function. This makes data easier to manage and faster to find.
Hash partitioning for distribution in PostgreSQL
Start learning this pattern below
Jump into concepts and practice - no test required
CREATE TABLE table_name ( column1 data_type, column2 data_type, ... ) PARTITION BY HASH (column_name); CREATE TABLE partition_name PARTITION OF table_name FOR VALUES WITH (MODULUS n, REMAINDER r);
The main table is created with PARTITION BY HASH on a chosen column.
Each partition is created with FOR VALUES WITH (MODULUS n, REMAINDER r) where n is total partitions and r is the partition number starting from 0.
users partitioned by hashing the id column.CREATE TABLE users ( id INT, name TEXT ) PARTITION BY HASH (id);
users where hash(id) % 4 = 0.CREATE TABLE users_part_0 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 0);
users where hash(id) % 4 = 1.CREATE TABLE users_part_1 PARTITION OF users FOR VALUES WITH (MODULUS 4, REMAINDER 1);
This example creates an employees table partitioned by hashing emp_id into 3 parts. It inserts 5 employees and selects all rows ordered by emp_id.
CREATE TABLE employees ( emp_id INT, emp_name TEXT ) PARTITION BY HASH (emp_id); CREATE TABLE employees_part_0 PARTITION OF employees FOR VALUES WITH (MODULUS 3, REMAINDER 0); CREATE TABLE employees_part_1 PARTITION OF employees FOR VALUES WITH (MODULUS 3, REMAINDER 1); CREATE TABLE employees_part_2 PARTITION OF employees FOR VALUES WITH (MODULUS 3, REMAINDER 2); INSERT INTO employees (emp_id, emp_name) VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie'), (4, 'Diana'), (5, 'Evan'); SELECT * FROM employees ORDER BY emp_id;
Hash partitioning evenly distributes rows but does not guarantee order.
Choose the number of partitions (modulus) based on your data size and hardware.
Queries filtering on the partition key can be faster because only relevant partitions are scanned.
Hash partitioning splits data into parts using a hash function on a column.
It helps balance data and improve query speed by limiting search to certain partitions.
Each partition is defined by modulus (total parts) and remainder (partition number).
Practice
Solution
Step 1: Understand hash partitioning concept
Hash partitioning divides data by applying a hash function to a column value, distributing rows into partitions.Step 2: Compare options with concept
Only To split data into parts using a hash function on a column correctly describes this purpose; others describe unrelated features.Final Answer:
To split data into parts using a hash function on a column -> Option AQuick Check:
Hash partitioning = split data by hash [OK]
- Confusing hash partitioning with sorting
- Thinking it stores data on one server only
- Mixing partitioning with encryption
Solution
Step 1: Identify partitioning syntax
PostgreSQL uses PARTITION BY HASH(column) to create hash partitions.Step 2: Match syntax with options
Only CREATE TABLE sales PARTITION BY HASH (region); uses PARTITION BY HASH correctly; others use different partition types or invalid syntax.Final Answer:
CREATE TABLE sales PARTITION BY HASH (region); -> Option DQuick Check:
Hash partition syntax = PARTITION BY HASH [OK]
- Using RANGE or LIST instead of HASH
- Writing PARTITION BY COLUMN which is invalid
- Omitting parentheses around column
CREATE TABLE users (id INT, name TEXT) PARTITION BY HASH (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);
Which partition will the row with
id = 7 be stored in?Solution
Step 1: Calculate hash partition remainder
Partition is chosen by (hash(id) % modulus). Assuming hash(id) = id for simplicity, 7 % 4 = 3.Step 2: Match remainder to partition
Remainder 3 corresponds to partition users_p3.Final Answer:
users_p3 -> Option AQuick Check:
7 % 4 = 3 -> users_p3 [OK]
- Confusing remainder with modulus
- Using id directly without modulo
- Mixing partition numbers
CREATE TABLE orders_p0 PARTITION OF orders FOR VALUES WITH (MODULUS 3, REMAINDER 3);
What is the error in this statement?
Solution
Step 1: Understand modulus and remainder rules
Remainder must be less than modulus because remainder is result of modulo operation.Step 2: Check given values
MODULUS is 3, REMAINDER is 3, which is invalid since remainder must be 0, 1, or 2.Final Answer:
REMAINDER cannot be equal to or greater than MODULUS -> Option BQuick Check:
Remainder < Modulus rule violated [OK]
- Setting remainder equal to modulus
- Thinking modulus must be prime
- Misunderstanding syntax for hash partitions
logs table by hashing the user_id column into 5 partitions. Which of the following is the correct way to define the partitions?Solution
Step 1: Understand modulus and remainder for partitions
MODULUS is total partitions count; REMAINDER ranges from 0 to MODULUS-1.Step 2: Apply to 5 partitions
MODULUS must be 5; REMAINDER values must be 0,1,2,3,4 for 5 partitions.Final Answer:
Create 5 partitions with MODULUS 5 and REMAINDER values from 0 to 4 -> Option CQuick Check:
Partitions = MODULUS 5, REMAINDER 0-4 [OK]
- Setting remainder range incorrectly
- Using wrong modulus number
- Starting remainder at 1 instead of 0
