Skip to content

feat: clone partitioned tables with PARTITION BY / PARTITION OF - #39

Merged
valehdba merged 1 commit into
mainfrom
feat/partitioned-tables
Nov 17, 2026
Merged

valehdba merged 1 commit into
mainfrom
feat/partitioned-tables

Conversation

@valehdba

Copy link
Copy Markdown
Owner

Fixes #38

Problem

pgclone.schema() created a partitioned parent as a plain table and then aborted with cannot copy from partitioned table "m"; the partitions were never cloned.

Changes

  • Parent: created with PARTITION BY <pg_get_partkeydef>; no data copy (COPY of a partitioned table is rejected).
  • Partitions: created with PARTITION OF <parent> FOR VALUES ... (sub-partitioned partitions also get their own PARTITION BY); rows are copied from leaf partitions only, so nothing is loaded twice.
  • Inherited objects: constraints (conparentid = 0), indexes (not in pg_inherits) and triggers (tgparentid = 0) that a partition inherits are no longer cloned separately; a parent index dumped as ON ONLY is created as a normal partitioned index so partitions inherit it.
  • Ordering: the table list is ordered by inheritance depth (PGCLONE_TABLE_DEPTH_SQL) so parents exist before partitions. In the parallel pool a partition worker waits up to ~60 s for its parent to appear.
  • Applies to the sync path (pgclone_table) and the async paths (bgw_clone_one_table, schema async, parallel pool).

Behaviour notes

  • pgclone.table() on a partitioned parent creates only the parent; clone the schema (or each partition) for the partitions. Documented in docs/USAGE.md.
  • conflict=skip matches existing parents/partitions by name (verified: all skipped when names match). Target partitions with different names look new and are created as extra partitions of the cloned parent.

Testing

New test/test_partitions.sh (wired into run_tests.sh): sync schema(), conflict=skip rerun, schema_async(), schema_async() with 3 parallel workers. Fixture has a sub-partitioned partition and a partition that sorts before its parent by name. Asserts relkinds, each row exactly once (parent 5, leaves 2/2/1), PK/index inherited not duplicated.

Full ./test/run_all.sh passes on PG14, 15, 16, 17 and 18 (partition tests 33/33, async 24/24, chaos 5/5 on each).

Not covered

  • Foreign keys referencing partitioned tables, per-partition column differences, and columns / mask filters on partitioned tables were not specifically tested.
  • Partitions in a different schema than their parent assume the same schema exists on the target.

🤖 Generated with Claude Code

Recreate partitioned parents with PARTITION BY, partitions with PARTITION OF (incl. sub-partitions), copy rows from leaf partitions only, skip partition-inherited constraints/indexes/triggers, and order tables so parents precede partitions (pool workers wait for the parent). Adds test/test_partitions.sh (sync, skip rerun, async, parallel).
@valehdba
valehdba merged commit 42ffa48 into main Nov 17, 2026
22 checks passed
@valehdba
valehdba deleted the feat/partitioned-tables branch November 17, 2026 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Partitioned tables: schema clone fails with "cannot copy from partitioned table" and loses PARTITION BY

1 participant