Truncate Database File on Checkpoint #7824
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for truncating the DuckDB database file on checkpoint if there are free blocks at the end of the file. As a result - we can now shrink the DuckDB file if tables or rows are deleted and there is space left at the end of the file.
Note that this won't always work as expected in its current form. If we drop a big table and then checkpoint, the file will likely not shrink. That is because as part of the checkpoint process we will write new metadata to the file. As we cannot overwrite the big table yet (to maintain ACID properties) - the metadata is written to the end of the file. As a result of the new metadata being at the end of the file, the file will not be able to be shrunk yet.
On a subsequent checkpoint, the file will be able to shrink as the new metadata can then overwrite the old data in the big table.
For example: