Compact an existing MDBX database instance to reclaim free/reclaimable space. Use this when the user wants to shrink mdbx.dat, reclaim MDBX garbage-collected pages, or fix MDBX_MAP_FULL issues.
Compacts an MDBX database file by copying it without garbage-collected pages, producing a smaller file.
The user provides a datadir path. The database being compacted is always <datadir>/chaindata/mdbx.dat.
mdbx_stat -ef shows a high "Reclaimable" percentageMDBX_MAP_FULLmdbx.datmdbx.dat size — the compacted copy is written alongside the original.Verify that <datadir>/chaindata/mdbx.dat exists. Abort if it does not.
du -sh <datadir>/chaindata/mdbx.dat and report the size to the user.df -h <datadir>/chaindata and check available space. If available space is less than the mdbx.dat size, abort and tell the user there is not enough disk space.If the user wants to see whether compaction is worthwhile before proceeding:
make db-tools
./build/bin/mdbx_stat -ef <datadir>/chaindata
Look at the Reclaimable line in the output to estimate space savings.
mdbx_copymake db-tools
This places mdbx_copy at ./build/bin/mdbx_copy.
Create a sibling directory for the compacted output. The destination argument to mdbx_copy must be a file path, not a directory.
mkdir -p <datadir>/chaindata-compacted
./build/bin/mdbx_copy -c <datadir>/chaindata <datadir>/chaindata-compacted/mdbx.dat
<datadir>/chaindata<datadir>/chaindata-compacted/mdbx.datThis can take hours to days for large databases. Run in background.
After mdbx_copy completes successfully:
rm <datadir>/chaindata/mdbx.dat <datadir>/chaindata/mdbx.lck
mv <datadir>/chaindata-compacted/mdbx.dat <datadir>/chaindata/mdbx.dat
rmdir <datadir>/chaindata-compacted
Run du -sh <datadir>/chaindata/mdbx.dat and report the new size compared to the original.
The user can now restart erigon.