From 3e2a9a13818f2d1b420bd45a9f2caae90205bb97 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent Date: Fri, 22 Nov 2024 13:48:11 +0100 Subject: [PATCH v0] Fix data loss when restarting the bulk_write facility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a user started a bulk write operation on a fork with existing data to append data in bulk, the bulk_write machinery would zero out all previously written pages up to the last page written by the new bulk_write operation. Though PostgreSQL doesn't use the bulk_write facility twice on the same fork, there are use cases where bulk writing to a fork with data makes sense, so this commit fixes that issue. Extensions could hit this issue if they used rewriteheap on multiple tables to merge their data into a single relation, or manually created multiple bulk_write operations for the same fork. Reported-By: Erik Nordström --- src/backend/storage/smgr/bulk_write.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/backend/storage/smgr/bulk_write.c b/src/backend/storage/smgr/bulk_write.c index 1a5f3ce96e1..94b9d071c22 100644 --- a/src/backend/storage/smgr/bulk_write.c +++ b/src/backend/storage/smgr/bulk_write.c @@ -4,8 +4,10 @@ * Efficiently and reliably populate a new relation * * The assumption is that no other backends access the relation while we are - * loading it, so we can take some shortcuts. Do not mix operations through - * the regular buffer manager and the bulk loading interface! + * loading it, so we can take some shortcuts. Pages already present in the + * indicated fork when the bulk write operation is started are not touched + * unless explicitly written to. Do not mix operations through the regular + * buffer manager and the bulk loading interface! * * We bypass the buffer manager to avoid the locking overhead, and call * smgrextend() directly. A downside is that the pages will need to be @@ -68,8 +70,10 @@ struct BulkWriteState int npending; PendingWrite pending_writes[MAX_PENDING_WRITES]; + /* Pages written to disk */ + BlockNumber pages_written; /* Current size of the relation */ - BlockNumber pages_written; + BlockNumber relsize; /* The RedoRecPtr at the time that the bulk operation started */ XLogRecPtr start_RedoRecPtr; @@ -107,6 +111,7 @@ smgr_bulk_start_smgr(SMgrRelation smgr, ForkNumber forknum, bool use_wal) state->npending = 0; state->pages_written = 0; + state->relsize = smgrnblocks(smgr, forknum); state->start_RedoRecPtr = GetRedoRecPtr(); @@ -280,7 +285,7 @@ smgr_bulk_flush(BulkWriteState *bulkstate) PageSetChecksumInplace(page, blkno); - if (blkno >= bulkstate->pages_written) + if (blkno >= bulkstate->relsize) { /* * If we have to write pages nonsequentially, fill in the space @@ -289,21 +294,22 @@ smgr_bulk_flush(BulkWriteState *bulkstate) * space will read as zeroes anyway), but it should help to avoid * fragmentation. The dummy pages aren't WAL-logged though. */ - while (blkno > bulkstate->pages_written) + while (blkno > bulkstate->relsize) { /* don't set checksum for all-zero page */ smgrextend(bulkstate->smgr, bulkstate->forknum, - bulkstate->pages_written++, + bulkstate->relsize++, &zero_buffer, true); } smgrextend(bulkstate->smgr, bulkstate->forknum, blkno, page, true); - bulkstate->pages_written = pending_writes[i].blkno + 1; + bulkstate->relsize = pending_writes[i].blkno + 1; } else smgrwrite(bulkstate->smgr, bulkstate->forknum, blkno, page, true); pfree(page); + bulkstate->pages_written++; } bulkstate->npending = 0; -- 2.45.2