Skip to content

feat: plan on the database's clock, not the process's - #77

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/plan-against-database-clock
Sep 29, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/plan-against-database-clock

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Closes #74.

The problem

A plan's "now" was the clock of the process running maintenance, and nothing compared it with anything. @alexluong set a container to June 2027 against Outpost's data through September 2026, and one apply --allow-destructive detached and dropped every monthly partition, the current one included, and exited 0. The regression test below reproduces it on unmodified master: the database's current month is detached.

The design

The database is the clock. PartitionMetadataProvider.current_time() answers SELECT now(), in UTC, and PartitionInspector.context() reads it whenever the caller names no instant. Everything downstream already ran on the one PlanningContext.now — the cursor window, creation ahead, retention's candidates — so this is the only place the clock enters. plan(now=...) is unchanged: an instant a caller names is still the instant the plan is made against.

This is the principle is_partition_closed already stated ("evaluated on the server rather than on the client, so the answer tolerates app-clock skew"); planning just had not followed it. pg_partman never had the problem because it runs inside the database.

What I rejected

  • A skew check that refuses to run. It converts the data loss into a failure, but keeps retention on a clock that is not the data's, and a behind-the-clock process would still create partitions late. Reading the database's clock removes the disagreement instead of detecting it.
  • A guard at apply time against plans made "from the future". With the default fixed, the only way to get such a plan is to pass now= explicitly, which is a decision the caller made.
  • A cap on drops per run. A reasonable operational knob, but a different feature; it would not have told the apply in Retention trusts the client clock, with no guard on what it drops #74 anything it could act on.

Public surface

current_time() is an addition to the PartitionMetadataProvider protocol in both mirrors, the same kind of change as is_partition_closed in 1.4.0: a provider written from scratch gains one method, subclasses of PostgresMetadataProvider are untouched. Hence feat: and 1.6.0.

Tests

The integration suites travel in time with freezegun, which moves only the process clock. tests/integration/conftest.py keeps that working: it answers current_time() from the process clock in integration tests, except those marked database_clock. Every existing test runs unchanged. The unit tests that patched inspection.datetime now set metadata.current_time instead.

The new test (both mirrors) creates this month and the next on the database's clock with retention=2, then runs maintenance with the process frozen three years ahead: nothing is created, detached or dropped, the current month stays attached, and the plan's generated_at is within a minute of the database's now(). Swapping the old inspection.py back in, it fails on is_attached(current).

Not in this PR

The instant a detach is stamped with, which the orphan grace period counts from, still comes from the process, so a clock that is off moves the end of the grace period by as much. Moving it means the repository has to be told the time, which changes detach_partition on the repository protocol, and touches the eighty-odd direct detach calls in the tests. It is its own change.

Verified

  • unit: 2815 passed
  • integration (aio + sync, PostgreSQL 17): 563 passed
  • the regression test fails on master's inspector and passes here
  • mypy pg_partsmith, ruff, and the docs build are clean

A plan's "now" is the database's clock unless the caller names an
instant. It used to be the clock of the process running maintenance, and
nothing compared the two: with a container at June 2027 against data
through September 2026, one apply --allow-destructive detached and
dropped every monthly partition, the current one included, and exited 0.

current_time() is a method of PartitionMetadataProvider in both mirrors:
SELECT now(), in UTC. PartitionInspector.context() reads it when no
instant is given; plan(now=...) is unchanged.

The integration suites travel in time with freezegun, which moves only
the process clock, so tests/integration/conftest.py answers
current_time() from the process clock there, except in tests marked
database_clock. The new one freezes the process three years ahead and
checks that the database's current month survives; it fails on the old
inspector.

The detach stamp the orphan grace period counts from still comes from
the process; moving it changes the repository protocol.

Closes #74
@AlexeyShalaev
AlexeyShalaev force-pushed the fix/plan-against-database-clock branch 2 times, most recently from 6c14f9b to f09594a Compare September 29, 2026 09:04
@codecov

codecov Bot commented Sep 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@AlexeyShalaev
AlexeyShalaev merged commit e0c0192 into master Sep 29, 2026
32 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/plan-against-database-clock branch September 29, 2026 09:12
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.

Retention trusts the client clock, with no guard on what it drops

1 participant