PostgreSQL Source Code  git master
storage.c File Reference
#include "postgres.h"
#include "access/parallel.h"
#include "access/visibilitymap.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
#include "access/xlogutils.h"
#include "catalog/storage.h"
#include "catalog/storage_xlog.h"
#include "miscadmin.h"
#include "storage/freespace.h"
#include "storage/smgr.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
#include "utils/rel.h"
Include dependency graph for storage.c:

Go to the source code of this file.

Data Structures

struct  PendingRelDelete
 
struct  PendingRelSync
 

Typedefs

typedef struct PendingRelDelete PendingRelDelete
 
typedef struct PendingRelSync PendingRelSync
 

Functions

static void AddPendingSync (const RelFileNode *rnode)
 
SMgrRelation RelationCreateStorage (RelFileNode rnode, char relpersistence, bool register_delete)
 
void log_smgrcreate (const RelFileNode *rnode, ForkNumber forkNum)
 
void RelationDropStorage (Relation rel)
 
void RelationPreserveStorage (RelFileNode rnode, bool atCommit)
 
void RelationTruncate (Relation rel, BlockNumber nblocks)
 
void RelationPreTruncate (Relation rel)
 
void RelationCopyStorage (SMgrRelation src, SMgrRelation dst, ForkNumber forkNum, char relpersistence)
 
bool RelFileNodeSkippingWAL (RelFileNode rnode)
 
Size EstimatePendingSyncsSpace (void)
 
void SerializePendingSyncs (Size maxSize, char *startAddress)
 
void RestorePendingSyncs (char *startAddress)
 
void smgrDoPendingDeletes (bool isCommit)
 
void smgrDoPendingSyncs (bool isCommit, bool isParallelWorker)
 
int smgrGetPendingDeletes (bool forCommit, RelFileNode **ptr)
 
void PostPrepare_smgr (void)
 
void AtSubCommit_smgr (void)
 
void AtSubAbort_smgr (void)
 
void smgr_redo (XLogReaderState *record)
 

Variables

int wal_skip_threshold = 2048
 
static PendingRelDeletependingDeletes = NULL
 
static HTABpendingSyncHash = NULL
 

Typedef Documentation

◆ PendingRelDelete

◆ PendingRelSync

Function Documentation

◆ AddPendingSync()

static void AddPendingSync ( const RelFileNode rnode)
static

Definition at line 84 of file storage.c.

85 {
86  PendingRelSync *pending;
87  bool found;
88 
89  /* create the hash if not yet */
90  if (!pendingSyncHash)
91  {
92  HASHCTL ctl;
93 
94  ctl.keysize = sizeof(RelFileNode);
95  ctl.entrysize = sizeof(PendingRelSync);
97  pendingSyncHash = hash_create("pending sync hash", 16, &ctl,
99  }
100 
101  pending = hash_search(pendingSyncHash, rnode, HASH_ENTER, &found);
102  Assert(!found);
103  pending->is_truncated = false;
104 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
Assert(fmt[strlen(fmt) - 1] !='\n')
MemoryContext TopTransactionContext
Definition: mcxt.c:53
struct RelFileNode RelFileNode
static HTAB * pendingSyncHash
Definition: storage.c:76
struct PendingRelSync PendingRelSync
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
bool is_truncated
Definition: storage.c:72

References Assert(), HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), HASHCTL::hcxt, PendingRelSync::is_truncated, HASHCTL::keysize, pendingSyncHash, and TopTransactionContext.

Referenced by RelationCreateStorage(), and RestorePendingSyncs().

◆ AtSubAbort_smgr()

void AtSubAbort_smgr ( void  )

Definition at line 951 of file storage.c.

952 {
953  smgrDoPendingDeletes(false);
954 }
void smgrDoPendingDeletes(bool isCommit)
Definition: storage.c:649

References smgrDoPendingDeletes().

Referenced by AbortSubTransaction().

◆ AtSubCommit_smgr()

void AtSubCommit_smgr ( void  )

Definition at line 931 of file storage.c.

932 {
933  int nestLevel = GetCurrentTransactionNestLevel();
934  PendingRelDelete *pending;
935 
936  for (pending = pendingDeletes; pending != NULL; pending = pending->next)
937  {
938  if (pending->nestLevel >= nestLevel)
939  pending->nestLevel = nestLevel - 1;
940  }
941 }
static PendingRelDelete * pendingDeletes
Definition: storage.c:75
struct PendingRelDelete * next
Definition: storage.c:66
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:910

References GetCurrentTransactionNestLevel(), PendingRelDelete::nestLevel, PendingRelDelete::next, and pendingDeletes.

Referenced by CommitSubTransaction().

◆ EstimatePendingSyncsSpace()

Size EstimatePendingSyncsSpace ( void  )

Definition at line 564 of file storage.c.

565 {
566  long entries;
567 
569  return mul_size(1 + entries, sizeof(RelFileNode));
570 }
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1382
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519

References hash_get_num_entries(), mul_size(), and pendingSyncHash.

Referenced by InitializeParallelDSM().

◆ log_smgrcreate()

void log_smgrcreate ( const RelFileNode rnode,
ForkNumber  forkNum 
)

Definition at line 185 of file storage.c.

186 {
187  xl_smgr_create xlrec;
188 
189  /*
190  * Make an XLOG entry reporting the file creation.
191  */
192  xlrec.rnode = *rnode;
193  xlrec.forkNum = forkNum;
194 
195  XLogBeginInsert();
196  XLogRegisterData((char *) &xlrec, sizeof(xlrec));
198 }
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
ForkNumber forkNum
Definition: storage_xlog.h:36
RelFileNode rnode
Definition: storage_xlog.h:35
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:443
void XLogBeginInsert(void)
Definition: xloginsert.c:150
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:351
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71

References xl_smgr_create::forkNum, xl_smgr_create::rnode, XLOG_SMGR_CREATE, XLogBeginInsert(), XLogInsert(), XLogRegisterData(), and XLR_SPECIAL_REL_UPDATE.

Referenced by CreateAndCopyRelationData(), fill_seq_with_data(), heapam_relation_copy_data(), heapam_relation_set_new_filenode(), index_copy_data(), and RelationCreateStorage().

◆ PostPrepare_smgr()

void PostPrepare_smgr ( void  )

Definition at line 910 of file storage.c.

911 {
912  PendingRelDelete *pending;
914 
915  for (pending = pendingDeletes; pending != NULL; pending = next)
916  {
917  next = pending->next;
919  /* must explicitly free the list entry */
920  pfree(pending);
921  }
922 }
static int32 next
Definition: blutils.c:219
void pfree(void *pointer)
Definition: mcxt.c:1175

References next, PendingRelDelete::next, pendingDeletes, and pfree().

Referenced by PrepareTransaction().

◆ RelationCopyStorage()

void RelationCopyStorage ( SMgrRelation  src,
SMgrRelation  dst,
ForkNumber  forkNum,
char  relpersistence 
)

Definition at line 451 of file storage.c.

453 {
455  Page page;
456  bool use_wal;
457  bool copying_initfork;
458  BlockNumber nblocks;
459  BlockNumber blkno;
460 
461  page = (Page) buf.data;
462 
463  /*
464  * The init fork for an unlogged relation in many respects has to be
465  * treated the same as normal relation, changes need to be WAL logged and
466  * it needs to be synced to disk.
467  */
468  copying_initfork = relpersistence == RELPERSISTENCE_UNLOGGED &&
469  forkNum == INIT_FORKNUM;
470 
471  /*
472  * We need to log the copied data in WAL iff WAL archiving/streaming is
473  * enabled AND it's a permanent relation. This gives the same answer as
474  * "RelationNeedsWAL(rel) || copying_initfork", because we know the
475  * current operation created a new relfilenode.
476  */
477  use_wal = XLogIsNeeded() &&
478  (relpersistence == RELPERSISTENCE_PERMANENT || copying_initfork);
479 
480  nblocks = smgrnblocks(src, forkNum);
481 
482  for (blkno = 0; blkno < nblocks; blkno++)
483  {
484  /* If we got a cancel signal during the copy of the data, quit */
486 
487  smgrread(src, forkNum, blkno, buf.data);
488 
489  if (!PageIsVerifiedExtended(page, blkno,
491  {
492  /*
493  * For paranoia's sake, capture the file path before invoking the
494  * ereport machinery. This guards against the possibility of a
495  * relcache flush caused by, e.g., an errcontext callback.
496  * (errcontext callbacks shouldn't be risking any such thing, but
497  * people have been known to forget that rule.)
498  */
499  char *relpath = relpathbackend(src->smgr_rnode.node,
500  src->smgr_rnode.backend,
501  forkNum);
502 
503  ereport(ERROR,
505  errmsg("invalid page in block %u of relation %s",
506  blkno, relpath)));
507  }
508 
509  /*
510  * WAL-log the copied page. Unfortunately we don't know what kind of a
511  * page this is, so we have to log the full page including any unused
512  * space.
513  */
514  if (use_wal)
515  log_newpage(&dst->smgr_rnode.node, forkNum, blkno, page, false);
516 
517  PageSetChecksumInplace(page, blkno);
518 
519  /*
520  * Now write the page. We say skipFsync = true because there's no
521  * need for smgr to schedule an fsync for this write; we'll do it
522  * ourselves below.
523  */
524  smgrextend(dst, forkNum, blkno, buf.data, true);
525  }
526 
527  /*
528  * When we WAL-logged rel pages, we must nonetheless fsync them. The
529  * reason is that since we're copying outside shared buffers, a CHECKPOINT
530  * occurring during the copy has no way to flush the previously written
531  * data to disk (indeed it won't know the new rel even exists). A crash
532  * later on would replay WAL from the checkpoint, therefore it wouldn't
533  * replay our earlier WAL entries. If we do not fsync those pages here,
534  * they might still not be on disk when the crash occurs.
535  */
536  if (use_wal || copying_initfork)
537  smgrimmedsync(dst, forkNum);
538 }
uint32 BlockNumber
Definition: block.h:31
bool PageIsVerifiedExtended(Page page, BlockNumber blkno, int flags)
Definition: bufpage.c:88
void PageSetChecksumInplace(Page page, BlockNumber blkno)
Definition: bufpage.c:1539
Pointer Page
Definition: bufpage.h:78
#define PIV_LOG_WARNING
Definition: bufpage.h:412
#define PIV_REPORT_STAT
Definition: bufpage.h:413
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:43
static char * buf
Definition: pg_test_fsync.c:67
@ INIT_FORKNUM
Definition: relpath.h:46
#define relpath(rnode, forknum)
Definition: relpath.h:87
#define relpathbackend(rnode, backend, forknum)
Definition: relpath.h:78
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:579
void smgrextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer, bool skipFsync)
Definition: smgr.c:493
void smgrimmedsync(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:691
void smgrread(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, char *buffer)
Definition: smgr.c:532
BackendId backend
Definition: relfilenode.h:75
RelFileNode node
Definition: relfilenode.h:74
RelFileNodeBackend smgr_rnode
Definition: smgr.h:42
#define XLogIsNeeded()
Definition: xlog.h:104
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:1083

References RelFileNodeBackend::backend, buf, CHECK_FOR_INTERRUPTS, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, INIT_FORKNUM, log_newpage(), RelFileNodeBackend::node, PageIsVerifiedExtended(), PageSetChecksumInplace(), PIV_LOG_WARNING, PIV_REPORT_STAT, relpath, relpathbackend, SMgrRelationData::smgr_rnode, smgrextend(), smgrimmedsync(), smgrnblocks(), smgrread(), and XLogIsNeeded.

Referenced by heapam_relation_copy_data(), and index_copy_data().

◆ RelationCreateStorage()

SMgrRelation RelationCreateStorage ( RelFileNode  rnode,
char  relpersistence,
bool  register_delete 
)

Definition at line 120 of file storage.c.

122 {
123  SMgrRelation srel;
124  BackendId backend;
125  bool needs_wal;
126 
127  Assert(!IsInParallelMode()); /* couldn't update pendingSyncHash */
128 
129  switch (relpersistence)
130  {
131  case RELPERSISTENCE_TEMP:
132  backend = BackendIdForTempRelations();
133  needs_wal = false;
134  break;
135  case RELPERSISTENCE_UNLOGGED:
136  backend = InvalidBackendId;
137  needs_wal = false;
138  break;
139  case RELPERSISTENCE_PERMANENT:
140  backend = InvalidBackendId;
141  needs_wal = true;
142  break;
143  default:
144  elog(ERROR, "invalid relpersistence: %c", relpersistence);
145  return NULL; /* placate compiler */
146  }
147 
148  srel = smgropen(rnode, backend);
149  smgrcreate(srel, MAIN_FORKNUM, false);
150 
151  if (needs_wal)
153 
154  /*
155  * Add the relation to the list of stuff to delete at abort, if we are
156  * asked to do so.
157  */
158  if (register_delete)
159  {
160  PendingRelDelete *pending;
161 
162  pending = (PendingRelDelete *)
164  pending->relnode = rnode;
165  pending->backend = backend;
166  pending->atCommit = false; /* delete if abort */
168  pending->next = pendingDeletes;
169  pendingDeletes = pending;
170  }
171 
172  if (relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded())
173  {
174  Assert(backend == InvalidBackendId);
175  AddPendingSync(&rnode);
176  }
177 
178  return srel;
179 }
int BackendId
Definition: backendid.h:21
#define BackendIdForTempRelations()
Definition: backendid.h:34
#define InvalidBackendId
Definition: backendid.h:23
#define elog(elevel,...)
Definition: elog.h:218
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
@ MAIN_FORKNUM
Definition: relpath.h:43
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:369
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:146
void log_smgrcreate(const RelFileNode *rnode, ForkNumber forkNum)
Definition: storage.c:185
static void AddPendingSync(const RelFileNode *rnode)
Definition: storage.c:84
RelFileNode relnode
Definition: storage.c:62
BackendId backend
Definition: storage.c:63
bool IsInParallelMode(void)
Definition: xact.c:1065

References AddPendingSync(), Assert(), PendingRelDelete::atCommit, PendingRelDelete::backend, BackendIdForTempRelations, elog, ERROR, GetCurrentTransactionNestLevel(), InvalidBackendId, IsInParallelMode(), log_smgrcreate(), MAIN_FORKNUM, MemoryContextAlloc(), PendingRelDelete::nestLevel, PendingRelDelete::next, RelFileNodeBackend::node, pendingDeletes, PendingRelDelete::relnode, SMgrRelationData::smgr_rnode, smgrcreate(), smgropen(), TopMemoryContext, and XLogIsNeeded.

Referenced by CreateAndCopyRelationData(), heap_create(), heapam_relation_copy_data(), heapam_relation_set_new_filenode(), index_copy_data(), and RelationSetNewRelfilenode().

◆ RelationDropStorage()

void RelationDropStorage ( Relation  rel)

Definition at line 205 of file storage.c.

206 {
207  PendingRelDelete *pending;
208 
209  /* Add the relation to the list of stuff to delete at commit */
210  pending = (PendingRelDelete *)
212  pending->relnode = rel->rd_node;
213  pending->backend = rel->rd_backend;
214  pending->atCommit = true; /* delete if commit */
216  pending->next = pendingDeletes;
217  pendingDeletes = pending;
218 
219  /*
220  * NOTE: if the relation was created in this transaction, it will now be
221  * present in the pending-delete list twice, once with atCommit true and
222  * once with atCommit false. Hence, it will be physically deleted at end
223  * of xact in either case (and the other entry will be ignored by
224  * smgrDoPendingDeletes, so no error will occur). We could instead remove
225  * the existing list entry and delete the physical file immediately, but
226  * for now I'll keep the logic simple.
227  */
228 
229  RelationCloseSmgr(rel);
230 }
#define RelationCloseSmgr(relation)
Definition: rel.h:569
RelFileNode rd_node
Definition: rel.h:56
BackendId rd_backend
Definition: rel.h:59

References PendingRelDelete::atCommit, PendingRelDelete::backend, GetCurrentTransactionNestLevel(), MemoryContextAlloc(), PendingRelDelete::nestLevel, PendingRelDelete::next, pendingDeletes, RelationData::rd_backend, RelationData::rd_node, RelationCloseSmgr, PendingRelDelete::relnode, and TopMemoryContext.

Referenced by DefineQueryRewrite(), heap_drop_with_catalog(), heapam_relation_copy_data(), index_copy_data(), index_drop(), reindex_index(), and RelationSetNewRelfilenode().

◆ RelationPreserveStorage()

void RelationPreserveStorage ( RelFileNode  rnode,
bool  atCommit 
)

Definition at line 250 of file storage.c.

251 {
252  PendingRelDelete *pending;
253  PendingRelDelete *prev;
255 
256  prev = NULL;
257  for (pending = pendingDeletes; pending != NULL; pending = next)
258  {
259  next = pending->next;
260  if (RelFileNodeEquals(rnode, pending->relnode)
261  && pending->atCommit == atCommit)
262  {
263  /* unlink and delete list entry */
264  if (prev)
265  prev->next = next;
266  else
268  pfree(pending);
269  /* prev does not change */
270  }
271  else
272  {
273  /* unrelated entry, don't touch it */
274  prev = pending;
275  }
276  }
277 }
#define RelFileNodeEquals(node1, node2)
Definition: relfilenode.h:88

References PendingRelDelete::atCommit, next, PendingRelDelete::next, pendingDeletes, pfree(), RelFileNodeEquals, and PendingRelDelete::relnode.

Referenced by ATExecAddIndex(), and write_relmap_file().

◆ RelationPreTruncate()

void RelationPreTruncate ( Relation  rel)

Definition at line 423 of file storage.c.

424 {
425  PendingRelSync *pending;
426 
427  if (!pendingSyncHash)
428  return;
429 
430  pending = hash_search(pendingSyncHash,
431  &(RelationGetSmgr(rel)->smgr_rnode.node),
432  HASH_FIND, NULL);
433  if (pending)
434  pending->is_truncated = true;
435 }
@ HASH_FIND
Definition: hsearch.h:113
static SMgrRelation RelationGetSmgr(Relation rel)
Definition: rel.h:556
dlist_node node
Definition: smgr.h:72

References HASH_FIND, hash_search(), PendingRelSync::is_truncated, SMgrRelationData::node, pendingSyncHash, and RelationGetSmgr().

Referenced by RelationTruncate().

◆ RelationTruncate()

void RelationTruncate ( Relation  rel,
BlockNumber  nblocks 
)

Definition at line 287 of file storage.c.

288 {
289  bool fsm;
290  bool vm;
291  bool need_fsm_vacuum = false;
292  ForkNumber forks[MAX_FORKNUM];
293  BlockNumber blocks[MAX_FORKNUM];
294  int nforks = 0;
295  SMgrRelation reln;
296 
297  /*
298  * Make sure smgr_targblock etc aren't pointing somewhere past new end.
299  * (Note: don't rely on this reln pointer below this loop.)
300  */
301  reln = RelationGetSmgr(rel);
303  for (int i = 0; i <= MAX_FORKNUM; ++i)
305 
306  /* Prepare for truncation of MAIN fork of the relation */
307  forks[nforks] = MAIN_FORKNUM;
308  blocks[nforks] = nblocks;
309  nforks++;
310 
311  /* Prepare for truncation of the FSM if it exists */
313  if (fsm)
314  {
315  blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, nblocks);
316  if (BlockNumberIsValid(blocks[nforks]))
317  {
318  forks[nforks] = FSM_FORKNUM;
319  nforks++;
320  need_fsm_vacuum = true;
321  }
322  }
323 
324  /* Prepare for truncation of the visibility map too if it exists */
326  if (vm)
327  {
328  blocks[nforks] = visibilitymap_prepare_truncate(rel, nblocks);
329  if (BlockNumberIsValid(blocks[nforks]))
330  {
331  forks[nforks] = VISIBILITYMAP_FORKNUM;
332  nforks++;
333  }
334  }
335 
336  RelationPreTruncate(rel);
337 
338  /*
339  * Make sure that a concurrent checkpoint can't complete while truncation
340  * is in progress.
341  *
342  * The truncation operation might drop buffers that the checkpoint
343  * otherwise would have flushed. If it does, then it's essential that the
344  * files actually get truncated on disk before the checkpoint record is
345  * written. Otherwise, if reply begins from that checkpoint, the
346  * to-be-truncated blocks might still exist on disk but have older
347  * contents than expected, which can cause replay to fail. It's OK for the
348  * blocks to not exist on disk at all, but not for them to have the wrong
349  * contents.
350  */
353 
354  /*
355  * We WAL-log the truncation before actually truncating, which means
356  * trouble if the truncation fails. If we then crash, the WAL replay
357  * likely isn't going to succeed in the truncation either, and cause a
358  * PANIC. It's tempting to put a critical section here, but that cure
359  * would be worse than the disease. It would turn a usually harmless
360  * failure to truncate, that might spell trouble at WAL replay, into a
361  * certain PANIC.
362  */
363  if (RelationNeedsWAL(rel))
364  {
365  /*
366  * Make an XLOG entry reporting the file truncation.
367  */
368  XLogRecPtr lsn;
369  xl_smgr_truncate xlrec;
370 
371  xlrec.blkno = nblocks;
372  xlrec.rnode = rel->rd_node;
373  xlrec.flags = SMGR_TRUNCATE_ALL;
374 
375  XLogBeginInsert();
376  XLogRegisterData((char *) &xlrec, sizeof(xlrec));
377 
378  lsn = XLogInsert(RM_SMGR_ID,
380 
381  /*
382  * Flush, because otherwise the truncation of the main relation might
383  * hit the disk before the WAL record, and the truncation of the FSM
384  * or visibility map. If we crashed during that window, we'd be left
385  * with a truncated heap, but the FSM or visibility map would still
386  * contain entries for the non-existent heap pages.
387  */
388  if (fsm || vm)
389  XLogFlush(lsn);
390  }
391 
392  /*
393  * This will first remove any buffers from the buffer pool that should no
394  * longer exist after truncation is complete, and then truncate the
395  * corresponding files on disk.
396  */
397  smgrtruncate(RelationGetSmgr(rel), forks, nforks, blocks);
398 
399  /* We've done all the critical work, so checkpoints are OK now. */
401 
402  /*
403  * Update upper-level FSM pages to account for the truncation. This is
404  * important because the just-truncated pages were likely marked as
405  * all-free, and would be preferentially selected.
406  *
407  * NB: There's no point in delaying checkpoints until this is done.
408  * Because the FSM is not WAL-logged, we have to be prepared for the
409  * possibility of corruption after a crash anyway.
410  */
411  if (need_fsm_vacuum)
413 }
#define InvalidBlockNumber
Definition: block.h:33
#define BlockNumberIsValid(blockNumber)
Definition: block.h:70
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:354
BlockNumber FreeSpaceMapPrepareTruncateRel(Relation rel, BlockNumber nblocks)
Definition: freespace.c:263
int i
Definition: isn.c:73
#define DELAY_CHKPT_COMPLETE
Definition: proc.h:118
#define RelationNeedsWAL(relation)
Definition: rel.h:613
ForkNumber
Definition: relpath.h:41
@ FSM_FORKNUM
Definition: relpath.h:44
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:45
#define MAX_FORKNUM
Definition: relpath.h:55
void smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *nblocks)
Definition: smgr.c:626
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:247
PGPROC * MyProc
Definition: proc.c:68
void RelationPreTruncate(Relation rel)
Definition: storage.c:423
#define SMGR_TRUNCATE_ALL
Definition: storage_xlog.h:43
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
int delayChkptFlags
Definition: proc.h:225
BlockNumber smgr_targblock
Definition: smgr.h:53
BlockNumber smgr_cached_nblocks[MAX_FORKNUM+1]
Definition: smgr.h:54
BlockNumber blkno
Definition: storage_xlog.h:48
RelFileNode rnode
Definition: storage_xlog.h:49
BlockNumber visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2509
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References Assert(), xl_smgr_truncate::blkno, BlockNumberIsValid, DELAY_CHKPT_COMPLETE, PGPROC::delayChkptFlags, xl_smgr_truncate::flags, FreeSpaceMapPrepareTruncateRel(), FreeSpaceMapVacuumRange(), FSM_FORKNUM, i, InvalidBlockNumber, MAIN_FORKNUM, MAX_FORKNUM, MyProc, RelationData::rd_node, RelationGetSmgr(), RelationNeedsWAL, RelationPreTruncate(), xl_smgr_truncate::rnode, SMgrRelationData::smgr_cached_nblocks, SMgrRelationData::smgr_targblock, SMGR_TRUNCATE_ALL, smgrexists(), smgrtruncate(), VISIBILITYMAP_FORKNUM, visibilitymap_prepare_truncate(), XLOG_SMGR_TRUNCATE, XLogBeginInsert(), XLogFlush(), XLogInsert(), XLogRegisterData(), and XLR_SPECIAL_REL_UPDATE.

Referenced by heapam_relation_nontransactional_truncate(), lazy_truncate_heap(), RelationTruncateIndexes(), and spgvacuumscan().

◆ RelFileNodeSkippingWAL()

bool RelFileNodeSkippingWAL ( RelFileNode  rnode)

Definition at line 550 of file storage.c.

551 {
552  if (!pendingSyncHash ||
553  hash_search(pendingSyncHash, &rnode, HASH_FIND, NULL) == NULL)
554  return false;
555 
556  return true;
557 }

References HASH_FIND, hash_search(), and pendingSyncHash.

Referenced by MarkBufferDirtyHint(), and RelationInitPhysicalAddr().

◆ RestorePendingSyncs()

void RestorePendingSyncs ( char *  startAddress)

Definition at line 628 of file storage.c.

629 {
630  RelFileNode *rnode;
631 
632  Assert(pendingSyncHash == NULL);
633  for (rnode = (RelFileNode *) startAddress; rnode->relNode != 0; rnode++)
634  AddPendingSync(rnode);
635 }

References AddPendingSync(), Assert(), pendingSyncHash, and RelFileNode::relNode.

Referenced by ParallelWorkerMain().

◆ SerializePendingSyncs()

void SerializePendingSyncs ( Size  maxSize,
char *  startAddress 
)

Definition at line 577 of file storage.c.

578 {
579  HTAB *tmphash;
580  HASHCTL ctl;
581  HASH_SEQ_STATUS scan;
582  PendingRelSync *sync;
583  PendingRelDelete *delete;
584  RelFileNode *src;
585  RelFileNode *dest = (RelFileNode *) startAddress;
586 
587  if (!pendingSyncHash)
588  goto terminate;
589 
590  /* Create temporary hash to collect active relfilenodes */
591  ctl.keysize = sizeof(RelFileNode);
592  ctl.entrysize = sizeof(RelFileNode);
594  tmphash = hash_create("tmp relfilenodes",
597 
598  /* collect all rnodes from pending syncs */
600  while ((sync = (PendingRelSync *) hash_seq_search(&scan)))
601  (void) hash_search(tmphash, &sync->rnode, HASH_ENTER, NULL);
602 
603  /* remove deleted rnodes */
604  for (delete = pendingDeletes; delete != NULL; delete = delete->next)
605  if (delete->atCommit)
606  (void) hash_search(tmphash, (void *) &delete->relnode,
607  HASH_REMOVE, NULL);
608 
609  hash_seq_init(&scan, tmphash);
610  while ((src = (RelFileNode *) hash_seq_search(&scan)))
611  *dest++ = *src;
612 
613  hash_destroy(tmphash);
614 
615 terminate:
616  MemSet(dest, 0, sizeof(RelFileNode));
617 }
#define MemSet(start, val, len)
Definition: c.h:1008
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
@ HASH_REMOVE
Definition: hsearch.h:115
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
Definition: dynahash.c:220
RelFileNode rnode
Definition: storage.c:71

References CurrentMemoryContext, generate_unaccent_rules::dest, HASHCTL::entrysize, HASH_BLOBS, HASH_CONTEXT, hash_create(), hash_destroy(), HASH_ELEM, HASH_ENTER, hash_get_num_entries(), HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), HASHCTL::hcxt, HASHCTL::keysize, MemSet, PendingRelDelete::next, pendingDeletes, pendingSyncHash, and PendingRelSync::rnode.

Referenced by InitializeParallelDSM().

◆ smgr_redo()

void smgr_redo ( XLogReaderState record)

Definition at line 957 of file storage.c.

958 {
959  XLogRecPtr lsn = record->EndRecPtr;
960  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
961 
962  /* Backup blocks are not used in smgr records */
963  Assert(!XLogRecHasAnyBlockRefs(record));
964 
965  if (info == XLOG_SMGR_CREATE)
966  {
967  xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
968  SMgrRelation reln;
969 
970  reln = smgropen(xlrec->rnode, InvalidBackendId);
971  smgrcreate(reln, xlrec->forkNum, true);
972  }
973  else if (info == XLOG_SMGR_TRUNCATE)
974  {
975  xl_smgr_truncate *xlrec = (xl_smgr_truncate *) XLogRecGetData(record);
976  SMgrRelation reln;
977  Relation rel;
978  ForkNumber forks[MAX_FORKNUM];
979  BlockNumber blocks[MAX_FORKNUM];
980  int nforks = 0;
981  bool need_fsm_vacuum = false;
982 
983  reln = smgropen(xlrec->rnode, InvalidBackendId);
984 
985  /*
986  * Forcibly create relation if it doesn't exist (which suggests that
987  * it was dropped somewhere later in the WAL sequence). As in
988  * XLogReadBufferForRedo, we prefer to recreate the rel and replay the
989  * log as best we can until the drop is seen.
990  */
991  smgrcreate(reln, MAIN_FORKNUM, true);
992 
993  /*
994  * Before we perform the truncation, update minimum recovery point to
995  * cover this WAL record. Once the relation is truncated, there's no
996  * going back. The buffer manager enforces the WAL-first rule for
997  * normal updates to relation files, so that the minimum recovery
998  * point is always updated before the corresponding change in the data
999  * file is flushed to disk. We have to do the same manually here.
1000  *
1001  * Doing this before the truncation means that if the truncation fails
1002  * for some reason, you cannot start up the system even after restart,
1003  * until you fix the underlying situation so that the truncation will
1004  * succeed. Alternatively, we could update the minimum recovery point
1005  * after truncation, but that would leave a small window where the
1006  * WAL-first rule could be violated.
1007  */
1008  XLogFlush(lsn);
1009 
1010  /* Prepare for truncation of MAIN fork */
1011  if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
1012  {
1013  forks[nforks] = MAIN_FORKNUM;
1014  blocks[nforks] = xlrec->blkno;
1015  nforks++;
1016 
1017  /* Also tell xlogutils.c about it */
1018  XLogTruncateRelation(xlrec->rnode, MAIN_FORKNUM, xlrec->blkno);
1019  }
1020 
1021  /* Prepare for truncation of FSM and VM too */
1022  rel = CreateFakeRelcacheEntry(xlrec->rnode);
1023 
1024  if ((xlrec->flags & SMGR_TRUNCATE_FSM) != 0 &&
1025  smgrexists(reln, FSM_FORKNUM))
1026  {
1027  blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, xlrec->blkno);
1028  if (BlockNumberIsValid(blocks[nforks]))
1029  {
1030  forks[nforks] = FSM_FORKNUM;
1031  nforks++;
1032  need_fsm_vacuum = true;
1033  }
1034  }
1035  if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0 &&
1037  {
1038  blocks[nforks] = visibilitymap_prepare_truncate(rel, xlrec->blkno);
1039  if (BlockNumberIsValid(blocks[nforks]))
1040  {
1041  forks[nforks] = VISIBILITYMAP_FORKNUM;
1042  nforks++;
1043  }
1044  }
1045 
1046  /* Do the real work to truncate relation forks */
1047  if (nforks > 0)
1048  smgrtruncate(reln, forks, nforks, blocks);
1049 
1050  /*
1051  * Update upper-level FSM pages to account for the truncation. This is
1052  * important because the just-truncated pages were likely marked as
1053  * all-free, and would be preferentially selected.
1054  */
1055  if (need_fsm_vacuum)
1056  FreeSpaceMapVacuumRange(rel, xlrec->blkno,
1058 
1059  FreeFakeRelcacheEntry(rel);
1060  }
1061  else
1062  elog(PANIC, "smgr_redo: unknown op code %u", info);
1063 }
unsigned char uint8
Definition: c.h:439
#define PANIC
Definition: elog.h:36
#define SMGR_TRUNCATE_VM
Definition: storage_xlog.h:41
#define SMGR_TRUNCATE_HEAP
Definition: storage_xlog.h:40
#define SMGR_TRUNCATE_FSM
Definition: storage_xlog.h:42
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:408
#define XLogRecGetData(decoder)
Definition: xlogreader.h:413
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:415
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
void XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, BlockNumber nblocks)
Definition: xlogutils.c:685
void FreeFakeRelcacheEntry(Relation fakerel)
Definition: xlogutils.c:640
Relation CreateFakeRelcacheEntry(RelFileNode rnode)
Definition: xlogutils.c:597

References Assert(), xl_smgr_truncate::blkno, BlockNumberIsValid, CreateFakeRelcacheEntry(), elog, XLogReaderState::EndRecPtr, xl_smgr_truncate::flags, xl_smgr_create::forkNum, FreeFakeRelcacheEntry(), FreeSpaceMapPrepareTruncateRel(), FreeSpaceMapVacuumRange(), FSM_FORKNUM, InvalidBackendId, InvalidBlockNumber, MAIN_FORKNUM, MAX_FORKNUM, PANIC, xl_smgr_create::rnode, xl_smgr_truncate::rnode, SMGR_TRUNCATE_FSM, SMGR_TRUNCATE_HEAP, SMGR_TRUNCATE_VM, smgrcreate(), smgrexists(), smgropen(), smgrtruncate(), VISIBILITYMAP_FORKNUM, visibilitymap_prepare_truncate(), XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, XLogFlush(), XLogRecGetData, XLogRecGetInfo, XLogRecHasAnyBlockRefs, XLogTruncateRelation(), and XLR_INFO_MASK.

◆ smgrDoPendingDeletes()

void smgrDoPendingDeletes ( bool  isCommit)

Definition at line 649 of file storage.c.

650 {
651  int nestLevel = GetCurrentTransactionNestLevel();
652  PendingRelDelete *pending;
653  PendingRelDelete *prev;
655  int nrels = 0,
656  maxrels = 0;
657  SMgrRelation *srels = NULL;
658 
659  prev = NULL;
660  for (pending = pendingDeletes; pending != NULL; pending = next)
661  {
662  next = pending->next;
663  if (pending->nestLevel < nestLevel)
664  {
665  /* outer-level entries should not be processed yet */
666  prev = pending;
667  }
668  else
669  {
670  /* unlink list entry first, so we don't retry on failure */
671  if (prev)
672  prev->next = next;
673  else
675  /* do deletion if called for */
676  if (pending->atCommit == isCommit)
677  {
678  SMgrRelation srel;
679 
680  srel = smgropen(pending->relnode, pending->backend);
681 
682  /* allocate the initial array, or extend it, if needed */
683  if (maxrels == 0)
684  {
685  maxrels = 8;
686  srels = palloc(sizeof(SMgrRelation) * maxrels);
687  }
688  else if (maxrels <= nrels)
689  {
690  maxrels *= 2;
691  srels = repalloc(srels, sizeof(SMgrRelation) * maxrels);
692  }
693 
694  srels[nrels++] = srel;
695  }
696  /* must explicitly free the list entry */
697  pfree(pending);
698  /* prev does not change */
699  }
700  }
701 
702  if (nrels > 0)
703  {
704  smgrdounlinkall(srels, nrels, false);
705 
706  for (int i = 0; i < nrels; i++)
707  smgrclose(srels[i]);
708 
709  pfree(srels);
710  }
711 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1188
void * palloc(Size size)
Definition: mcxt.c:1068
void smgrclose(SMgrRelation reln)
Definition: smgr.c:256
void smgrdounlinkall(SMgrRelation *rels, int nrels, bool isRedo)
Definition: smgr.c:420

References PendingRelDelete::atCommit, PendingRelDelete::backend, GetCurrentTransactionNestLevel(), i, PendingRelDelete::nestLevel, next, PendingRelDelete::next, palloc(), pendingDeletes, pfree(), PendingRelDelete::relnode, repalloc(), smgrclose(), smgrdounlinkall(), and smgropen().

Referenced by AbortTransaction(), AtSubAbort_smgr(), and CommitTransaction().

◆ smgrDoPendingSyncs()

void smgrDoPendingSyncs ( bool  isCommit,
bool  isParallelWorker 
)

Definition at line 717 of file storage.c.

718 {
719  PendingRelDelete *pending;
720  int nrels = 0,
721  maxrels = 0;
722  SMgrRelation *srels = NULL;
723  HASH_SEQ_STATUS scan;
724  PendingRelSync *pendingsync;
725 
727 
728  if (!pendingSyncHash)
729  return; /* no relation needs sync */
730 
731  /* Abort -- just throw away all pending syncs */
732  if (!isCommit)
733  {
734  pendingSyncHash = NULL;
735  return;
736  }
737 
739 
740  /* Parallel worker -- just throw away all pending syncs */
741  if (isParallelWorker)
742  {
743  pendingSyncHash = NULL;
744  return;
745  }
746 
747  /* Skip syncing nodes that smgrDoPendingDeletes() will delete. */
748  for (pending = pendingDeletes; pending != NULL; pending = pending->next)
749  if (pending->atCommit)
750  (void) hash_search(pendingSyncHash, (void *) &pending->relnode,
751  HASH_REMOVE, NULL);
752 
754  while ((pendingsync = (PendingRelSync *) hash_seq_search(&scan)))
755  {
756  ForkNumber fork;
757  BlockNumber nblocks[MAX_FORKNUM + 1];
758  BlockNumber total_blocks = 0;
759  SMgrRelation srel;
760 
761  srel = smgropen(pendingsync->rnode, InvalidBackendId);
762 
763  /*
764  * We emit newpage WAL records for smaller relations.
765  *
766  * Small WAL records have a chance to be emitted along with other
767  * backends' WAL records. We emit WAL records instead of syncing for
768  * files that are smaller than a certain threshold, expecting faster
769  * commit. The threshold is defined by the GUC wal_skip_threshold.
770  */
771  if (!pendingsync->is_truncated)
772  {
773  for (fork = 0; fork <= MAX_FORKNUM; fork++)
774  {
775  if (smgrexists(srel, fork))
776  {
777  BlockNumber n = smgrnblocks(srel, fork);
778 
779  /* we shouldn't come here for unlogged relations */
780  Assert(fork != INIT_FORKNUM);
781  nblocks[fork] = n;
782  total_blocks += n;
783  }
784  else
785  nblocks[fork] = InvalidBlockNumber;
786  }
787  }
788 
789  /*
790  * Sync file or emit WAL records for its contents.
791  *
792  * Although we emit WAL record if the file is small enough, do file
793  * sync regardless of the size if the file has experienced a
794  * truncation. It is because the file would be followed by trailing
795  * garbage blocks after a crash recovery if, while a past longer file
796  * had been flushed out, we omitted syncing-out of the file and
797  * emitted WAL instead. You might think that we could choose WAL if
798  * the current main fork is longer than ever, but there's a case where
799  * main fork is longer than ever but FSM fork gets shorter.
800  */
801  if (pendingsync->is_truncated ||
802  total_blocks * BLCKSZ / 1024 >= wal_skip_threshold)
803  {
804  /* allocate the initial array, or extend it, if needed */
805  if (maxrels == 0)
806  {
807  maxrels = 8;
808  srels = palloc(sizeof(SMgrRelation) * maxrels);
809  }
810  else if (maxrels <= nrels)
811  {
812  maxrels *= 2;
813  srels = repalloc(srels, sizeof(SMgrRelation) * maxrels);
814  }
815 
816  srels[nrels++] = srel;
817  }
818  else
819  {
820  /* Emit WAL records for all blocks. The file is small enough. */
821  for (fork = 0; fork <= MAX_FORKNUM; fork++)
822  {
823  int n = nblocks[fork];
824  Relation rel;
825 
826  if (!BlockNumberIsValid(n))
827  continue;
828 
829  /*
830  * Emit WAL for the whole file. Unfortunately we don't know
831  * what kind of a page this is, so we have to log the full
832  * page including any unused space. ReadBufferExtended()
833  * counts some pgstat events; unfortunately, we discard them.
834  */
836  log_newpage_range(rel, fork, 0, n, false);
838  }
839  }
840  }
841 
842  pendingSyncHash = NULL;
843 
844  if (nrels > 0)
845  {
846  smgrdosyncall(srels, nrels);
847  pfree(srels);
848  }
849 }
#define AssertPendingSyncs_RelationCache()
Definition: relcache.h:133
void smgrdosyncall(SMgrRelation *rels, int nrels)
Definition: smgr.c:384
int wal_skip_threshold
Definition: storage.c:38
void log_newpage_range(Relation rel, ForkNumber forkNum, BlockNumber startblk, BlockNumber endblk, bool page_std)
Definition: xloginsert.c:1210

References Assert(), AssertPendingSyncs_RelationCache, PendingRelDelete::atCommit, BlockNumberIsValid, CreateFakeRelcacheEntry(), FreeFakeRelcacheEntry(), GetCurrentTransactionNestLevel(), HASH_REMOVE, hash_search(), hash_seq_init(), hash_seq_search(), INIT_FORKNUM, InvalidBackendId, InvalidBlockNumber, PendingRelSync::is_truncated, log_newpage_range(), MAX_FORKNUM, PendingRelDelete::next, RelFileNodeBackend::node, palloc(), pendingDeletes, pendingSyncHash, pfree(), PendingRelDelete::relnode, repalloc(), PendingRelSync::rnode, SMgrRelationData::smgr_rnode, smgrdosyncall(), smgrexists(), smgrnblocks(), smgropen(), and wal_skip_threshold.

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

◆ smgrGetPendingDeletes()

int smgrGetPendingDeletes ( bool  forCommit,
RelFileNode **  ptr 
)

Definition at line 869 of file storage.c.

870 {
871  int nestLevel = GetCurrentTransactionNestLevel();
872  int nrels;
873  RelFileNode *rptr;
874  PendingRelDelete *pending;
875 
876  nrels = 0;
877  for (pending = pendingDeletes; pending != NULL; pending = pending->next)
878  {
879  if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
880  && pending->backend == InvalidBackendId)
881  nrels++;
882  }
883  if (nrels == 0)
884  {
885  *ptr = NULL;
886  return 0;
887  }
888  rptr = (RelFileNode *) palloc(nrels * sizeof(RelFileNode));
889  *ptr = rptr;
890  for (pending = pendingDeletes; pending != NULL; pending = pending->next)
891  {
892  if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
893  && pending->backend == InvalidBackendId)
894  {
895  *rptr = pending->relnode;
896  rptr++;
897  }
898  }
899  return nrels;
900 }

References PendingRelDelete::atCommit, PendingRelDelete::backend, GetCurrentTransactionNestLevel(), InvalidBackendId, PendingRelDelete::nestLevel, PendingRelDelete::next, palloc(), pendingDeletes, and PendingRelDelete::relnode.

Referenced by RecordTransactionAbort(), RecordTransactionCommit(), and StartPrepare().

Variable Documentation

◆ pendingDeletes

◆ pendingSyncHash

◆ wal_skip_threshold

int wal_skip_threshold = 2048

Definition at line 38 of file storage.c.

Referenced by smgrDoPendingSyncs().