This is an interesting question.
Let's take this partitioned table structure for reference (taken from PgAnalyze blog post)
Speaking of locking procedure wise, both two type of operations:
Drop the partitioned table right away (immediate-drop)
DROP TABLE people_partitioned_birthdays_1800_to_1850;
versus
Detach the partitioned table from the root(parent) table, then drop it (detach-drop)
ALTER TABLE people_partitioned DETACH PARTITION people_partitioned_birthdays_1800_to_1850;
DROP TABLE people_partitioned_birthdays_1800_to_1850;
both requires taking an ACCESS EXCLUSIVE LOCK
on the parent table people_partitioned
, which blocks any SELECT
(without FOR UPDATE/SHARE
) statement against the people_partitioned
while the lock is being in placed.
However, the detach-drop strategy is truly shine when you need to perform additional maintenance operations on the partitioned table. It's very likely that you might want to create a backup of this partition with COPY
, pg_dump
,
perform data aggregation, analytics, etc... before dropping it for good. And since this partitioned table has been detached, it would be free from lock conflicting with the parent table.
Now you might wonder that you can still perform these maintenance actions while the schedule for delete partition still in place?
-- perform maintenance action on people_partitioned_birthdays_1800_to_1850 ...
DROP TABLE people_partitioned_birthdays_1800_to_1850;
then again, if there's any operation that require excluse lock on the parent (e.g another new partition is being created/deleted, which also require exclusive lock on the parent table), then your maintenance action above will be blocked until the lock conflict is resolved.
So to conclude, it's generally safer to detach-drop a partitioned table from the root partitioning table than immediate-drop. Any operation on the existing partitioned tables structure should not block your maintenance operation on the detached partition, and vice-versa
Reference
Postgres DDL Declarative Partitioning (with use cases)