COMPACT TABLE¶
Description¶
The COMPACT TABLE statement prunes superseded historical versions older than a specified anchor date to reclaim disk space. To prevent accidental data loss, MemoraDB archives the pre-compaction data file before activating the compacted storage.
Syntax¶
(See the Syntax Notation Guide for notation rules).
Basic syntax¶
Full syntax¶
Parameters¶
| Parameter | Description |
|---|---|
<table> |
Target table name. |
TO <date> |
The cutoff date or timestamp. Versions prior to each row's anchor at this date are purged from active storage. |
Example (employees)¶
Expected Output:
(OrCompacted 'employees' (with vector table) if a semantic index exists).
How It Works¶
Compaction proceeds in four atomic stages:
1. Anchor Identification¶
For each primary key in the table:
* Finds the latest version committed at or before the cutoff timestamp (anchor).
* Retains the anchor version and all subsequent versions.
* Versions older than the anchor are marked for exclusion.
2. Temporary File Generation¶
- Opens
data/<table_name>/data.db.tmp. - Copies over the table metadata header.
- Sequentially writes each retained record, recording new file offsets into a fresh
HistoryIndex. - If a companion vector table exists (
.vec),vt->startRewrite()andvt->copyRecord()mirror the process.
3. Safe Archival¶
- Moves the active
data.dbto: - Activates
data.db.tmpby renaming it todata.db.
4. Index Swap¶
- Replaces the table's in-memory
HistoryIndexwith the newly computed contiguous index.
Safety Guarantees¶
- Non-Destructive Archival: The pre-compaction database file is preserved in the
archive/folder. - Point-in-Time Integrity: Because the anchor version is retained, running
AS OF <date>queries at or after the cutoff date remains 100% accurate. - Vector Table Synchronization: Vector embeddings are rewritten and compacted in lockstep with relational records.