0
0
PostgreSQLquery~30 mins

List partitioning by category in PostgreSQL - Mini Project: Build & Apply

Choose your learning style9 modes available
List Partitioning by Category in PostgreSQL
📖 Scenario: You are managing a database for an online store. The store sells products in different categories like Electronics, Clothing, and Books. To improve performance and organization, you want to split the main products table into smaller tables based on product categories using list partitioning.
🎯 Goal: Create a partitioned table products in PostgreSQL that divides data into partitions by the category column. You will create the main table, define partitions for Electronics, Clothing, and Books, and insert sample data into the correct partitions.
📋 What You'll Learn
Create a partitioned table named products partitioned by list on the category column.
Create three partitions named products_electronics, products_clothing, and products_books for categories 'Electronics', 'Clothing', and 'Books' respectively.
Insert sample rows into the products table with correct categories so they go into the right partitions.
Verify the partitioning setup with correct table and partition definitions.
💡 Why This Matters
🌍 Real World
Partitioning large tables by category helps improve query speed and manageability in real-world databases like e-commerce product catalogs.
💼 Career
Database administrators and backend developers use partitioning to optimize storage and query performance in production systems.
Progress0 / 4 steps
1
Create the main partitioned table
Write a SQL statement to create a table called products with columns id (integer), name (text), and category (text). Partition this table by list on the category column.
PostgreSQL
Need a hint?

Use PARTITION BY LIST (category) to specify list partitioning on the category column.

2
Create partitions for each category
Write SQL statements to create three partitions of the products table: products_electronics for category 'Electronics', products_clothing for category 'Clothing', and products_books for category 'Books'. Use FOR VALUES IN with the exact category names.
PostgreSQL
Need a hint?

Use CREATE TABLE <partition_name> PARTITION OF products FOR VALUES IN ('CategoryName'); for each category.

3
Insert sample data into the partitioned table
Write SQL insert statements to add these rows into the products table: (1, 'Smartphone', 'Electronics'), (2, 'Jeans', 'Clothing'), and (3, 'Novel', 'Books').
PostgreSQL
Need a hint?

Insert rows into the main products table. PostgreSQL will route them to the correct partitions automatically.

4
Verify the partitioning setup
Write a SQL query to select all rows from the products table ordered by id. This will confirm that data is correctly partitioned and accessible from the main table.
PostgreSQL
Need a hint?

Use a simple SELECT * FROM products ORDER BY id; to see all rows from all partitions combined.