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  * When we wrote out the pages, we passed skipFsync=true to avoid the
136  * overhead of registering all the writes with the checkpointer. Register
137  * the whole relation now.
138  *
139  * There is one hole in that idea: If a checkpoint occurred while we were
140  * writing the pages, it already missed fsyncing the pages we had written
141  * before the checkpoint started. A crash later on would replay the WAL
142  * starting from the checkpoint, therefore it wouldn't replay our earlier
143  * WAL records. So if a checkpoint started after the bulk write, fsync
144  * the files now.
145  */
146  if (!SmgrIsTemp(bulkstate->smgr))
147  {
148  /*
149  * Prevent a checkpoint from starting between the GetRedoRecPtr() and
150  * smgrregistersync() calls.
151  */
154 
155  if (bulkstate->start_RedoRecPtr != GetRedoRecPtr())
156  {
157  /*
158  * A checkpoint occurred and it didn't know about our writes, so
159  * fsync() the relation ourselves.
160  */
162  smgrimmedsync(bulkstate->smgr, bulkstate->forknum);
163  elog(DEBUG1, "flushed relation because a checkpoint occurred concurrently");
164  }
165  else
166  {
167  smgrregistersync(bulkstate->smgr, bulkstate->forknum);
169  }
170  }
171 }
static void smgr_bulk_flush(BulkWriteState *bulkstate)
Definition: bulk_write.c:191
#define Assert(condition)
Definition: c.h:858
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:224
#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:66
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:236
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6393

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

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 295 of file bulk_write.c.

296 {
297  return MemoryContextAllocAligned(bulkstate->memcxt, BLCKSZ, PG_IO_ALIGN_SIZE, 0);
298 }
void * MemoryContextAllocAligned(MemoryContext context, Size size, Size alignto, int flags)
Definition: mcxt.c:1408
#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:53

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 271 of file bulk_write.c.

272 {
273  PendingWrite *w;
274 
275  w = &bulkstate->pending_writes[bulkstate->npending++];
276  w->buf = buf;
277  w->blkno = blocknum;
278  w->page_std = page_std;
279 
280  if (bulkstate->npending == MAX_PENDING_WRITES)
281  smgr_bulk_flush(bulkstate);
282 }
#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().