PostgreSQL Source Code  git master
storage_xlog.h File Reference
#include "access/xlogreader.h"
#include "lib/stringinfo.h"
#include "storage/block.h"
#include "storage/relfilenode.h"
Include dependency graph for storage_xlog.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  xl_smgr_create
 
struct  xl_smgr_truncate
 

Macros

#define XLOG_SMGR_CREATE   0x10
 
#define XLOG_SMGR_TRUNCATE   0x20
 
#define SMGR_TRUNCATE_HEAP   0x0001
 
#define SMGR_TRUNCATE_VM   0x0002
 
#define SMGR_TRUNCATE_FSM   0x0004
 
#define SMGR_TRUNCATE_ALL   (SMGR_TRUNCATE_HEAP|SMGR_TRUNCATE_VM|SMGR_TRUNCATE_FSM)
 

Typedefs

typedef struct xl_smgr_create xl_smgr_create
 
typedef struct xl_smgr_truncate xl_smgr_truncate
 

Functions

void log_smgrcreate (const RelFileNode *rnode, ForkNumber forkNum)
 
void smgr_redo (XLogReaderState *record)
 
void smgr_desc (StringInfo buf, XLogReaderState *record)
 
const char * smgr_identify (uint8 info)
 

Macro Definition Documentation

◆ SMGR_TRUNCATE_ALL

#define SMGR_TRUNCATE_ALL   (SMGR_TRUNCATE_HEAP|SMGR_TRUNCATE_VM|SMGR_TRUNCATE_FSM)

Definition at line 43 of file storage_xlog.h.

Referenced by RelationTruncate().

◆ SMGR_TRUNCATE_FSM

#define SMGR_TRUNCATE_FSM   0x0004

Definition at line 42 of file storage_xlog.h.

Referenced by smgr_redo().

◆ SMGR_TRUNCATE_HEAP

#define SMGR_TRUNCATE_HEAP   0x0001

Definition at line 40 of file storage_xlog.h.

Referenced by smgr_redo().

◆ SMGR_TRUNCATE_VM

#define SMGR_TRUNCATE_VM   0x0002

Definition at line 41 of file storage_xlog.h.

Referenced by pg_truncate_visibility_map(), and smgr_redo().

◆ XLOG_SMGR_CREATE

#define XLOG_SMGR_CREATE   0x10

Definition at line 30 of file storage_xlog.h.

Referenced by extractPageInfo(), log_smgrcreate(), smgr_desc(), smgr_identify(), and smgr_redo().

◆ XLOG_SMGR_TRUNCATE

#define XLOG_SMGR_TRUNCATE   0x20

Typedef Documentation

◆ xl_smgr_create

◆ xl_smgr_truncate

Function Documentation

◆ log_smgrcreate()

void log_smgrcreate ( const RelFileNode rnode,
ForkNumber  forkNum 
)

Definition at line 175 of file storage.c.

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

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

176 {
177  xl_smgr_create xlrec;
178 
179  /*
180  * Make an XLOG entry reporting the file creation.
181  */
182  xlrec.rnode = *rnode;
183  xlrec.forkNum = forkNum;
184 
185  XLogBeginInsert();
186  XLogRegisterData((char *) &xlrec, sizeof(xlrec));
188 }
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:330
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:422
ForkNumber forkNum
Definition: storage_xlog.h:36
void XLogBeginInsert(void)
Definition: xloginsert.c:123
RelFileNode rnode
Definition: storage_xlog.h:35

◆ smgr_desc()

void smgr_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 21 of file smgrdesc.c.

References appendStringInfo(), appendStringInfoString(), xl_smgr_truncate::blkno, xl_smgr_truncate::flags, xl_smgr_create::forkNum, MAIN_FORKNUM, pfree(), relpathperm, xl_smgr_create::rnode, xl_smgr_truncate::rnode, XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

22 {
23  char *rec = XLogRecGetData(record);
24  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
25 
26  if (info == XLOG_SMGR_CREATE)
27  {
28  xl_smgr_create *xlrec = (xl_smgr_create *) rec;
29  char *path = relpathperm(xlrec->rnode, xlrec->forkNum);
30 
31  appendStringInfoString(buf, path);
32  pfree(path);
33  }
34  else if (info == XLOG_SMGR_TRUNCATE)
35  {
36  xl_smgr_truncate *xlrec = (xl_smgr_truncate *) rec;
37  char *path = relpathperm(xlrec->rnode, MAIN_FORKNUM);
38 
39  appendStringInfo(buf, "%s to %u blocks flags %d", path,
40  xlrec->blkno, xlrec->flags);
41  pfree(path);
42  }
43 }
#define relpathperm(rnode, forknum)
Definition: relpath.h:83
unsigned char uint8
Definition: c.h:372
RelFileNode rnode
Definition: storage_xlog.h:49
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
void pfree(void *pointer)
Definition: mcxt.c:1057
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:176
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
ForkNumber forkNum
Definition: storage_xlog.h:36
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
BlockNumber blkno
Definition: storage_xlog.h:48
RelFileNode rnode
Definition: storage_xlog.h:35

◆ smgr_identify()

const char* smgr_identify ( uint8  info)

Definition at line 46 of file smgrdesc.c.

References XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, and XLR_INFO_MASK.

47 {
48  const char *id = NULL;
49 
50  switch (info & ~XLR_INFO_MASK)
51  {
52  case XLOG_SMGR_CREATE:
53  id = "CREATE";
54  break;
55  case XLOG_SMGR_TRUNCATE:
56  id = "TRUNCATE";
57  break;
58  }
59 
60  return id;
61 }
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

◆ smgr_redo()

void smgr_redo ( XLogReaderState record)

Definition at line 903 of file storage.c.

References Assert, xl_smgr_truncate::blkno, BlockNumberIsValid, blocks, 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.

904 {
905  XLogRecPtr lsn = record->EndRecPtr;
906  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
907 
908  /* Backup blocks are not used in smgr records */
909  Assert(!XLogRecHasAnyBlockRefs(record));
910 
911  if (info == XLOG_SMGR_CREATE)
912  {
913  xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
914  SMgrRelation reln;
915 
916  reln = smgropen(xlrec->rnode, InvalidBackendId);
917  smgrcreate(reln, xlrec->forkNum, true);
918  }
919  else if (info == XLOG_SMGR_TRUNCATE)
920  {
921  xl_smgr_truncate *xlrec = (xl_smgr_truncate *) XLogRecGetData(record);
922  SMgrRelation reln;
923  Relation rel;
924  ForkNumber forks[MAX_FORKNUM];
926  int nforks = 0;
927  bool need_fsm_vacuum = false;
928 
929  reln = smgropen(xlrec->rnode, InvalidBackendId);
930 
931  /*
932  * Forcibly create relation if it doesn't exist (which suggests that
933  * it was dropped somewhere later in the WAL sequence). As in
934  * XLogReadBufferForRedo, we prefer to recreate the rel and replay the
935  * log as best we can until the drop is seen.
936  */
937  smgrcreate(reln, MAIN_FORKNUM, true);
938 
939  /*
940  * Before we perform the truncation, update minimum recovery point to
941  * cover this WAL record. Once the relation is truncated, there's no
942  * going back. The buffer manager enforces the WAL-first rule for
943  * normal updates to relation files, so that the minimum recovery
944  * point is always updated before the corresponding change in the data
945  * file is flushed to disk. We have to do the same manually here.
946  *
947  * Doing this before the truncation means that if the truncation fails
948  * for some reason, you cannot start up the system even after restart,
949  * until you fix the underlying situation so that the truncation will
950  * succeed. Alternatively, we could update the minimum recovery point
951  * after truncation, but that would leave a small window where the
952  * WAL-first rule could be violated.
953  */
954  XLogFlush(lsn);
955 
956  /* Prepare for truncation of MAIN fork */
957  if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
958  {
959  forks[nforks] = MAIN_FORKNUM;
960  blocks[nforks] = xlrec->blkno;
961  nforks++;
962 
963  /* Also tell xlogutils.c about it */
964  XLogTruncateRelation(xlrec->rnode, MAIN_FORKNUM, xlrec->blkno);
965  }
966 
967  /* Prepare for truncation of FSM and VM too */
968  rel = CreateFakeRelcacheEntry(xlrec->rnode);
969 
970  if ((xlrec->flags & SMGR_TRUNCATE_FSM) != 0 &&
971  smgrexists(reln, FSM_FORKNUM))
972  {
973  blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, xlrec->blkno);
974  if (BlockNumberIsValid(blocks[nforks]))
975  {
976  forks[nforks] = FSM_FORKNUM;
977  nforks++;
978  need_fsm_vacuum = true;
979  }
980  }
981  if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0 &&
983  {
984  blocks[nforks] = visibilitymap_prepare_truncate(rel, xlrec->blkno);
985  if (BlockNumberIsValid(blocks[nforks]))
986  {
987  forks[nforks] = VISIBILITYMAP_FORKNUM;
988  nforks++;
989  }
990  }
991 
992  /* Do the real work to truncate relation forks */
993  if (nforks > 0)
994  smgrtruncate(reln, forks, nforks, blocks);
995 
996  /*
997  * Update upper-level FSM pages to account for the truncation. This is
998  * important because the just-truncated pages were likely marked as
999  * all-free, and would be preferentially selected.
1000  */
1001  if (need_fsm_vacuum)
1002  FreeSpaceMapVacuumRange(rel, xlrec->blkno,
1004 
1005  FreeFakeRelcacheEntry(rel);
1006  }
1007  else
1008  elog(PANIC, "smgr_redo: unknown op code %u", info);
1009 }
void XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, BlockNumber nblocks)
Definition: xlogutils.c:644
#define SMGR_TRUNCATE_HEAP
Definition: storage_xlog.h:40
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:334
unsigned char uint8
Definition: c.h:372
#define SMGR_TRUNCATE_FSM
Definition: storage_xlog.h:42
uint32 BlockNumber
Definition: block.h:31
RelFileNode rnode
Definition: storage_xlog.h:49
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:248
#define PANIC
Definition: elog.h:53
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2845
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
XLogRecPtr EndRecPtr
Definition: xlogreader.h:176
#define XLogRecGetData(decoder)
Definition: xlogreader.h:310
Relation CreateFakeRelcacheEntry(RelFileNode rnode)
Definition: xlogutils.c:556
BlockNumber FreeSpaceMapPrepareTruncateRel(Relation rel, BlockNumber nblocks)
Definition: freespace.c:261
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:305
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:146
ForkNumber
Definition: relpath.h:40
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
void FreeFakeRelcacheEntry(Relation fakerel)
Definition: xlogutils.c:599
#define InvalidBackendId
Definition: backendid.h:23
void smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *nblocks)
Definition: smgr.c:578
#define BlockNumberIsValid(blockNumber)
Definition: block.h:70
ForkNumber forkNum
Definition: storage_xlog.h:36
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define InvalidBlockNumber
Definition: block.h:33
#define SMGR_TRUNCATE_VM
Definition: storage_xlog.h:41
#define MAX_FORKNUM
Definition: relpath.h:55
static int64 blocks
Definition: pg_checksums.c:35
#define elog(elevel,...)
Definition: elog.h:214
BlockNumber visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:312
BlockNumber blkno
Definition: storage_xlog.h:48
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:354
RelFileNode rnode
Definition: storage_xlog.h:35