Skip to main content

VACUUM

VACUUM serves two roles in SereneDB. Without options it is a PostgreSQL-compatible no-op on SereneDB tables, accepted so that existing tooling keeps working. It is also extended with options that maintain tables and their inverted indexes.

Standard VACUUM and ANALYZE​

On a SereneDB table, VACUUM and ANALYZE without the options below do no work — they are accepted purely for PostgreSQL compatibility. On a table of an attached DuckDB database file, VACUUM table and ANALYZE table recompute the table's statistics.

The bare form:

SQL
VACUUM;

ANALYZE is accepted as well:

SQL
VACUUM ANALYZE;

Targeting a specific table, optionally with a column list, is also accepted and is likewise a no-op:

SQL
VACUUM my_table(name);
SQL
VACUUM ANALYZE my_table(name);

VACUUM FULL is not supported:

SQL
VACUUM FULL;
error db error: ERROR: FULL is not yet implemented

Table and Index Maintenance​

SereneDB extends VACUUM with options that maintain tables and their inverted indexes. A statement takes at most one of these options, and an extension option cannot be combined with standard options such as ANALYZE. The options fall into three families.

Flushing — FLUSH_*​

A committed INSERT, UPDATE or DELETE is visible to every snapshot taken after its commit, full-text search included, whether or not it was flushed. A table holds its most recent writes in memory and writes them to its on-disk segments later. FLUSH_* writes them now; FLUSH_INDEX flushes the table the index belongs to, and does nothing for an index on a view. It does not change query results:

SQL
INSERT INTO articles VALUES (1, 'the quick brown fox');
SELECT count(*) FROM articles_idx WHERE body @@ ts_phrase('fox');
VACUUM (FLUSH_TABLE) articles;
SELECT count(*) FROM articles_idx WHERE body @@ ts_phrase('fox');
 count
-------
     1

 count
-------
     1

Compacting — COMPACT_*​

COMPACT_* merges the segments of tables and of inverted indexes on views to reclaim space and keep queries fast; COMPACT_INDEX compacts the table an index belongs to, or the index itself when it is on a view. It does not change query results:

SQL
VACUUM (COMPACT_TABLE) articles;

Tables and inverted indexes on views also compact in the background by default (see Background compaction); COMPACT_* merges on demand, including when background compaction is disabled. A table is merged into a single segment. Set sdb_compact_target_segments to N to stop at N segments instead: the segments are split into N disjoint groups and each group is merged into one segment, so no single merge holds the whole table. Up to threads groups merge at once, so a larger N finishes sooner and holds more merges in memory at a time.

SQL
SET sdb_compact_target_segments = 4;
VACUUM (COMPACT_TABLE) docs_store;
RESET sdb_compact_target_segments;

Packing in-memory tables — PACK_TABLE​

A temporary table and its inverted indexes are kept in memory. Each segment the table writes out of its newest rows or merges is packed, each of its files into one allocation, so that queries read it as fast as a segment on a local disk. A segment written when the memory limit left no room to pack it is kept as it was written: queries on it are slower, and under memory pressure its memory is written to the temporary directory. VACUUM (PACK_TABLE) table packs every such segment of the table:

SQL
VACUUM (PACK_TABLE) session_notes;
SELECT id FROM session_notes_idx WHERE body @@ ts_phrase('fox');
 id
----
  1

The option takes a table name and applies only to temporary tables:

Text
ERROR:  "articles" is not an in-memory table
DETAIL: VACUUM (PACK_TABLE) applies to temporary tables.

Scopes​

Each option applies at the scope named by its suffix and takes the matching object name as its argument:

ScopeFLUSHCOMPACTArgument
IndexFLUSH_INDEXCOMPACT_INDEXindex name
TableFLUSH_TABLECOMPACT_TABLEtable name
SchemaFLUSH_SCHEMACOMPACT_SCHEMA[database.]schema
DatabaseFLUSH_DATABASECOMPACT_DATABASEdatabase name
EverythingFLUSH_ALLCOMPACT_ALL(none)

Object names may be qualified, for example VACUUM (FLUSH_TABLE) mydb.public.articles. The *_ALL scopes take no argument and cover every database of the server but not attached databases.

The statistics of a table and its indexes follow its segments as they are written and compacted, so there is no option to recompute them. To recompute the statistics of a table in an attached DuckDB database file, run ANALYZE on it.

A table you lack the MAINTAIN privilege on is skipped with a warning such as permission denied to compact "articles", skipping it; an index is maintained under the privileges of its table or view. A read-only database, such as one attached with READ_ONLY, refuses the options that name an object.

Combining options​

A statement accepts at most one maintenance option, and a maintenance option cannot be mixed with standard VACUUM options such as ANALYZE:

SQL
VACUUM (COMPACT_TABLE, ANALYZE) my_table;
db error: ERROR: VACUUM SereneDB option 'compact_table' cannot be combined with standard VACUUM options

The vacuum_rebuild_indexes setting, which governs index rebuilds in attached DuckDB database files, is fixed at startup and cannot be changed while the server is running:

SQL
SET vacuum_rebuild_indexes = 1000000;
error db error: ERROR: Cannot change vacuum_rebuild_indexes setting while database is running

Reclaiming Space​

VACUUM without options does not reclaim space. COMPACT_* rewrites a table's segments without the rows deleted from it. In an attached DuckDB database file, use the CHECKPOINT statement, or create a fresh copy with the COPY FROM DATABASE statement.

Syntax​

Syntax

See the scope table for the valid ( … ) maintenance options and their arguments.