Skip to content

Commit

Permalink
Make sure that HashIndex::prepareCommit only runs once, but always ru…
Browse files Browse the repository at this point in the history
…ns before the COPY TABLE record is logged in the wal file during batch insert
  • Loading branch information
benjaminwinger committed Mar 27, 2024
1 parent 69dcd44 commit fed83ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/include/storage/index/hash_index.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ class PrimaryKeyIndex {
}

private:
// When doing batch inserts, prepareCommit needs to be run before the COPY TABLE record is
// logged to the WAL file, since the index is reloaded when that record is replayed. However
// prepareCommit will also be run later, and the local storage can't cleared from the
// HashIndices until checkpointing is done, and entries will get added twice if
// HashIndex::prepareCommit is run twice. It seems simplest to just track whether or not
// prepareCommit has been run.
bool hasRunPrepareCommit;
common::PhysicalTypeID keyDataTypeID;
std::shared_ptr<BMFileHandle> fileHandle;
std::unique_ptr<OverflowFile> overflowFile;
Expand Down
17 changes: 11 additions & 6 deletions src/storage/index/hash_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ template class HashIndex<ku_string_t>;
PrimaryKeyIndex::PrimaryKeyIndex(const DBFileIDAndName& dbFileIDAndName, bool readOnly,
common::PhysicalTypeID keyDataType, BufferManager& bufferManager, WAL* wal,
VirtualFileSystem* vfs)
: keyDataTypeID(keyDataType) {
: keyDataTypeID(keyDataType), hasRunPrepareCommit{false} {
fileHandle = bufferManager.getBMFileHandle(dbFileIDAndName.fName,
readOnly ? FileHandle::O_PERSISTENT_FILE_READ_ONLY :
FileHandle::O_PERSISTENT_FILE_NO_CREATE,
Expand Down Expand Up @@ -713,6 +713,7 @@ void PrimaryKeyIndex::checkpointInMemory() {
if (overflowFile) {
overflowFile->checkpointInMemory();
}
hasRunPrepareCommit = false;
}

void PrimaryKeyIndex::rollbackInMemory() {
Expand All @@ -722,14 +723,18 @@ void PrimaryKeyIndex::rollbackInMemory() {
if (overflowFile) {
overflowFile->rollbackInMemory();
}
hasRunPrepareCommit = false;
}

void PrimaryKeyIndex::prepareCommit() {
for (auto i = 0u; i < NUM_HASH_INDEXES; i++) {
hashIndices[i]->prepareCommit();
}
if (overflowFile) {
overflowFile->prepareCommit();
if (!hasRunPrepareCommit) {
for (auto i = 0u; i < NUM_HASH_INDEXES; i++) {
hashIndices[i]->prepareCommit();
}
if (overflowFile) {
overflowFile->prepareCommit();
}
hasRunPrepareCommit = true;
}
}

Expand Down

0 comments on commit fed83ba

Please sign in to comment.