0
0
PostgreSQLquery~20 mins

Creating partitioned tables in PostgreSQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Partitioning Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of inserting data into a range partitioned table

Consider the following PostgreSQL partitioned table setup:

CREATE TABLE sales (
  id SERIAL PRIMARY KEY,
  sale_date DATE NOT NULL,
  amount NUMERIC
) PARTITION BY RANGE (sale_date);

CREATE TABLE sales_2023 PARTITION OF sales
  FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

CREATE TABLE sales_2024 PARTITION OF sales
  FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

INSERT INTO sales (sale_date, amount) VALUES
  ('2023-06-15', 100),
  ('2024-03-10', 200);

SELECT tableoid::regclass, sale_date, amount FROM sales ORDER BY sale_date;

What will be the output of the SELECT query?

PostgreSQL
CREATE TABLE sales (
  id SERIAL PRIMARY KEY,
  sale_date DATE NOT NULL,
  amount NUMERIC
) PARTITION BY RANGE (sale_date);

CREATE TABLE sales_2023 PARTITION OF sales
  FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

CREATE TABLE sales_2024 PARTITION OF sales
  FOR VALUES FROM ('2024-01-01') TO ('2025-01-01');

INSERT INTO sales (sale_date, amount) VALUES
  ('2023-06-15', 100),
  ('2024-03-10', 200);

SELECT tableoid::regclass, sale_date, amount FROM sales ORDER BY sale_date;
A
sales_2023 | 2024-03-10 | 200
sales_2024 | 2023-06-15 | 100
B
sales_2023 | 2023-06-15 | 100
sales_2024 | 2024-03-10 | 200
C
sales | 2023-06-15 | 100
sales | 2024-03-10 | 200
DError: no partition for value
Attempts:
2 left
💡 Hint

Remember that data inserted into a partitioned table is stored in the matching partition table.

🧠 Conceptual
intermediate
1:30remaining
Choosing partition key type for performance

You want to create a partitioned table in PostgreSQL to store user logs. The logs have a timestamp column and a user_id column. You expect queries mostly filter by date ranges and sometimes by user_id.

Which partitioning strategy is best for optimizing query performance?

APartition by RANGE on the timestamp column
BPartition by LIST on the user_id column
CPartition by HASH on the user_id column
DPartition by RANGE on the user_id column
Attempts:
2 left
💡 Hint

Think about which column is most commonly used in WHERE clauses and how partition pruning works.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in partition creation

Which of the following CREATE TABLE statements for a partitioned table is syntactically correct in PostgreSQL?

A
CREATE TABLE orders (
  id INT,
  order_date DATE
) PARTITION BY RANGE (order_date);
B
CREATE TABLE orders (
  id INT,
  order_date DATE
) PARTITION BY LIST (order_date);
C
CREATE TABLE orders (
  id INT,
  order_date DATE
) PARTITIONED BY RANGE (order_date);
D
CREATE TABLE orders (
  id INT,
  order_date DATE
) PARTITION BY RANGE order_date;
Attempts:
2 left
💡 Hint

Check the exact syntax for partitioning in PostgreSQL.

🔧 Debug
advanced
2:00remaining
Why does this insert fail on a partitioned table?

Given this partitioned table setup:

CREATE TABLE events (
  id SERIAL PRIMARY KEY,
  event_type TEXT NOT NULL
) PARTITION BY LIST (event_type);

CREATE TABLE events_login PARTITION OF events FOR VALUES IN ('login');
CREATE TABLE events_logout PARTITION OF events FOR VALUES IN ('logout');

INSERT INTO events (event_type) VALUES ('signup');

Why does the INSERT statement fail?

AThe partition tables are missing primary keys
BThe event_type column cannot be NULL
CNo partition exists for the value 'signup' in event_type
DThe PARTITION BY LIST syntax is invalid
Attempts:
2 left
💡 Hint

Think about how PostgreSQL routes data to partitions.

optimization
expert
2:30remaining
Optimizing query performance on a large partitioned table

You have a large PostgreSQL table partitioned by RANGE on a date column with monthly partitions. You notice queries filtering on date ranges are slow.

Which of the following actions will most likely improve query performance?

ADrop all indexes and rely on sequential scans
BCreate a global index on the parent partitioned table
CConvert the partitioning to LIST on the date column
DCreate indexes on the partition key column in each partition
Attempts:
2 left
💡 Hint

Consider how indexes work with partitioned tables in PostgreSQL.