Skip to content

feat: find and attach the partitions a stopped backfill left unattached - #85

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/find-and-finish-stranded-windows
Sep 29, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/find-and-finish-stranded-windows

Conversation

@AlexeyShalaev

@AlexeyShalaev AlexeyShalaev commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Closes #76. The other half, #83, already moves the rows back when an attach fails; this one is for a process that never gets to fail.

The problem

A backfill creates a window's partition, moves the window's rows into it from DEFAULT, and attaches it last. If the process dies after the move and before the attach, the rows are in that table and nowhere else. The next backfill probes DEFAULT for the oldest key, finds nothing of that window, and never looks at it again, while plan and validate both say the table is fine. On 1.6.3:

visible through events: 0        in events__2026_05 (detached): 744
$ pg-partsmith plan --check      exit 2, CREATE for the current month only
$ pg-partsmith backfill          moved 0 rows in 0 batches; DEFAULT is drained

The design

Finding them. PartitionMetadataProvider.get_unattached_tables(table_name): tables in the root's schema whose names begin with the root's, attached to nothing and carrying no orphan marker. The schema comes from to_regclass, so a configuration without an explicit schema works. It is a query of its own, not part of get_actual_tree, because list_partitions and get_default_partition are built on that and have no use for it. It reads no rows.

Deciding which are partitions. unattached_window(config, relname) is the one rule, used by the inspector, the planner and the backfill: the name must read back as a window and be the very name the scheme gives that window under this root. Reading back alone is not enough. The integration suite's foreign-leaf tests have metrics_…_remote__2025_01 beside metrics_…, which the month pattern happily reads as January; with a one-way check the backfill would have created an empty January partition to "finish" it. Only after a table passes is its first row asked for (get_leading_key_minimum), so a table that merely shares the prefix, the pre-migration copy say, is never read and needs no grant.

Reporting. The planner reports each such table holding rows as unattached_rows, a WARNING: plan --check exits 3 and apply carries it in result.issues. It stays quiet for a window the same plan creates anyway, because the executor already attaches a table it finds under a name it creates.

Finishing. Once DEFAULT is drained, partition_data takes the next such window and runs it through the path a window out of DEFAULT takes. plan_for(window) gives the create, the executor finds the table under that name and completes and attaches it, and the fill has nothing to move. The same scenario on this branch:

$ pg-partsmith plan --check      exit 3
  [warning] unattached_rows: public.events__2026_05 holds rows of 2026_05 but is attached to nothing, so no query through public.events sees them; backfill (partition_data) attaches it.
$ pg-partsmith backfill          exit 0, moved 0 rows in 1 batches into 1 partitions; DEFAULT is drained
visible through events: 744      events__2026_05 attached
$ pg-partsmith plan --check      exit 2, no unattached_rows

A table under a window's name whose rows do not fit that window was not left by a backfill, since a fill only ever moves a window's own rows. ATTACH refuses it, and partition_data reports that and leaves the table as it is instead of raising.

What I rejected

  • Reporting in validate, as the issue suggested. validate is the CI check, the document against the table's shape, and it runs no planner; putting production data state into it would fail CI for the wrong reason. plan --check is where this belongs, and exits 3.
  • Attaching these in apply. A table under a window name without a marker is the library's by the rule the executor already applies to windows it wants, but for a past window the natural fix is the command that fills windows, and backfill is what the finding points to.
  • Matching by __ and the root's name. Numeric schemes take a configurable name_suffix, so the only reliable judge of what is a partition name is the scheme itself.

Public surface

get_unattached_tables is an addition to the metadata protocol in both mirrors, like current_time in 1.6.0: a provider written from scratch gains one method. UnattachedTable and ActualTree.unattached are new, and FindingReason.UNATTACHED_ROWS is a new reason. Hence feat: and 1.7.0. Docs: the findings reference, troubleshooting, the interruption table in concepts/execution.md, the backfill steps, extending.md, the API cheatsheet and agents.md.

Verified

  • unit: 2861 passed. New: the finding for a window table holding rows, none for an empty one, a non-window name or a look-alike of another table's, and none for a window the plan creates; unattached_tables reads only window-named tables and skips a non-RANGE root; the provider's rows and parameters; partition_data finishing a stranded window once DEFAULT is drained, and completing when there is none
  • integration (aio + sync, PG 17): 571 passed. New: a killed fill is reported by plan, attached by partition_data with all 25 rows visible, gone from the next plan, and a __archive table beside it is neither reported nor touched. The foreign-leaf tests caught the one-way name check before it shipped. New as well: a table under March's name holding February's rows is reported by the backfill and left alone. CI's first run caught my own test data using a zone-dependent literal on the UTC+14 server; with explicit UTC instants the whole suite passes locally under Pacific/Kiritimati / Etc/GMT+12 too (571), and the old literal fails there, so the setting was really in effect
  • the CLI run above against a live database
  • mypy pg_partsmith, ruff, and the docs build are clean

@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.47328% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pg_partsmith/planner.py 90.47% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@AlexeyShalaev
AlexeyShalaev force-pushed the fix/find-and-finish-stranded-windows branch from 0df8440 to c83b45c Compare September 29, 2026 16:00
A backfill moves a window's rows out of DEFAULT into a partition it
attaches last. If the process died between the two, the rows sat in a
table no query through the parent sees, DEFAULT held nothing to bring a
later backfill back to the window, and plan and validate said all was
well.

plan now reports such a table as unattached_rows (WARNING), and
partition_data attaches it once DEFAULT is drained, through the path a
window out of DEFAULT takes. A table counts only when its name is the
very name the scheme gives a window under this root, checked both ways,
so metrics_remote__2025_01 beside metrics is not taken for January; its
rows are read only after that, so a table that merely shares the prefix
is never read and needs no grant.

A table under a window's name holding rows outside that window was not
left by a backfill; partition_data reports it and leaves it as it is
rather than raising the check violation ATTACH gives.

get_unattached_tables(table_name) is a method of
PartitionMetadataProvider in both mirrors: the tables in the root's
schema named under it, attached to nothing and unmarked. It is its own
query rather than part of get_actual_tree, so list_partitions and
get_default_partition do not pay for it. ActualTree.unattached and
UnattachedTable carry what it found.

Closes #76
@AlexeyShalaev
AlexeyShalaev force-pushed the fix/find-and-finish-stranded-windows branch from c83b45c to f57ee1d Compare September 29, 2026 16:03
@AlexeyShalaev
AlexeyShalaev merged commit cb9c85c into master Sep 29, 2026
32 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/find-and-finish-stranded-windows branch September 29, 2026 16:11
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.

An interrupted backfill leaves rows in an unattached partition; plan / validate report ok

1 participant