0
0
PostgreSQLquery~20 mins

Attaching and detaching partitions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of attaching a partition
Given a partitioned table sales and a child table sales_2023 with matching structure, what is the output of the following command?

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

What will be the result of this command?
PostgreSQL
ALTER TABLE sales ATTACH PARTITION sales_2023 FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
ASyntax error due to missing parentheses around the range values.
BError because the range values overlap with an existing partition.
CError because sales_2023 already contains data.
DThe partition sales_2023 is attached successfully to sales.
Attempts:
2 left
💡 Hint
Check if the partition range matches the parent table's partitioning scheme.
query_result
intermediate
2:00remaining
Result of detaching a partition
What happens when you run this command on a partitioned table orders with a partition orders_2022 attached?

ALTER TABLE orders DETACH PARTITION orders_2022;
PostgreSQL
ALTER TABLE orders DETACH PARTITION orders_2022;
AError because the partition contains data.
BSyntax error due to missing CASCADE keyword.
CThe partition orders_2022 is detached and becomes a standalone table.
DThe partition is detached but all data is deleted.
Attempts:
2 left
💡 Hint
Detaching a partition removes it from the parent but keeps the data intact.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in attaching a partition
Which option contains a syntax error when trying to attach a partition log_2023 to a partitioned table logs for the year 2023?
AALTER TABLE logs ATTACH PARTITION log_2023 FOR VALUES IN ('2023');
BALTER TABLE logs ATTACH PARTITION log_2023 FOR VALUES FROM '2023-01-01' TO '2024-01-01';
CALTER TABLE logs ATTACH PARTITION log_2023 FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');
D;)'10-10-4202'( OT )'10-10-3202'( MORF SEULAV ROF 3202_gol NOITITRAP HCATTA sgol ELBAT RETLA
Attempts:
2 left
💡 Hint
Check the syntax for specifying range values in the ATTACH PARTITION command.
🔧 Debug
advanced
2:00remaining
Why does attaching a partition fail?
You try to attach a partition events_2023 to a partitioned table events using:

ALTER TABLE events ATTACH PARTITION events_2023 FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

The command fails with an error about overlapping ranges. What is the most likely cause?
AThere is already a partition attached with a range that overlaps the specified range.
BThe child table events_2023 has a different column structure than events.
CThe parent table events is not partitioned.
DThe child table events_2023 contains no data.
Attempts:
2 left
💡 Hint
Check existing partitions and their ranges for conflicts.
🧠 Conceptual
expert
2:00remaining
Effect of detaching a partition on indexes
After detaching a partition archive_2020 from a partitioned table archive, what happens to the indexes defined on the partitioned table?
AThe detached partition retains its own indexes independently of the parent table.
BAll indexes on the detached partition are dropped automatically.
CThe detached partition inherits all indexes from the parent table dynamically.
DThe detached partition loses all indexes and must be reindexed manually.
Attempts:
2 left
💡 Hint
Consider how indexes are managed on partitions versus standalone tables.