|
4 | 4 | * Efficiently and reliably populate a new relation
|
5 | 5 | *
|
6 | 6 | * The assumption is that no other backends access the relation while we are
|
7 |
| - * loading it, so we can take some shortcuts. Do not mix operations through |
8 |
| - * the regular buffer manager and the bulk loading interface! |
| 7 | + * loading it, so we can take some shortcuts. Pages already present in the |
| 8 | + * indicated fork when the bulk write operation is started are not modified |
| 9 | + * unless explicitly written to. Do not mix operations through the regular |
| 10 | + * buffer manager and the bulk loading interface! |
9 | 11 | *
|
10 | 12 | * We bypass the buffer manager to avoid the locking overhead, and call
|
11 | 13 | * smgrextend() directly. A downside is that the pages will need to be
|
@@ -71,7 +73,7 @@ struct BulkWriteState
|
71 | 73 | PendingWrite pending_writes[MAX_PENDING_WRITES];
|
72 | 74 |
|
73 | 75 | /* Current size of the relation */
|
74 |
| - BlockNumber pages_written; |
| 76 | + BlockNumber relsize; |
75 | 77 |
|
76 | 78 | MemoryContext memcxt;
|
77 | 79 | };
|
@@ -110,7 +112,7 @@ smgr_bulk_start_smgr(SMgrRelation smgr, ForkNumber forknum, bool use_wal, char r
|
110 | 112 | state->relpersistence = relpersistence;
|
111 | 113 |
|
112 | 114 | state->npending = 0;
|
113 |
| - state->pages_written = 0; |
| 115 | + state->relsize = smgrnblocks(smgr, forknum); |
114 | 116 |
|
115 | 117 | /*
|
116 | 118 | * Remember the memory context. We will use it to allocate all the
|
@@ -167,11 +169,11 @@ smgr_bulk_flush(BulkWriteState *bulkstate)
|
167 | 169 | * Before we alloc buffers from buffer pool for those pages, extend the
|
168 | 170 | * underlying file first.
|
169 | 171 | */
|
170 |
| - if (nblocks > bulkstate->pages_written) |
| 172 | + if (nblocks > bulkstate->relsize) |
171 | 173 | {
|
172 |
| - smgrzeroextend(bulkstate->smgr, bulkstate->forknum, bulkstate->pages_written, |
173 |
| - nblocks - bulkstate->pages_written, true); |
174 |
| - bulkstate->pages_written = nblocks; |
| 174 | + smgrzeroextend(bulkstate->smgr, bulkstate->forknum, bulkstate->relsize, |
| 175 | + nblocks - bulkstate->relsize, true); |
| 176 | + bulkstate->relsize = nblocks; |
175 | 177 | }
|
176 | 178 |
|
177 | 179 | for (int i = 0; i < npending;)
|
|
0 commit comments