Space reclamation for a file-backed SQLite store.
SQLite does not give the file back by itself. Pages that a delete or a DROP COLUMN frees go on the FREELIST, and later writes use them again. A one-off
bulk reclaim (retiring a column across a whole transcript) can leave hundreds
of megabytes in the file.
maybe-vacuum! gives that space back when the freelist is at least
vacuum-min-free-bytes. A compact store is never touched, so the usual
answer is four PRAGMAs and no I/O.
Writers never wait for the whole file. A store with auto_vacuum=INCREMENTAL
frees its freelist with PRAGMA incremental_vacuum(N) in chunks of
vacuum-chunk-pages. Each chunk is its own short write transaction, and a
pause after each chunk lets waiting writers take the lock. Mozilla storage
does the same (mozStorageConnection.cpp AsyncVacuum, application-services
maintenance.rs). use-incremental-vacuum! gives each new store this mode.
Two processes that reclaim one store at the same time only take turns.
A store with auto_vacuum=NONE (made by an older Vis) has no page map for
this. One full VACUUM with auto_vacuum=INCREMENTAL converts it, and it is
its last full VACUUM. That rewrite holds the write lock for the whole file
(measured: 15 s for 2.4 GB), so it runs only when the store is at most
vacuum-max-file-bytes, inside the 30 s busy timeout of core. A larger old
store keeps its freelist, and SQLite uses those pages again: on a 17 GB store
the rewrite held the lock for many minutes and each turn write failed or
waited. The vis.db.vacuum file beside the store is the cross-process lock
of this conversion, an exclusive FileLock. The next reclaim of the
converted store deletes it.
The work runs off the open path: vacuum-async! gives it to
internal.janitor, the one maintenance worker of a process. One pass frees
pages for at most vacuum-slice-ms and then queues the next pass, so a long
freelist never holds the other chores for more than one slice. Each chunk and
each rewrite is a transaction, so a process that stops first leaves a
consistent store, and the rest stays due.
Space reclamation for a file-backed SQLite store. SQLite does not give the file back by itself. Pages that a delete or a `DROP COLUMN` frees go on the FREELIST, and later writes use them again. A one-off bulk reclaim (retiring a column across a whole transcript) can leave hundreds of megabytes in the file. `maybe-vacuum!` gives that space back when the freelist is at least `vacuum-min-free-bytes`. A compact store is never touched, so the usual answer is four PRAGMAs and no I/O. Writers never wait for the whole file. A store with `auto_vacuum=INCREMENTAL` frees its freelist with `PRAGMA incremental_vacuum(N)` in chunks of `vacuum-chunk-pages`. Each chunk is its own short write transaction, and a pause after each chunk lets waiting writers take the lock. Mozilla storage does the same (`mozStorageConnection.cpp` AsyncVacuum, application-services `maintenance.rs`). `use-incremental-vacuum!` gives each new store this mode. Two processes that reclaim one store at the same time only take turns. A store with `auto_vacuum=NONE` (made by an older Vis) has no page map for this. One full VACUUM with `auto_vacuum=INCREMENTAL` converts it, and it is its last full VACUUM. That rewrite holds the write lock for the whole file (measured: 15 s for 2.4 GB), so it runs only when the store is at most `vacuum-max-file-bytes`, inside the 30 s busy timeout of `core`. A larger old store keeps its freelist, and SQLite uses those pages again: on a 17 GB store the rewrite held the lock for many minutes and each turn write failed or waited. The `vis.db.vacuum` file beside the store is the cross-process lock of this conversion, an exclusive `FileLock`. The next reclaim of the converted store deletes it. The work runs off the open path: `vacuum-async!` gives it to `internal.janitor`, the one maintenance worker of a process. One pass frees pages for at most `vacuum-slice-ms` and then queues the next pass, so a long freelist never holds the other chores for more than one slice. Each chunk and each rewrite is a transaction, so a process that stops first leaves a consistent store, and the rest stays due.
(free-space ds){:auto-vacuum :page-size :page-count :freelist-count :file-bytes :free-bytes}
as SQLite itself reports it — :auto-vacuum is 0 (NONE) or 2 (INCREMENTAL),
:file-bytes is the live file, :free-bytes what a vacuum would give back.
`{:auto-vacuum :page-size :page-count :freelist-count :file-bytes :free-bytes}`
as SQLite itself reports it — `:auto-vacuum` is 0 (NONE) or 2 (INCREMENTAL),
`:file-bytes` is the live file, `:free-bytes` what a vacuum would give back.(maybe-vacuum! ds db-file)(maybe-vacuum! ds
db-file
{:keys [min-free-bytes max-file-bytes slice-ms open?]
disk-space :usable-space})Give back the freelist of the store at db-file when it is at least
vacuum-min-free-bytes. Returns a report — :is-vacuumed with a :reason of
:vacuumed, :compact (nothing worth reclaiming), :too-large (an
auto_vacuum=NONE store whose rewrite would block writers too long),
:low-disk, :locked (another process converts it) or :failed. Does not
throw. An incremental store frees pages for at most vacuum-slice-ms, and
:is-more is true when pages are left for the next pass. A busy checkpoint
retries on the janitor without a new reclaim. The optional :open? predicate
stops the work and these retries when the store closes.
Test options: :min-free-bytes, :max-file-bytes, :slice-ms, and
:usable-space (takes the store path).
Give back the freelist of the store at `db-file` when it is at least `vacuum-min-free-bytes`. Returns a report — `:is-vacuumed` with a `:reason` of `:vacuumed`, `:compact` (nothing worth reclaiming), `:too-large` (an `auto_vacuum=NONE` store whose rewrite would block writers too long), `:low-disk`, `:locked` (another process converts it) or `:failed`. Does not throw. An incremental store frees pages for at most `vacuum-slice-ms`, and `:is-more` is true when pages are left for the next pass. A busy checkpoint retries on the janitor without a new reclaim. The optional `:open?` predicate stops the work and these retries when the store closes. Test options: `:min-free-bytes`, `:max-file-bytes`, `:slice-ms`, and `:usable-space` (takes the store path).
(use-incremental-vacuum! ds)Give a new store auto_vacuum=INCREMENTAL before its first table, so that
maybe-vacuum! never needs a full VACUUM for it. A new connection already
writes the file header for WAL mode, so the mode needs a VACUUM, which is
instant on an empty store. A store with tables stays as it is.
Give a new store `auto_vacuum=INCREMENTAL` before its first table, so that `maybe-vacuum!` never needs a full VACUUM for it. A new connection already writes the file header for WAL mode, so the mode needs a VACUUM, which is instant on an empty store. A store with tables stays as it is.
(vacuum-async! ds db-file)(vacuum-async! ds db-file {:keys [delay-ms before open?] :as opts})Give maybe-vacuum! for the store at db-file to the janitor, which runs it
after :delay-ms, so the reclaim is not what a starting process waits on. A
:before function runs first in the same task, so a data rewrite frees its
pages before the reclaim checks them. If it returns true, truncate the WAL.
:open? answers whether the store is still open: a store that closed in the
delay is due at its next open. Called once per store per process, from the
pool that just opened it. A second call for one store replaces a task that
still waits.
The janitor writes the report of each pass that did work or that another process holds. A store with nothing to reclaim leaves no line, and a failed pass is a warning.
Give `maybe-vacuum!` for the store at `db-file` to the janitor, which runs it after `:delay-ms`, so the reclaim is not what a starting process waits on. A `:before` function runs first in the same task, so a data rewrite frees its pages before the reclaim checks them. If it returns true, truncate the WAL. `:open?` answers whether the store is still open: a store that closed in the delay is due at its next open. Called once per store per process, from the pool that just opened it. A second call for one store replaces a task that still waits. The janitor writes the report of each pass that did work or that another process holds. A store with nothing to reclaim leaves no line, and a failed pass is a warning.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |