|
56 | 56 | * Portions Copyright (c) 1994, Regents of the University of California
|
57 | 57 | *
|
58 | 58 | * IDENTIFICATION
|
59 |
| - * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsort.c,v 1.85 2004/07/21 22:31:20 tgl Exp $ |
| 59 | + * $PostgreSQL: pgsql/src/backend/access/nbtree/nbtsort.c,v 1.86 2004/08/15 23:44:46 tgl Exp $ |
60 | 60 | *
|
61 | 61 | *-------------------------------------------------------------------------
|
62 | 62 | */
|
@@ -322,16 +322,15 @@ _bt_blwritepage(BTWriteState *wstate, Page page, BlockNumber blkno)
|
322 | 322 | wstate->btws_zeropage = (Page) palloc0(BLCKSZ);
|
323 | 323 | smgrwrite(wstate->index->rd_smgr, wstate->btws_pages_written++,
|
324 | 324 | (char *) wstate->btws_zeropage,
|
325 |
| - !wstate->btws_use_wal); |
| 325 | + true); |
326 | 326 | }
|
327 | 327 |
|
328 | 328 | /*
|
329 |
| - * Now write the page. If not using WAL, say isTemp = true, to suppress |
330 |
| - * duplicate fsync. If we are using WAL, it surely isn't a temp index, |
331 |
| - * so !use_wal is a sufficient condition. |
| 329 | + * Now write the page. We say isTemp = true even if it's not a |
| 330 | + * temp index, because there's no need for smgr to schedule an fsync |
| 331 | + * for this write; we'll do it ourselves before ending the build. |
332 | 332 | */
|
333 |
| - smgrwrite(wstate->index->rd_smgr, blkno, (char *) page, |
334 |
| - !wstate->btws_use_wal); |
| 333 | + smgrwrite(wstate->index->rd_smgr, blkno, (char *) page, true); |
335 | 334 |
|
336 | 335 | if (blkno == wstate->btws_pages_written)
|
337 | 336 | wstate->btws_pages_written++;
|
@@ -802,9 +801,20 @@ _bt_load(BTWriteState *wstate, BTSpool *btspool, BTSpool *btspool2)
|
802 | 801 | _bt_uppershutdown(wstate, state);
|
803 | 802 |
|
804 | 803 | /*
|
805 |
| - * If we weren't using WAL, and the index isn't temp, we must fsync it |
806 |
| - * down to disk before it's safe to commit the transaction. |
| 804 | + * If the index isn't temp, we must fsync it down to disk before it's |
| 805 | + * safe to commit the transaction. (For a temp index we don't care |
| 806 | + * since the index will be uninteresting after a crash anyway.) |
| 807 | + * |
| 808 | + * It's obvious that we must do this when not WAL-logging the build. |
| 809 | + * It's less obvious that we have to do it even if we did WAL-log the |
| 810 | + * index pages. The reason is that since we're building outside |
| 811 | + * shared buffers, a CHECKPOINT occurring during the build has no way |
| 812 | + * to flush the previously written data to disk (indeed it won't know |
| 813 | + * the index even exists). A crash later on would replay WAL from the |
| 814 | + * checkpoint, therefore it wouldn't replay our earlier WAL entries. |
| 815 | + * If we do not fsync those pages here, they might still not be on disk |
| 816 | + * when the crash occurs. |
807 | 817 | */
|
808 |
| - if (!wstate->btws_use_wal && !wstate->index->rd_istemp) |
| 818 | + if (!wstate->index->rd_istemp) |
809 | 819 | smgrimmedsync(wstate->index->rd_smgr);
|
810 | 820 | }
|
0 commit comments