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
Recall & Review
beginner
What is sub-partitioning in PostgreSQL?
Sub-partitioning is dividing a partition of a table into smaller partitions. It helps organize data better and can improve query speed by narrowing down where data is stored.
Click to reveal answer
intermediate
How do you create a sub-partitioned table in PostgreSQL?
You first create a partitioned table, then define partitions for it. Each partition can itself be partitioned further, creating sub-partitions using the PARTITION BY clause again.
Click to reveal answer
intermediate
Why use sub-partitioning instead of just partitioning?
Sub-partitioning allows more detailed data grouping. For example, first by year, then by month inside each year. This makes data management and queries more efficient for large datasets.
Click to reveal answer
advanced
Can you use different partitioning methods for sub-partitions?
Yes, PostgreSQL allows mixing partitioning methods. For example, you can partition a table by range, then sub-partition by list inside each range partition.
Click to reveal answer
beginner
What happens if you query a sub-partitioned table without specifying partition keys?
PostgreSQL will scan all partitions and sub-partitions, which can be slower. Specifying partition keys helps the database skip irrelevant partitions and speeds up queries.
Click to reveal answer
What is the main benefit of sub-partitioning in PostgreSQL?
AAutomatic backups
BMore disk space usage
CSlower data insertion
DBetter data organization and faster queries
✗ Incorrect
Sub-partitioning helps organize data into smaller groups, making queries faster by scanning fewer partitions.
Which SQL clause is used to define sub-partitions in PostgreSQL?
AGROUP BY
BORDER BY
CPARTITION BY
DJOIN
✗ Incorrect
The PARTITION BY clause is used to create partitions and sub-partitions.
Can you mix partitioning methods in sub-partitioning?
AYes, different methods can be combined
BNo, only one method per table
COnly hash partitioning is allowed
DOnly range partitioning is allowed
✗ Incorrect
PostgreSQL allows mixing partitioning methods like range and list in sub-partitions.
What happens if you query a sub-partitioned table without specifying partition keys?
AOnly the first partition is scanned
BAll partitions and sub-partitions are scanned
CQuery fails with error
DQuery runs instantly
✗ Incorrect
Without partition keys, PostgreSQL scans all partitions and sub-partitions, which can slow down queries.
Which of these is a valid reason to use sub-partitioning?
ATo organize data by year and then by month
BTo reduce the number of tables
CTo avoid indexing
DTo disable constraints
✗ Incorrect
Sub-partitioning helps organize data hierarchically, such as by year and then by month.
Explain what sub-partitioning is and why it is useful in PostgreSQL.
Think about how breaking data into smaller groups helps find data faster.
You got /3 concepts.
Describe how you would create a sub-partitioned table in PostgreSQL.
Start with a partitioned table, then add partitions inside partitions.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of sub-partitioning in PostgreSQL?
easy
A. To encrypt data within partitions
B. To create backups of partitions automatically
C. To merge multiple partitions into one
D. To split data twice for better organization and faster queries
Solution
Step 1: Understand partitioning basics
Partitioning divides a table into parts to improve management and performance.
Step 2: Recognize sub-partitioning role
Sub-partitioning splits each partition further, organizing data more finely and speeding up queries.
Final Answer:
To split data twice for better organization and faster queries -> Option D
Quick Check:
Sub-partitioning = double data split [OK]
Hint: Sub-partitioning means splitting partitions again [OK]
Common Mistakes:
Thinking sub-partitioning creates backups
Confusing sub-partitioning with encryption
Believing it merges partitions
2. Which of the following is the correct syntax to create a sub-partitioned table in PostgreSQL?
easy
A. CREATE TABLE sales (id INT, region TEXT, month INT) SUBPARTITION BY RANGE (region) PARTITION BY LIST (month);
B. CREATE TABLE sales (id INT, region TEXT, month INT) PARTITION BY RANGE (region) PARTITION BY LIST (month);
C. CREATE TABLE sales (id INT, region TEXT, month INT) PARTITION BY RANGE (region) SUBPARTITION BY LIST (month);
D. CREATE TABLE sales (id INT, region TEXT, month INT) PARTITION BY LIST (region) SUBPARTITION BY HASH (month);
Solution
Step 1: Identify correct keywords for partitioning
PostgreSQL uses PARTITION BY for main partition and SUBPARTITION BY for sub-partition.
Step 2: Check syntax order and clauses
CREATE TABLE sales (id INT, region TEXT, month INT) PARTITION BY RANGE (region) SUBPARTITION BY LIST (month); correctly uses PARTITION BY RANGE then SUBPARTITION BY LIST, matching PostgreSQL syntax.
Final Answer:
CREATE TABLE sales (id INT, region TEXT, month INT) PARTITION BY RANGE (region) SUBPARTITION BY LIST (month); -> Option C
Quick Check:
Use PARTITION BY then SUBPARTITION BY [OK]
Hint: Use PARTITION BY first, then SUBPARTITION BY [OK]
Common Mistakes:
Using PARTITION BY twice instead of SUBPARTITION BY
Swapping PARTITION BY and SUBPARTITION BY keywords
Using SUBPARTITION BY before PARTITION BY
3. Given the following table and partitions:
CREATE TABLE orders (id INT, country TEXT, year INT) PARTITION BY LIST (country) SUBPARTITION BY RANGE (year); CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('US') SUBPARTITION BY RANGE (year); CREATE TABLE orders_us_2022 PARTITION OF orders_us FOR VALUES FROM (2022) TO (2023);
What will be the result of SELECT * FROM orders WHERE country = 'US' AND year = 2022; if there are rows with country 'US' and year 2022?
medium
A. Rows with country 'US' and year 2022 will be returned
B. No rows will be returned because subpartition is missing
C. Syntax error due to incorrect partitioning
D. Rows with any country but year 2022 will be returned
Solution
Step 1: Understand partition and subpartition setup
The table is partitioned by country (LIST) and subpartitioned by year (RANGE). The 'US' partition and 2022 subpartition exist.
Step 2: Query filters match partition and subpartition
The query filters country='US' and year=2022, matching the defined partitions, so matching rows will be found.
Final Answer:
Rows with country 'US' and year 2022 will be returned -> Option A
Quick Check:
Partition + subpartition match = rows returned [OK]
Hint: Query matches partition and subpartition filters [OK]
Common Mistakes:
Assuming no rows because subpartition is complex
Thinking query causes syntax error
Ignoring subpartition filtering
4. You wrote this code:
CREATE TABLE logs (id INT, region TEXT, day DATE) PARTITION BY RANGE (region) SUBPARTITION BY LIST (day);
What is the error in this statement?
medium
A. RANGE partitioning cannot be done on a TEXT column
B. Partitioning by RANGE requires a numeric or date type, not TEXT
C. Syntax error: SUBPARTITION BY must come before PARTITION BY
D. SUBPARTITION BY LIST cannot be used with RANGE partitioning
Solution
Step 1: Check partition column data type
Partitioning by RANGE requires a column with an orderable type like numeric or date, not TEXT.
Step 2: Identify the error cause
Here, region is TEXT, so RANGE partitioning on it is invalid.
Final Answer:
Partitioning by RANGE requires a numeric or date type, not TEXT -> Option B
Quick Check:
RANGE needs numeric/date, not TEXT [OK]
Hint: RANGE partition needs numeric or date column [OK]
Common Mistakes:
Thinking TEXT can be used for RANGE partitioning
Confusing order of PARTITION BY and SUBPARTITION BY
Assuming SUBPARTITION BY LIST is invalid with RANGE
5. You want to create a sales table partitioned by region (LIST) and subpartitioned by sale_date (RANGE). Which approach correctly handles the subpartitioning to optimize query performance for recent sales?
hard
A. Partition by LIST on region, then subpartition by RANGE on sale_date with recent years as separate subpartitions
B. Partition by RANGE on sale_date, then subpartition by LIST on region with all regions in one subpartition
C. Partition by HASH on region, no subpartitioning needed for sale_date
D. Partition by LIST on sale_date, then subpartition by RANGE on region
Solution
Step 1: Match partitioning to data and query needs
Partitioning by region (LIST) groups data by location, then subpartitioning by sale_date (RANGE) organizes by time.
Step 2: Optimize recent sales queries
Using RANGE subpartitions for recent years allows fast access to recent data, improving query speed.
Final Answer:
Partition by LIST on region, then subpartition by RANGE on sale_date with recent years as separate subpartitions -> Option A
Quick Check:
LIST then RANGE for region and date [OK]
Hint: Partition by region LIST, subpartition by date RANGE [OK]