pgbackrest.git / summary / log / commit / refs
commit b908d5e2c5b37ef968f19e2c40a1001e19e5b87f
Author: David Steele <david@pgbackrest.org>
Commit: GitHub <noreply@github.com>
Date: Sat Feb 21 01:37:31 2026 +0000
Improve seek performance during block incremental delta restore.
The prior code issued a seek for each block even if the file was in the correct position. The extra call to lseek() was probably not too expensive but a flush was also required in case of a seek on the next loop. Flushing the write buffer after each block (which is often 8KiB) was definitely wasteful.
Fix this by adding seek to the IoWrite interface. That means the file position can be tracked and calls to seek/flush are only done when needed.
Seek during write is only used in PGDATA during restore so seek functionality is only added to the Posix driver.
doc/xml/release/2020s/2026/2.59.0.xml | 12 ++++++++++++
src/command/restore/file.c | 10 ++--------
src/common/io/write.c | 32 +++++++++++++++++++++++++++++++-
src/common/io/write.h | 3 +++
src/common/io/write.intern.h | 3 +++
src/storage/posix/write.c | 23 +++++++++++++++++++++++
src/storage/storage.intern.h | 1 +
test/src/module/common/ioTest.c | 28 ++++++++++++++++++++++++++++
test/src/module/storage/posixTest.c | 18 ++++++++++++++++++
9 files changed, 121 insertions(+), 9 deletions(-)
diff --git a/doc/xml/release/2020s/2026/2.59.0.xml b/doc/xml/release/2020s/2026/2.59.0.xml
index 0b91e1179..95d21622c 100644
--- a/doc/xml/release/2020s/2026/2.59.0.xml
+++ b/doc/xml/release/2020s/2026/2.59.0.xml
@@ -1,6 +1,18 @@
<release date="XXXX-XX-XX" version="2.59.0dev" title="UNDER DEVELOPMENT">
<release-core-list>
<release-improvement-list>
+ <release-item>
+ <github-issue id="2732"/>
+
+ <release-item-contributor-list>
+ <release-item-contributor id="david.steele"/>
+ <release-item-reviewer id="david.christensen"/>
+ <release-item-reviewer id="douglas.j.hunley"/>
+ </release-item-contributor-list>
+
+ <p>Improve seek performance during block incremental delta restore.</p>
+ </release-item>
+
<release-item>
<github-issue id="2724"/>
diff --git a/src/command/restore/file.c b/src/command/restore/file.c
index bae7f927f..62d6f9d73 100644
--- a/src/command/restore/file.c
+++ b/src/command/restore/file.c
@@ -336,19 +336,13 @@ restoreFile(
while (deltaWrite != NULL)
{
// Seek to the block offset. It is possible we are already at the correct position but it is easier
- // and safer to let lseek() figure this out.
- THROW_ON_SYS_ERROR_FMT(
- lseek(ioWriteFd(storageWriteIo(pgFileWrite)), (off_t)deltaWrite->offset, SEEK_SET) == -1,
- FileOpenError, STORAGE_ERROR_READ_SEEK, deltaWrite->offset,
- strZ(storagePathP(storagePg(), file->name)));
+ // and safer to let ioWriteSeek() figure it out.
+ ioWriteSeek(storageWriteIo(pgFileWrite), deltaWrite->offset);
// Write block
ioWrite(storageWriteIo(pgFileWrite), deltaWrite->block);
fileResult->blockIncrDeltaSize += bufUsed(deltaWrite->block);
- // Flush writes since we may seek to a new location for the next block
- ioWriteFlush(storageWriteIo(pgFileWrite));
-
deltaWrite = blockDeltaNext(blockDelta, read, storageReadIo(superBlockRead));
}
diff --git a/src/common/io/write.c b/src/common/io/write.c
index 9d83fcebe..a3aa3179a 100644
--- a/src/common/io/write.c
+++ b/src/common/io/write.c
@@ -17,6 +17,7 @@ struct IoWrite
{
IoWritePub pub; // Publicly accessible variables
Buffer *output; // Output buffer
+ uint64_t position; // Current write position
#ifdef DEBUG
bool filterGroupSet; // Were filters set?
@@ -69,7 +70,7 @@ ioWriteOpen(IoWrite *const this)
if (ioWriteInterface(this)->open != NULL)
ioWriteInterface(this)->open(ioWriteDriver(this));
- // Track whether filters were added to prevent flush() from being called later since flush() won't work with most filters
+ // Track whether filters were added to prevent seek()/flush() from being called later since they won't work with most filters
#ifdef DEBUG
this->filterGroupSet = ioFilterGroupSize(this->pub.filterGroup) > 0;
#endif
@@ -107,6 +108,7 @@ ioWrite(IoWrite *const this, const Buffer *const buffer)
if (bufRemains(this->output) == 0)
{
ioWriteInterface(this)->write(ioWriteDriver(this), this->output);
+ this->position += bufUsed(this->output);
bufUsedZero(this->output);
}
}
@@ -229,6 +231,34 @@ ioWriteFlush(IoWrite *const this)
FUNCTION_LOG_RETURN_VOID();
}
+/**********************************************************************************************************************************/
+FN_EXTERN void
+ioWriteSeek(IoWrite *const this, const uint64_t position)
+{
+ FUNCTION_LOG_BEGIN(logLevelTrace);
+ FUNCTION_LOG_PARAM(IO_WRITE, this);
+ FUNCTION_LOG_PARAM(UINT64, position);
+ FUNCTION_LOG_END();
+
+ ASSERT(this != NULL);
+ ASSERT(this->opened && !this->closed);
+ ASSERT(!this->filterGroupSet);
+ ASSERT(ioWriteInterface(this)->write != NULL);
+
+ // If the requested position is not the current position then flush writes and seek. Writes must be flushed first so they will
+ // not later end up in the wrong location.
+ if (position != this->position)
+ {
+ ioWriteFlush(this);
+ ioWriteInterface(this)->seek(ioWriteDriver(this), position);
+
+ // Update current position
+ this->position = position;
+ }
+
+ FUNCTION_LOG_RETURN_VOID();
+}
+
/**********************************************************************************************************************************/
FN_EXTERN void
ioWriteClose(IoWrite *const this)
diff --git a/src/common/io/write.h b/src/common/io/write.h
index 69ba2e994..b940f69d7 100644
--- a/src/common/io/write.h
+++ b/src/common/io/write.h
@@ -67,6 +67,9 @@ FN_EXTERN void ioWriteVarIntU64(IoWrite *this, uint64_t value);
// Flush any data in the output buffer. This does not end writing and will not work if filters are present.
FN_EXTERN void ioWriteFlush(IoWrite *this);
+// Seek to specified position relative to beginning of write
+FN_EXTERN void ioWriteSeek(IoWrite *this, uint64_t position);
+
// Close the IO and write any additional data that has not been written yet
FN_EXTERN void ioWriteClose(IoWrite *this);
diff --git a/src/common/io/write.intern.h b/src/common/io/write.intern.h
index c1b270e18..0e0dcc70e 100644
--- a/src/common/io/write.intern.h
+++ b/src/common/io/write.intern.h
@@ -20,6 +20,9 @@ typedef struct IoWriteInterface
// Optionally error when write is not ready.
bool (*ready)(void *driver, bool error);
+ // Seek to specified position relative to beginning of write
+ void (*seek)(void *driver, uint64_t position);
+
void (*write)(void *driver, const Buffer *buffer);
} IoWriteInterface;
diff --git a/src/storage/posix/write.c b/src/storage/posix/write.c
index 694d806ee..b14cf0ebd 100644
--- a/src/storage/posix/write.c
+++ b/src/storage/posix/write.c
@@ -178,6 +178,28 @@ storageWritePosix(THIS_VOID, const Buffer *const buffer)
FUNCTION_LOG_RETURN_VOID();
}
+/***********************************************************************************************************************************
+Seek to specified position relative to beginning of file
+***********************************************************************************************************************************/
+static void
+storageWritePosixSeek(THIS_VOID, const uint64_t position)
+{
+ THIS(StorageWritePosix);
+
+ FUNCTION_LOG_BEGIN(logLevelTrace);
+ FUNCTION_LOG_PARAM(STORAGE_WRITE_POSIX, this);
+ FUNCTION_LOG_PARAM(UINT64, position);
+ FUNCTION_LOG_END();
+
+ ASSERT(this != NULL);
+ ASSERT(this->fd != -1);
+
+ THROW_ON_SYS_ERROR_FMT(
+ lseek(this->fd, (off_t)position, SEEK_SET) == -1, FileWriteError, STORAGE_ERROR_WRITE_SEEK, position, strZ(this->nameTmp));
+
+ FUNCTION_LOG_RETURN_VOID();
+}
+
/***********************************************************************************************************************************
Close the file
***********************************************************************************************************************************/
@@ -252,6 +274,7 @@ static const IoWriteInterface storageWritePosixInterface =
.close = storageWritePosixClose,
.fd = storageWritePosixFd,
.open = storageWritePosixOpen,
+ .seek = storageWritePosixSeek,
.write = storageWritePosix,
};
diff --git a/src/storage/storage.intern.h b/src/storage/storage.intern.h
index 4054f4fcf..04654b14f 100644
--- a/src/storage/storage.intern.h
+++ b/src/storage/storage.intern.h
@@ -51,6 +51,7 @@ Error messages
#define STORAGE_ERROR_WRITE_CLOSE "unable to close file '%s' after write"
#define STORAGE_ERROR_WRITE_OPEN "unable to open file '%s' for write"
#define STORAGE_ERROR_WRITE_MISSING "unable to open file '%s' for write in missing path"
+#define STORAGE_ERROR_WRITE_SEEK "unable to seek to %" PRIu64 " in file '%s'"
#define STORAGE_ERROR_WRITE_SYNC "unable to sync file '%s' after write"
/***********************************************************************************************************************************
diff --git a/test/src/module/common/ioTest.c b/test/src/module/common/ioTest.c
index 7dd61dd76..1bb9e4c7f 100644
--- a/test/src/module/common/ioTest.c
+++ b/test/src/module/common/ioTest.c
@@ -69,6 +69,17 @@ testIoWriteClose(void *driver)
testIoWriteCloseCalled = true;
}
+/***********************************************************************************************************************************
+Test seek function
+***********************************************************************************************************************************/
+static void
+testBufferWriteSeek(THIS_VOID, uint64_t position)
+{
+ THIS(IoBufferWrite);
+ (void)position;
+ bufCat(this->write, BUFSTRDEF("SEEK"));
+}
+
/***********************************************************************************************************************************
Test filter that counts total bytes
***********************************************************************************************************************************/
@@ -599,6 +610,23 @@ testRun(void)
pckReadU64P(ioFilterGroupResultP(filterGroup, ioFilterType(sizeFilter))), 9, " check filter result");
TEST_RESULT_UINT(
pckReadU64P(ioFilterGroupResultP(filterGroup, STRID5("size2", 0x1c2e9330))), 22, " check filter result");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("seek");
+
+ buffer = bufNew(0);
+ TEST_ASSIGN(bufferWrite, ioBufferWriteNew(buffer), "create buffer write object");
+
+ bufferWrite->pub.interface.seek = testBufferWriteSeek;
+
+ TEST_RESULT_VOID(ioWriteOpen(bufferWrite), "open");
+ TEST_RESULT_VOID(ioWrite(bufferWrite, BUFSTRDEF("XXX")), "write");
+ TEST_RESULT_VOID(ioWriteSeek(bufferWrite, 3), "seek to current position");
+ TEST_RESULT_VOID(ioWriteSeek(bufferWrite, 4), "seek to new position");
+ TEST_RESULT_VOID(ioWrite(bufferWrite, BUFSTRDEF("XXX")), "write");
+ TEST_RESULT_VOID(ioWriteClose(bufferWrite), "close");
+
+ TEST_RESULT_STR_Z(strNewBuf(buffer), "XXXSEEKXXX", "check buffer");
}
// *****************************************************************************************************************************
diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c
index 2c0289d4f..417cf8372 100644
--- a/test/src/module/storage/posixTest.c
+++ b/test/src/module/storage/posixTest.c
@@ -1045,6 +1045,24 @@ testRun(void)
TEST_RESULT_VOID(storageWritePosixClose(ioWriteDriver(storageWriteIo(file))), "close file again");
TEST_RESULT_INT(storageInfoP(storageTest, strPath(fileName)).mode, 0700, "check path mode");
TEST_RESULT_INT(storageInfoP(storageTest, fileName).mode, 0600, "check file mode");
+
+ // -------------------------------------------------------------------------------------------------------------------------
+ TEST_TITLE("write with fseek");
+
+ HRN_STORAGE_PUT_Z(storageTest, "fseek.txt", "XXXXZZYYYY");
+ TEST_STORAGE_GET(storageTest, "fseek.txt", "XXXXZZYYYY");
+
+ TEST_ASSIGN(file, storageNewWriteP(storageTest, STRDEF("fseek.txt"), .noTruncate = true, .noAtomic = true), "open write");
+ TEST_RESULT_VOID(ioWriteOpen(storageWriteIo(file)), "open");
+ TEST_RESULT_VOID(ioWrite(storageWriteIo(file), BUFSTRDEF("AA")), "write");
+ TEST_RESULT_VOID(ioWrite(storageWriteIo(file), BUFSTRDEF("AA")), "write");
+
+ TEST_RESULT_VOID(ioWriteSeek(storageWriteIo(file), 6), "seek");
+ TEST_RESULT_VOID(ioWrite(storageWriteIo(file), BUFSTRDEF("BB")), "write");
+ TEST_RESULT_VOID(ioWrite(storageWriteIo(file), BUFSTRDEF("BB")), "write");
+ TEST_RESULT_VOID(ioWriteClose(storageWriteIo(file)), "close");
+
+ TEST_STORAGE_GET(storageTest, "fseek.txt", "AAAAZZBBBB");
}
// *****************************************************************************************************************************
[parent: 009644b443b7]