PostgreSQL Source Code  git master
bulk_write.h File Reference
#include "storage/smgr.h"
#include "utils/rel.h"
Include dependency graph for bulk_write.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct BulkWriteState BulkWriteState
 
typedef PGIOAlignedBlockBulkWriteBuffer
 

Functions

BulkWriteStatesmgr_bulk_start_rel (Relation rel, ForkNumber forknum)
 
BulkWriteStatesmgr_bulk_start_smgr (struct SMgrRelationData *smgr, ForkNumber forknum, bool use_wal)
 
BulkWriteBuffer smgr_bulk_get_buf (BulkWriteState *bulkstate)
 
void smgr_bulk_write (BulkWriteState *bulkstate, BlockNumber blocknum, BulkWriteBuffer buf, bool page_std)
 
void smgr_bulk_finish (BulkWriteState *bulkstate)
 

Typedef Documentation

◆ BulkWriteBuffer

Definition at line 28 of file bulk_write.h.

◆ BulkWriteState

Definition at line 1 of file bulk_write.h.

Function Documentation

◆ smgr_bulk_finish()

void smgr_bulk_finish ( BulkWriteState bulkstate)

Definition at line 129 of file bulk_write.c.

130 {
131  /* WAL-log and flush any remaining pages */
132  smgr_bulk_flush(bulkstate);
133 
134  /*
135  * Fsync the relation, or register it for the next checkpoint, if
136  * necessary.
137  */
138  if (SmgrIsTemp(bulkstate->smgr))
139  {
140  /* Temporary relations don't need to be fsync'd, ever */
141  }
142  else if (!bulkstate->use_wal)
143  {
144  /*----------
145  * This is either an unlogged relation, or a permanent relation but we
146  * skipped WAL-logging because wal_level=minimal:
147  *
148  * A) Unlogged relation
149  *
150  * Unlogged relations will go away on crash, but they need to be
151  * fsync'd on a clean shutdown. It's sufficient to call
152  * smgrregistersync(), that ensures that the checkpointer will
153  * flush it at the shutdown checkpoint. (It will flush it on the
154  * next online checkpoint too, which is not strictly necessary.)
155  *
156  * Note that the init-fork of an unlogged relation is not
157  * considered unlogged for our purposes. It's treated like a
158  * regular permanent relation. The callers will pass use_wal=true
159  * for the init fork.
160  *
161  * B) Permanent relation, WAL-logging skipped because wal_level=minimal
162  *
163  * This is a new relation, and we didn't WAL-log the pages as we
164  * wrote, but they need to be fsync'd before commit.
165  *
166  * We don't need to do that here, however. The fsync() is done at
167  * commit, by smgrDoPendingSyncs() (*).
168  *
169  * (*) smgrDoPendingSyncs() might decide to WAL-log the whole
170  * relation at commit instead of fsyncing it, if the relation was
171  * very small, but it's smgrDoPendingSyncs() responsibility in any
172  * case.
173  *
174  * We cannot distinguish the two here, so conservatively assume it's
175  * an unlogged relation. A permanent relation with wal_level=minimal
176  * would require no actions, see above.
177  */
178  smgrregistersync(bulkstate->smgr, bulkstate->forknum);
179  }
180  else
181  {
182  /*
183  * Permanent relation, WAL-logged normally.
184  *
185  * We already WAL-logged all the pages, so they will be replayed from
186  * WAL on crash. However, when we wrote out the pages, we passed
187  * skipFsync=true to avoid the overhead of registering all the writes
188  * with the checkpointer. Register the whole relation now.
189  *
190  * There is one hole in that idea: If a checkpoint occurred while we
191  * were writing the pages, it already missed fsyncing the pages we had
192  * written before the checkpoint started. A crash later on would
193  * replay the WAL starting from the checkpoint, therefore it wouldn't
194  * replay our earlier WAL records. So if a checkpoint started after
195  * the bulk write, fsync the files now.
196  */
197 
198  /*
199  * Prevent a checkpoint from starting between the GetRedoRecPtr() and
200  * smgrregistersync() calls.
201  */
204 
205  if (bulkstate->start_RedoRecPtr != GetRedoRecPtr())
206  {
207  /*
208  * A checkpoint occurred and it didn't know about our writes, so
209  * fsync() the relation ourselves.
210  */
212  smgrimmedsync(bulkstate->smgr, bulkstate->forknum);
213  elog(DEBUG1, "flushed relation because a checkpoint occurred concurrently");
214  }
215  else
216  {
217  smgrregistersync(bulkstate->smgr, bulkstate->forknum);
219  }
220  }
221 }
static void smgr_bulk_flush(BulkWriteState *bulkstate)
Definition: bulk_write.c:241
#define Assert(condition)
Definition: c.h:858
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:225
#define DELAY_CHKPT_START
Definition: proc.h:114
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:789
void smgrregistersync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:757
#define SmgrIsTemp(smgr)
Definition: smgr.h:73
PGPROC * MyProc
Definition: proc.c:67
SMgrRelation smgr
Definition: bulk_write.c:63
XLogRecPtr start_RedoRecPtr
Definition: bulk_write.c:75
ForkNumber forknum
Definition: bulk_write.c:64
int delayChkptFlags
Definition: proc.h:235
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6436

References Assert, DEBUG1, DELAY_CHKPT_START, PGPROC::delayChkptFlags, elog, BulkWriteState::forknum, GetRedoRecPtr(), MyProc, BulkWriteState::smgr, smgr_bulk_flush(), smgrimmedsync(), SmgrIsTemp, smgrregistersync(), BulkWriteState::start_RedoRecPtr, and BulkWriteState::use_wal.

Referenced by _bt_load(), btbuildempty(), end_heap_rewrite(), gist_indexsortbuild(), RelationCopyStorage(), and spgbuildempty().

◆ smgr_bulk_get_buf()

BulkWriteBuffer smgr_bulk_get_buf ( BulkWriteState bulkstate)

Definition at line 345 of file bulk_write.c.

346 {
347  return MemoryContextAllocAligned(bulkstate->memcxt, BLCKSZ, PG_IO_ALIGN_SIZE, 0);
348 }
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
Definition: mcxt.c:1409
#define PG_IO_ALIGN_SIZE
MemoryContext memcxt
Definition: bulk_write.c:77

References BulkWriteState::memcxt, MemoryContextAllocAligned(), and PG_IO_ALIGN_SIZE.

Referenced by _bt_blnewpage(), _bt_uppershutdown(), btbuildempty(), gist_indexsortbuild(), gist_indexsortbuild_levelstate_flush(), raw_heap_insert(), RelationCopyStorage(), and spgbuildempty().

◆ smgr_bulk_start_rel()

BulkWriteState* smgr_bulk_start_rel ( Relation  rel,
ForkNumber  forknum 
)

Definition at line 86 of file bulk_write.c.

87 {
89  forknum,
90  RelationNeedsWAL(rel) || forknum == INIT_FORKNUM);
91 }
BulkWriteState * smgr_bulk_start_smgr(SMgrRelation smgr, ForkNumber forknum, bool use_wal)
Definition: bulk_write.c:99
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:567
#define RelationNeedsWAL(relation)
Definition: rel.h:628
@ INIT_FORKNUM
Definition: relpath.h:61

References INIT_FORKNUM, RelationGetSmgr(), RelationNeedsWAL, and smgr_bulk_start_smgr().

Referenced by _bt_load(), begin_heap_rewrite(), btbuildempty(), gist_indexsortbuild(), and spgbuildempty().

◆ smgr_bulk_start_smgr()

BulkWriteState* smgr_bulk_start_smgr ( struct SMgrRelationData smgr,
ForkNumber  forknum,
bool  use_wal 
)

◆ smgr_bulk_write()

void smgr_bulk_write ( BulkWriteState bulkstate,
BlockNumber  blocknum,
BulkWriteBuffer  buf,
bool  page_std 
)

Definition at line 321 of file bulk_write.c.

322 {
323  PendingWrite *w;
324 
325  w = &bulkstate->pending_writes[bulkstate->npending++];
326  w->buf = buf;
327  w->blkno = blocknum;
328  w->page_std = page_std;
329 
330  if (bulkstate->npending == MAX_PENDING_WRITES)
331  smgr_bulk_flush(bulkstate);
332 }
#define MAX_PENDING_WRITES
Definition: bulk_write.c:46
static char * buf
Definition: pg_test_fsync.c:73
PendingWrite pending_writes[MAX_PENDING_WRITES]
Definition: bulk_write.c:69
BulkWriteBuffer buf
Definition: bulk_write.c:52
BlockNumber blkno
Definition: bulk_write.c:53
bool page_std
Definition: bulk_write.c:54

References PendingWrite::blkno, PendingWrite::buf, buf, MAX_PENDING_WRITES, BulkWriteState::npending, PendingWrite::page_std, BulkWriteState::pending_writes, and smgr_bulk_flush().

Referenced by _bt_blwritepage(), btbuildempty(), end_heap_rewrite(), gist_indexsortbuild(), gist_indexsortbuild_levelstate_flush(), raw_heap_insert(), RelationCopyStorage(), and spgbuildempty().