Repository navigation
feat: plan on the database's clock, not the process's - #77
Merged
Merged
Conversation
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
force-pushed
the
fix/plan-against-database-clock
branch
2 times, most recently
from
September 29, 2026 09:04
6c14f9b to
f09594a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-destructivedetached 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()answersSELECT now(), in UTC, andPartitionInspector.context()reads it whenever the caller names no instant. Everything downstream already ran on the onePlanningContext.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_closedalready 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
now=explicitly, which is a decision the caller made.Public surface
current_time()is an addition to thePartitionMetadataProviderprotocol in both mirrors, the same kind of change asis_partition_closedin 1.4.0: a provider written from scratch gains one method, subclasses ofPostgresMetadataProviderare untouched. Hencefeat:and 1.6.0.Tests
The integration suites travel in time with freezegun, which moves only the process clock.
tests/integration/conftest.pykeeps that working: it answerscurrent_time()from the process clock in integration tests, except those markeddatabase_clock. Every existing test runs unchanged. The unit tests that patchedinspection.datetimenow setmetadata.current_timeinstead.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'sgenerated_atis within a minute of the database'snow(). Swapping the oldinspection.pyback in, it fails onis_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_partitionon the repository protocol, and touches the eighty-odd direct detach calls in the tests. It is its own change.Verified
mypy pg_partsmith, ruff, and the docs build are clean