# LMDB restoration: the records survived, but the comparator did not
Research date: 2026-10-25.
Synthetic demonstration of a documented application contract, with retained native inputs.

## Finding
A complete inventory is not sufficient evidence that a restored LMDB database
supports the application's lookups. In our constructed 257-record database,
a full cursor scan returned every expected key and value, and the statistics
reported 257 entries. Reopening with the default comparator instead of the
original descending comparator made all 257 existing-key lookups return
MDB_NOTFOUND. Read-only probing left data.mdb byte-identical.

The result survived ordinary and compact native copies. Dump/load introduced a
second distinction: normal loading reconstructed ascending order, while
`mdb_load -a` preserved the supplied descending order. Neither route supplied
the application's comparator to subsequent processes. This is a demonstration
of a documented caller obligation, not a new LMDB vulnerability.

## Evidence and method
We used native LMDB 0.9.33 at commit
`3a29a24777c82a0165de813ae696a5068b5add30`, compiled from an existing clean
checkout. The same source was built with GCC 13.3.0 at -O2 and -O0 on Linux
x86_64/glibc 2.39. These are two builds on one host, not two library versions or
independent laboratory replications.

The fixture stores zero-padded, six-byte decimal keys and distinct 67-byte
ASCII values. Two populations, 9 and 257 keys, exercise one-level and two-level
B+ trees. Each is written with default ascending order and with a custom
descending lexical comparator. There are no special key flags, named
subdatabases or duplicate values.

Each source is tested directly, after plain native copy, after compact native
copy, after normal dump/load, and after dump/load with -a. Each artifact is
opened in a fresh process using the default comparator, a separately registered
but equivalent ascending comparator, and the descending comparator. Comparator
registration precedes all data access. The matrix contains
2 sizes x 2 writer orders x 5 artifacts x 3 reader policies = 60 configurations
per build, or 120 retained observations.

For each configuration the harness records database statistics, the complete
cursor sequence and payload checks, all n existing-key lookups, and two
known-absent keys. A traversal must terminate with MDB_NOTFOUND; other errors
fail the harness. The data-file SHA-256 is measured before and after probing.
All 32 transformations across the two builds returned zero. No writer runs
concurrently with a copy or probe.

## Main contrast
Descending source; successful existing-key reads / keys tested. Both builds
gave identical semantic observations. All rows retained the full scan inventory
and correct values. The underlying `summary.json` is generated from captures.

| Artifact | 9 keys, default reader | 9 keys, descending reader | 257 keys, default reader | 257 keys, descending reader |
|---|---:|---:|---:|---:|
| Original | 1/9 | 9/9 | 0/257 | 257/257 |
| Native copy | 1/9 | 9/9 | 0/257 | 257/257 |
| Compact copy | 1/9 | 9/9 | 0/257 | 257/257 |
| Normal dump/load | 9/9 | 1/9 | 257/257 | 0/257 |
| Dump/load with -a | 1/9 | 9/9 | 0/257 | 257/257 |

The default-writer controls remain fully searchable with default or equivalent
ascending readers. A registered callback is therefore not itself the problem:
its ordering must match the stored tree. Deliberately reversing the comparator
on the ascending controls also loses lookups. Both known-absent keys remain
absent. All successful lookups return the correct value, so the reported
mismatch is omission, not an observed wrong-value substitution.

Counts describe exhaustive queries over constructed fixtures. They are not
field failure probabilities, performance measurements or statistically sampled
error rates, and no confidence interval is warranted.

## Resolving the scattered documentation
The pinned API requires each program to install compatible comparison behavior
before accessing data. The implementation selects defaults from persisted
flags when opening the database, while `mdb_set_compare` installs a runtime
function pointer. Those are different kinds of state. [S1, S2]

The pinned `mdb_dump` manual still contains a severe warning about reloading
custom-comparator databases and describes modifying the loader. Meanwhile,
the `mdb_load` manual explicitly provides -a for this case. Treating either
sentence as a complete recovery procedure loses a precondition. [S3, S4]

The loader source explains the narrower -a behavior: it supplies an always-
greater callback and uses append operations to retain input order. It does
not reconstruct the application's callback implementation. The manual says
the input is assumed sorted and redundant-value checks are skipped. Therefore
-a is not a validator for arbitrary or reordered dumps. [S4, S5]

Our result does not establish irreversible damage after an ordinary reload.
In these unique-key fixtures it builds a usable ascending tree containing all
records, but that tree is incompatible with the original descending reader.
Preserving ordered bytes and restoring comparison semantics are separate
obligations. We did not attempt writes through an incompatible comparator,
test duplicate comparators, or generalize this result to every dump layout.

## Connection to existing knowledge
Chu's original MDB paper describes snapshot roots and copy-on-write page
lifetime. Those mechanisms answer which committed pages a reader sees.
Our synthesis adds a different question: can the current application navigate
those pages using the same key order? The experiment is not a replication of
the paper's performance evaluation. [S6]

Hesela already covers LMDB reader retention and copy backpressure. That work
validates the lifetime and content of a copy. This study adds runtime
interpretation to the restore acceptance criteria, without replacing that
evidence. It also connects to the existing SQLite callback/index experiment:
a storage artifact can depend on comparison behavior outside its byte
inventory. The checking interfaces differ, so we do not equate LMDB statistics
with SQLite integrity_check. [H1, H2]

LevelDB provides a useful source-level contrast. Its documented contract stores
and checks the comparator name, and the pinned recovery implementation rejects
a name mismatch. That is an explicit compatibility identifier, not proof that
two implementations using the same name behave identically. We have not run
a LevelDB experiment or ranked the two products' safety. [S7, S8]

## Restore acceptance implications
1. Retain comparator implementation/version and key encoding alongside the
   backup, including locale or external tables if the application's comparator
   actually depends on them.
2. Preserve originals. Test in an isolated output, register the intended
   comparator before access, and keep readers using different semantics away
   from a writable restored environment.
3. Check all expected key/value pairs in these small fixtures. For a real
   system, record the coverage and limitations of any sampled lookup workload.
   A full scan and an entry count are different evidence from indexed lookup.
4. Record the loader mode and validate its preconditions. An order-preserving
   import still needs the correct runtime comparator.
5. If changing ordering intentionally, design and verify a migration into a
   new database under the target semantics. Do not treat comparator replacement
   as a repair command.

These are conclusions from the API contract and this bounded experiment, not
a universal operational runbook.

## Reproduce
The [-O2 manifest](evidence-o2/manifest.json) and [-O0 manifest](evidence-o0/manifest.json)
list every retained database, dump and capture with its SHA-256. Paths are relative
to their manifest directory. Keep both directories beside the replay scripts.
[Summary](summary.json), [source records](sources.json) and
[integration replay receipt](publication-validation.json) are machine-readable.

Requirements: Python 3 standard library, Git, a C compiler, POSIX process and
filesystem support, and the clean pinned LMDB checkout. No Python packages,
network services or production database are used by the harness.

Obtain upstream separately, verify its license and exact commit, then run from
this directory. The source argument must point to the repository root.
Output directories must not already exist; earlier evidence is never overwritten.

```bash
python3 -B run.py --source /path/to/lmdb --output /tmp/lmdb-new-o2
python3 -B run.py --source /path/to/lmdb --output /tmp/lmdb-new-o0 --optimization=-O0
python3 -B verify.py /tmp/lmdb-new-o2 --compare /tmp/lmdb-new-o0 --source /path/to/lmdb
python3 -B -m unittest -v test_contract.py
python3 -O -B -m unittest -v test_contract.py
```

The unit suite uses the retained evidence-o2/evidence-o0 bundles. To rerun the
full retained-input acceptance and generate a fresh validation receipt:
`python3 -B validate.py --source /path/to/lmdb`.
It runs both test modes and replays every retained database/read-policy pair.

`verify.py` validates exact inventory and digests, matrix coverage, comparator
controls, payloads, scan termination and native input identity. A manifest is
an integrity inventory, not an authenticity signature. Anyone modifying both
data and its manifest must still be assessed through provenance and review.

## Limits and rights
The data are synthetic and contain no user records. Native database images
are retained so replay can use exact inputs; their format is not claimed
portable across architectures or incompatible LMDB releases. Only this pinned
release and two compiler optimization settings were tested. No power loss,
media corruption, production workload, throughput or incident frequency was
measured. Successful statistics are not advertised as an integrity check.
The original captures and validation receipt are retained. A separate reviewer
checked the supplied code, source claims and publication prose; the integration
author reran all 120 retained-input comparisons. This is not an independent
laboratory replication. The integration receipt is publication-validation.json.

Upstream code is fetched/reused separately under its OpenLDAP Public License;
no upstream source or compiled executable is redistributed in this packet.
Bibliographic references are in sources.json. This package's native test data
are newly generated; links to the original paper and documentation preserve
their original rights.

## Source keys
S1: LMDB 0.9.33 lmdb.h, mdb_set_compare.
S2: LMDB 0.9.33 mdb.c, mdb_default_cmp, mdb_node_search, mdb_set_compare.
S3: LMDB 0.9.33 mdb_dump.1, diagnostics.
S4: LMDB 0.9.33 mdb_load.1, -a.
S5: LMDB 0.9.33 mdb_load.c, greater and append loading.
S6: Howard Chu, MDB: A Memory-Mapped Database and Backend for OpenLDAP,
LDAPCon 2011, sections 3.2 and 4.1. No DOI identified in the retrieved document.
S7/S8: LevelDB 1.23 documentation and VersionSet::Recover.
H1/H2: Existing Hesela LMDB and SQLite runtime-dependency analyses.
