Report
pg_dump/pg_restore --clean with -t or -n silently drops partitions (and their rows) that are outside the selection
- Severity
- 40
- Blamed commit
- f0e44751d7175fa3394da2c8f85e3ceb3cdbfe63
On 11 branches
REL_10_STABLE, REL_11_STABLE, REL_12_STABLE, REL_13_STABLE, REL_14_STABLE, REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
With --clean, pg_dump and pg_restore emit DROP TABLE for a selected partitioned table even when its partitions are not part of the selection. DROP TABLE on a partitioned table drops every partition (partitions have AUTO dependencies), so the restore destroys tables and rows it never dumped and never recreates. Nothing is reported. The documentation says --clean drops "all the dumped database objects".
Reproduction
Setup:
CREATE SCHEMA keep; CREATE SCHEMA app;
CREATE TABLE app.m (id int PRIMARY KEY, v text)
PARTITION BY RANGE (id);
CREATE TABLE keep.m_2025 PARTITION OF app.m
FOR VALUES FROM (0) TO (1000);
CREATE TABLE app.m_2026 PARTITION OF app.m
FOR VALUES FROM (1000) TO (2000);
INSERT INTO app.m SELECT i, 'row' || i
FROM generate_series(0, 1999) i;
Schema-only selection:
$ pg_dump --clean --if-exists -n app db | psql db
-- only error: cannot drop inherited constraint "m_2026_pkey"
-- (the known BUG #16928 noise)
-- afterwards: keep.m_2025 no longer exists;
-- SELECT count(*) FROM app.m -> 1000 (was 2000)
Parent only:
$ pg_dump --clean --if-exists -t app.m db | psql db
-- m_2025 and m_2026 are gone; app.m is recreated empty
pg_restore selection from a full archive:
$ pg_restore --clean --if-exists -n app -t m -d db full.dump
-- NOTICE-free; every partition is dropped, app.m is left with
-- 0 rows
With legacy inheritance the same commands fail safely ("cannot drop table app.ip because other objects depend on it"), and the child and its rows survive. Only declarative partitioning loses data, because DROP TABLE on the parent cascades to the partitions without CASCADE.
Expected
--clean should not remove objects that are not being restored. For example, pg_dump could emit DROP TABLE for a partitioned table only when all of its partitions are also selected, or it could detach unselected partitions first. At minimum the docs for --clean, -t and -n should warn that unselected partitions are dropped together with their parent, and point to --table-and-children.
Notes
The cross-schema -n case is the clearest: a table in a schema that was not selected disappears. The blamed commit introduced declarative partitioning and its AUTO dependency; the pg_dump DROP statement code is older.
Related but separate: BUG #16928 (pg_restore --clean fails to drop inherited partition constraints).
Tested on master at b7822f9992216f9aa85422337c15f1e7a8d6db72.
Branches
Reproduced on master. The blamed commit is in REL_14_STABLE and every later branch; the release branches were not tested.