-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Optimistically write data to disk when batch loading data into the system #4996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ng tasks, otherwise if all tasks finish before the event is scheduled weird things happen
…ge of existing partial blocks
…ger store a pointer to a specific row group as it might become invalidated if we run e.g. an alter type
This was referenced Nov 10, 2022
Merged
sacundim
pushed a commit
to sacundim/covid-19-puerto-rico
that referenced
this pull request
May 21, 2023
…vague hope that this will exploit some logic to better spill out to disk duckdb/duckdb#4996
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 enables DuckDB to optimistically compress and write data to disk when bulk loading.
Previously, when loading data from e.g. a large CSV or Parquet file in a single transaction, data was first loaded entirely into the transaction local storage. When committing, the data would then be flushed out of transaction local storage and written to the actual database file on disk.
While this has the advantage that in case of a
ROLLBACK
or similar error during loading discarding of the transaction-local state is free, it comes with a big disadvantage. Namely, when loading data that is bigger than memory we have to make multiple round-trips to disk.Previously, this is what would happen when loading more data than fits in memory in a single transaction:
Optimistic Streaming to Disk
In this PR, we instead optimistically write transaction-local data to the database file as it is appended to tables. For every row group that is appended (120K~ rows) we immediately compress the data and write it out to the database file. This allows for a full streaming load into the database file, and greatly speeds up loading speed when loading more data than fits in memory in a single transaction.
If the transaction is rolled back or aborted, the blocks that were pre-emptively written to disk are marked as unused and reclaimed by the system for use in subsequent writes. This might still cause the database file to grow temporarily, however, and may create gaps in the database file if there are multiple transactions writing at the same time with a subset of those transactions aborting. That space is not lost - however. It will be re-used by the system when new data is ingested.
The actual performance gain depends mostly on the speed of the storage. On my Macbook (with a very fast SSD) the performance difference is small (on the order of 10%~ faster). On a machine with a hard disk or slower SSD, the performance difference will be far larger.
Another benefit is that required disk space drops heavily, as we will no longer have to write uncompressed data to disk. Instead, the data will be directly compressed as it is written to the table.
SQLLogicTest:
concurrentloop
As part of this PR, we expand the sqllogictest framework with two new loops:
concurrentloop
andconcurrentforeach
. These operate similarly to the existingloop
andforeach
- with one big difference: every iteration of the loop is executed in parallel using separate threads that each have their own connection to the database. Example:We have several tests that test this type of behavior written in C++, but adding this functionality to the
sqllogictest
makes it significantly easier to write tests for the multiple-connection scenario, which should also enable us to write many more of them.