DEV Community

Cover image for No Night Watchman For Partitions
Anton Brilliantov
Anton Brilliantov

Posted on

No Night Watchman For Partitions

The cage is built when the resident arrives, not by a watchman who walks the zoo at night counting whether there are enough cages for tomorrow.


👋 Hi, I'm Anton - a software engineer. These days I work on a platform: contracts, generated code, data paths and the tooling around them. Running notes live on my GitHub: github.com/brilliant-almazov.

Partitions at write time are built into the shared library; four services use the package. I describe the mechanism, not a measurement of its cost on a busy table.

This is part 8 of the series on what a shared platform does for every service, told as a zoo. Maybe you already do it better, maybe you'd do it another way - this is how it is going, and where it stops.


In real life

Biscuit has a night job. With a lamp, every night, he walks the zoo and counts the empty cages: are there enough for tomorrow's arrivals? "Enough cages for tomorrow? Again..." It is the most boring job in the zoo, and Biscuit does it with exactly the enthusiasm it deserves.

One night he oversleeps. A new resident arrives in the morning, and there is no cage. Pickle finds it standing on the path with its parcel: "A new resident and no cage!"

A four-panel comic: Biscuit under a lamppost at night asks again whether there are enough cages for tomorrow; Biscuit asleep while Pickle finds a new resident with no cage; Pip next to a fresh cage says it arrived, build its cage now; Nora says into the book only when it stands

Pip's answer is to fire the night shift. A cage is built when a resident arrives - at that moment, for that resident, not ahead of time by someone guessing. And Nora adds the rule that keeps the register honest: the cage goes into the book only when it actually stands. Not when someone started building it.

A partition per time range

Some of our tables are partitioned by time: one partition per day, per month, per year. A row must land in a partition that covers its timestamp. The classic way to make sure that partition exists is a scheduled job that creates the next few partitions ahead of time - the night watchman. Before this package, the library had exactly one such watchman, and it knew exactly one table: the audit journal.

The night watchman fails in the boring ways. It runs on a schedule, so it guesses how far ahead to go. It oversleeps when the scheduler is down. It knows only the tables someone told it about. And a row for a range nobody guessed - a late event, a timestamp in the future - finds no cage.

The cage comes with the resident

The partition package does the opposite. At the moment of a write, it makes sure the partition for that row's range exists, creates it if it does not, and only then lets the row in. In the library's own words: partitions are created at the moment of writing, there is no default step, the name is remembered only after the creating transaction commits, and there are no background scanners.

Three tiles with faces: a write, happy; the partition check, proud; the table, happy; labels read a row for a range, and create if missing; a pill reads built

As a form - the names are illustrative:

partition:
  table: <schema.table>
  key: created_at
  step: Month              # Day | Week | Month | Year | Every(d); no default
# on write: no partition for the row's range -> create it, then insert
# the name is remembered only after the creating transaction commits
Enter fullscreen mode Exit fullscreen mode

A dark card with the partition form; the step line, Month, with the list of allowed steps and no default, is highlighted; a pill reads built

In PostgreSQL terms, the first row of a new month does roughly this - a form, the naming is illustrative:

-- the row's range has no partition yet
CREATE TABLE <table>_<range> PARTITION OF <table>
    FOR VALUES FROM ('<range start>') TO ('<range end>');
INSERT INTO <table> (...) VALUES (...);
-- every later row of that range goes straight to the INSERT
Enter fullscreen mode Exit fullscreen mode

Three decisions in that form carry the design.

No default step. The application chooses Day, Week, Month, Year or an arbitrary period, and the package refuses to guess. A default would be right for some tables and silently wrong for others.

Remembered after commit. The package caches the names of partitions it knows exist, so it does not ask the database on every write. It adds a name to that cache only after the transaction that created the partition commits. If the transaction rolls back, the partition does not exist, and a cache that remembered it anyway would let the next row in through a door that is not there. That is Nora's rule: into the book only when the cage stands.

No scanner at all. Nothing walks the tables in the background. The only time a partition is created is when a row needs it.

Bars: background scanners 0, steps to choose from 5, services using it 4; Biscuit says I sleep at night now; a pill reads built

A waffle of six cells: five teal cells with checks - Day, Week, Month, Year, Every; one red cell with a cross - a default step, none, the app chooses; Nora says you choose the step; a pill reads built

Night watchman At write time
when a partition appears on a schedule, ahead when a row needs it
a row for an unguessed range no partition, failed write its partition is created
which tables the one it knew any table that declares a step
the cache of known names — filled only after commit

An animation: a write for next month reaches the partition check, which finds no partition yet, creates it in the same moment, and the row lands in the table with the name cached

Who asked for it

The package came from a request: the admin service wanted a partitioned journal of console calls, and the only partition helper the library had was the one built for its own audit journal. Instead of a second watchman for a second table, the library got a package any table can use. Four services use it today - the admin service, the identity service, the security service and the project data service.

Before and after: a watchman - a scanner on a schedule, guessing ahead; 1 table, the only one it knew - and at write time - 0 scanners, the write makes the cage; any table, the step is a parameter

What went wrong

The failure this answers is a design one, not an incident I can point to: a scheduled partition creator is a second moving part that must be right for a write to succeed, and it is only right as long as its guess about the future is right. Every table that wanted partitions would have needed its own watchman or its own entry in the one that existed.

The quieter lesson is in the cache. Remembering a partition before its transaction commits is the kind of bug that never shows up in a test - until a rollback happens in exactly the wrong place. Writing "only after commit" into the package's own description is how we keep that rule from being optimised away.

Service tiles with faces: the admin journal, identity, security and project data are happy; the partition check proud; the night watchman bored; the write happy; the step happy; a pill reads built

What I do not claim

  • Not a measurement of the cost of the check on a busy table. It is built, not benchmarked here.
  • Not that every partitioned table has moved to the package. Four services use it.
  • Not that a night watchman is never right. It is not needed here.
  • Not the exact settings format. The form above shows the shape.
  • Not the exact statements the package runs. The SQL above is a form of the idea.

What of this is method

  • Create the cage when the resident arrives. Not ahead, by guessing.
  • Remember only what committed. A cache filled before commit lies after a rollback.
  • No default where defaults differ. The application chooses the step.

Zoo keeping, built in - Part 8. Next: Same "Seat Taken", Before And After The Race.

If you partition by time, who creates the next partition - a job or the write? If your job oversleeps, what happens to the first row of the new month? If you cache partition names, when exactly do you add one? Who walks your zoo with a lamp at night?

Top comments (0)