History retention: retention

retention removes old versions of facts. It requires database, and removes history from the database storage. Settings name the fields to prune, and an entity can carry its own policy in stardust/history. Without retention, the database keeps every version of every fact.

SettingAcceptsPurposeDefault
fieldsA map from a field name to {retain …}The history to keep of each field.No fields
debounceA #durTime without commits before a removal pass starts.#dur "PT2S"
max-waitA #durLongest time after a commit before a removal pass starts.#dur "PT30S"
recheckA #durTime between passes without commits, for a #dur or #utc retain, and after a pass that failed or that a history floor held back.#dur "PT1M"

Each field takes one retain value:

retainKept history
An integer N, 1 or moreThe newest N values of each fact, and the value before them.
A positive #durThe values written within that time before now, and the value before them.
A #utcThe values written at or after that time, and the value before them.

Fields that the settings do not name keep all of their history. The plugin refuses an invalid retain value or an unknown setting when the database opens.

Dependencies

Configuration

Select this plugin, and database with a path, when opening the database:

TRON285 B
Offset (hex)Bytes (hex)Meaning
00054 52 4F 4Emagic TRON
0044C 70617468txt "path"
0096C 6170702E6462txt "app.db"
0100F 0A 04000000 0900 0000map leaf, size 10: @4 → @9
01A6C 72657461696Etxt "retain"
02102 0A000000000000 00i64 10
02A0F 0A 1A000000 2100 0000map leaf, size 10: @1A → @21
0344C 23647572txt "#dur"
0394C 50333044txt "P30D"
03E0F 0A 34000000 3900 0000map leaf, size 10: @34 → @39
0486C 72657461696Etxt "retain"
04F0F 0A 48000000 3E00 0000map leaf, size 10: @48 → @3E
0596C 737461747573txt "status"
0600F 0A 59000000 2A00 0000map leaf, size 10: @59 → @2A
06A5C 6175646974txt "audit"
0700F 0A 6A000000 4F00 0000map leaf, size 10: @6A → @4F
07A07 0E 01100000 6000 0000 70000000map branch, size 14, depth 0 (root), slots 0, 12: @60 → @70
0884C 23647572txt "#dur"
08D4C 50543253txt "PT2S"
0920F 0A 88000000 8D00 0000map leaf, size 10: @88 → @8D
09C8C 6465626F756E63 65txt "debounce"
0A50F 0A 9C000000 9200 0000map leaf, size 10: @9C → @92
0AF6C 6669656C6473txt "fields"
0B60F 0A AF000000 7A00 0000map leaf, size 10: @AF → @7A
0C007 0E 0C000000 A500 0000 B6000000map branch, size 14, depth 0 (root), slots 2, 3: @A5 → @B6
0CE9C 726574656E7469 6F6Etxt "retention"
0D80F 0A CE000000 C000 0000map leaf, size 10: @CE → @C0
0E28C 64617461626173 65txt "database"
0EB0F 0A E2000000 1000 0000map leaf, size 10: @E2 → @10
0F507 0E 08100000 D800 0000 EB000000map branch, size 14, depth 0 (root), slots 3, 12: @D8 → @EB
1037C 706C7567696E73txt "plugins"
10B0F 0A 03010000 F500 0000map leaf, size 10: @103 → @F5

Usage

Removal runs in the background, in small steps that each commit on their own. A commit that writes facts starts a pass after debounce without more commits, and not later than max-wait after that commit. A database that opens starts a pass after debounce, so removal continues after a restart. Without storage, a pass removes nothing.

Removal never changes the current value of a fact. An as-of read before the oldest kept value reads the oldest kept value, and no read fails. List values release their storage chunks, and the database deletes the chunks that no kept value uses.

Removal never waits for readers, and readers never see it: each request reads one snapshot. A session opened with as-of, or pinned by namespaces/load with as-of, reads one snapshot until it closes or resets. While such a session is open, the database cannot reuse the pages of its snapshot, and the file can grow.

Entity policies

A patch gives an entity its own policy in the stardust/history field:

TRON242 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
046C 72657461696Etxt "retain"
0B02 05000000000000 00i64 5
140F 0A 04000000 0B00 0000map leaf, size 10: @4 → @B
1E7C 63617363616465txt "cascade"
2609bit true
270F 0A 1E000000 2600 0000map leaf, size 10: @1E → @26
3107 0E 80010000 1400 0000 27000000map branch, size 14, depth 0 (root), slots 7, 8: @14 → @27
3F6C 737461747573txt "status"
466C 706C61636564txt "placed"
4D0F 0A 3F000000 4600 0000map leaf, size 10: @3F → @46
5714 10 737461726475 73742F686973746F 7279txt "stardust/history"
690F 0A 57000000 3100 0000map leaf, size 10: @57 → @31
7307 0E 01020000 4D00 0000 69000000map branch, size 14, depth 0 (root), slots 0, 9: @4D → @69
816C 5F6F72646572txt "_order"
880F 0A 81000000 7300 0000map leaf, size 10: @81 → @73
925C 7061746368txt "patch"
980F 0A 92000000 8800 0000map leaf, size 10: @92 → @88
A24C 636F6465txt "code"
A70F 0A A2000000 9800 0000map leaf, size 10: @A2 → @98
B16C 6D6574686F64txt "method"
B88C 7472616E736163 74txt "transact"
C10F 0A B1000000 B800 0000map leaf, size 10: @B1 → @B8
CB6C 706172616D73txt "params"
D20F 0A CB000000 A700 0000map leaf, size 10: @CB → @A7
DC07 0E 80010000 C100 0000 D2000000map branch, size 14, depth 0 (root), slots 7, 8: @C1 → @D2

The policy is {retain …} with an optional boolean cascade, and nothing else. Other values are refused with #err {code <invalid value>}. null removes the policy.

retainKept history of each field of the entity
An integer N, 1 or moreThe facts of the newest N transactions that wrote a field of the entity, and the value before them.
A positive #durThe facts written within that time before now, and the value before them.
A #utcThe facts written at or after that time, and the value before them.

With cascade true, the policy also applies to the descendants of the entity through stardust/path/parent. A descendant with its own policy uses it. An own policy without cascade applies to that entity only, so its children still use the cascading policy above it. The policy map merges into an entity that stardust/history links to, as every map in a patch does, so a patch that gives only retain keeps the earlier cascade. If a direct edit makes the linked policy invalid, pruning stops and retention/status reports last-error until the policy is repaired.

When a field setting and an entity policy apply to the same fact, the one that keeps more history applies.

History floors

Another plugin can hold history back with a history floor (Spec.history_floor). A removal step then keeps every fact that a read at or after the lowest floor needs. A pass that a floor held back sets held-back, and the plugin runs a pass again after recheck.

Status

The retention/status request reports what removal did. It takes no params and starts no pass.

FieldMeaning
runsPasses since the database opened.
last-runWhen the last pass ended, a #utc, or null before the first pass.
entitiesThe distinct entities whose versions the last pass changed. When memory ran out while counting them, an upper bound, and last-error says so.
deletedVersions deleted since the database opened.
rewrittenVersions that took the oldest kept value since the database opened.
removed-pairsFacts with no versions left since the database opened.
freed-chunksList chunks deleted since the database opened.
held-backA history floor held the last pass back.
last-errorWhy the last pass failed, or null when it did not fail.
pendingA commit waits for a pass, or a pass runs.

A pass that fails on a field or an entity records last-error, continues with the other work, and runs again after recheck.

Contributions

Fields

Patches write these fields only while this plugin is selected.

FieldWrites
stardust/historyAny patch, in the shape this page gives

Requests

  • retention/status