*** DRAFT ***

SQL As Understood By SQLite

[Top]

VACUUM

vacuum-stmt:

R-54664-28647:[ ]

The VACUUM command rebuilds the database file, repacking it into a minimal amount of disk space. There are several reasons an application might do this:

R-55119-57913:[By default, VACUUM only works only on the main database. ] R-36598-60500:[Attached databases can be vacuumed by appending the appropriate schema-name to the VACUUM statement. ]

Compatibility Warning: The ability to vacuum attached databases was added in version 3.15.0 (2016-10-14). Prior to that, a schema-name added to the VACUUM statement would be silently ignored and the "main" schema would be vacuumed.

VACUUM with an INTO clause

R-07264-50488:[If the INTO clause is included, then the original database file is unchanged and a new database is created in the filename given by the argument to the INTO clause. ] The new database will contain the same logical content as the original database, fully vacuumed.

The VACUUM command with an INTO clause is an alternative to the backup API for generating backup copies of a live database. The advantage of using VACUUM INTO is that the resulting backup database is minimal in size and hence the amount of filesystem I/O may be reduced. Also, all deleted content is purged from the backup, leaving behind no forensic traces. On the other hand, the backup API uses fewer CPU cycles and can be executed incrementally.

R-16711-29980:[The filename in the INTO clause can be an arbitrary SQL expression that evaluates to a string. ] R-46334-60999:[The file named by the INTO clause must not previously exist, or else it must be an empty file, or the VACUUM INTO command will fail with an error. ]

R-25072-57278:[The argument to INTO can be a URI filename if URI filenames are enabled. ] URL filenames are enabled if any of the following are true:

The VACUUM INTO command is transactional in the sense that the generated output database is a consistent snapshot of the original database. However, if the VACUUM INTO command is interrupted by a unplanned shutdown or power lose, then the generated output database might be incomplete and corrupt. Also, SQLite does not invoke fsync() or FlushFileBuffers() on the generated database to ensure that it has reached non-volatile storage before completing.

How VACUUM works

R-27809-56550:[The VACUUM command works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file. ] R-48974-49065:[When overwriting the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction. ] R-13903-46567:[This means that when VACUUMing a database, as much as twice the size of the original database file is required in free disk space. ]

R-02754-60157:[The VACUUM INTO command works the same way except that it uses the file named on the INTO clause in place of the temporary database and omits the step of copying the vacuumed database back over top of the original database. ]

R-17495-17419:[The VACUUM command may change the ROWIDs of entries in any tables that do not have an explicit INTEGER PRIMARY KEY. ]

R-12218-18073:[A VACUUM will fail if there is an open transaction on the database connection that is attempting to run the VACUUM. ] R-15610-35227:[Unfinalized SQL statements typically hold a read transaction open, so the VACUUM might fail if there are unfinalized SQL statements on the same connection. ] R-19879-20948:[VACUUM (but not VACUUM INTO) is a write operation and so if another database connection is holding a lock that prevents writes, then the VACUUM will fail. ]

R-55138-13241:[An alternative to using the VACUUM command to reclaim space after data has been deleted is auto-vacuum mode, enabled using the auto_vacuum pragma. ] R-64844-34873:[When auto_vacuum is enabled for a database free pages may be reclaimed after deleting data, causing the file to shrink, without rebuilding the entire database using VACUUM. ] R-41548-42669:[However, using auto_vacuum can lead to extra database file fragmentation. ] R-49389-26640:[And auto_vacuum does not compact partially filled pages of the database as VACUUM does. ]

*** DRAFT ***