PostgreSQL Source Code git master
storage_xlog.h File Reference
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 RelFileLocator *rlocator, 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.

◆ SMGR_TRUNCATE_FSM

#define SMGR_TRUNCATE_FSM   0x0004

Definition at line 42 of file storage_xlog.h.

◆ SMGR_TRUNCATE_HEAP

#define SMGR_TRUNCATE_HEAP   0x0001

Definition at line 40 of file storage_xlog.h.

◆ SMGR_TRUNCATE_VM

#define SMGR_TRUNCATE_VM   0x0002

Definition at line 41 of file storage_xlog.h.

◆ XLOG_SMGR_CREATE

#define XLOG_SMGR_CREATE   0x10

Definition at line 30 of file storage_xlog.h.

◆ XLOG_SMGR_TRUNCATE

#define XLOG_SMGR_TRUNCATE   0x20

Definition at line 31 of file storage_xlog.h.

Typedef Documentation

◆ xl_smgr_create

◆ xl_smgr_truncate

Function Documentation

◆ log_smgrcreate()

void log_smgrcreate ( const RelFileLocator rlocator,
ForkNumber  forkNum 
)

Definition at line 186 of file storage.c.

187{
188 xl_smgr_create xlrec;
189
190 /*
191 * Make an XLOG entry reporting the file creation.
192 */
193 xlrec.rlocator = *rlocator;
194 xlrec.forkNum = forkNum;
195
197 XLogRegisterData((char *) &xlrec, sizeof(xlrec));
199}
#define XLOG_SMGR_CREATE
Definition: storage_xlog.h:30
ForkNumber forkNum
Definition: storage_xlog.h:36
RelFileLocator rlocator
Definition: storage_xlog.h:35
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:474
void XLogRegisterData(const char *data, uint32 len)
Definition: xloginsert.c:364
void XLogBeginInsert(void)
Definition: xloginsert.c:149
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:82

References xl_smgr_create::forkNum, xl_smgr_create::rlocator, 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_filelocator(), index_build(), index_copy_data(), and RelationCreateStorage().

◆ smgr_desc()

void smgr_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 21 of file smgrdesc.c.

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->rlocator, xlrec->forkNum);
30
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->rlocator, MAIN_FORKNUM);
38
39 appendStringInfo(buf, "%s to %u blocks flags %d", path,
40 xlrec->blkno, xlrec->flags);
41 pfree(path);
42 }
43}
uint8_t uint8
Definition: c.h:483
void pfree(void *pointer)
Definition: mcxt.c:1521
static char * buf
Definition: pg_test_fsync.c:72
@ MAIN_FORKNUM
Definition: relpath.h:58
#define relpathperm(rlocator, forknum)
Definition: relpath.h:98
#define XLOG_SMGR_TRUNCATE
Definition: storage_xlog.h:31
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
RelFileLocator rlocator
Definition: storage_xlog.h:49
BlockNumber blkno
Definition: storage_xlog.h:48
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415

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

◆ smgr_identify()

const char * smgr_identify ( uint8  info)

Definition at line 46 of file smgrdesc.c.

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

References XLOG_SMGR_CREATE, XLOG_SMGR_TRUNCATE, and XLR_INFO_MASK.

◆ smgr_redo()

void smgr_redo ( XLogReaderState record)

Definition at line 965 of file storage.c.

966{
967 XLogRecPtr lsn = record->EndRecPtr;
968 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
969
970 /* Backup blocks are not used in smgr records */
972
973 if (info == XLOG_SMGR_CREATE)
974 {
975 xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
976 SMgrRelation reln;
977
978 reln = smgropen(xlrec->rlocator, INVALID_PROC_NUMBER);
979 smgrcreate(reln, xlrec->forkNum, true);
980 }
981 else if (info == XLOG_SMGR_TRUNCATE)
982 {
984 SMgrRelation reln;
985 Relation rel;
986 ForkNumber forks[MAX_FORKNUM];
987 BlockNumber blocks[MAX_FORKNUM];
988 BlockNumber old_blocks[MAX_FORKNUM];
989 int nforks = 0;
990 bool need_fsm_vacuum = false;
991
992 reln = smgropen(xlrec->rlocator, INVALID_PROC_NUMBER);
993
994 /*
995 * Forcibly create relation if it doesn't exist (which suggests that
996 * it was dropped somewhere later in the WAL sequence). As in
997 * XLogReadBufferForRedo, we prefer to recreate the rel and replay the
998 * log as best we can until the drop is seen.
999 */
1000 smgrcreate(reln, MAIN_FORKNUM, true);
1001
1002 /*
1003 * Before we perform the truncation, update minimum recovery point to
1004 * cover this WAL record. Once the relation is truncated, there's no
1005 * going back. The buffer manager enforces the WAL-first rule for
1006 * normal updates to relation files, so that the minimum recovery
1007 * point is always updated before the corresponding change in the data
1008 * file is flushed to disk. We have to do the same manually here.
1009 *
1010 * Doing this before the truncation means that if the truncation fails
1011 * for some reason, you cannot start up the system even after restart,
1012 * until you fix the underlying situation so that the truncation will
1013 * succeed. Alternatively, we could update the minimum recovery point
1014 * after truncation, but that would leave a small window where the
1015 * WAL-first rule could be violated.
1016 */
1017 XLogFlush(lsn);
1018
1019 /* Prepare for truncation of MAIN fork */
1020 if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
1021 {
1022 forks[nforks] = MAIN_FORKNUM;
1023 old_blocks[nforks] = smgrnblocks(reln, MAIN_FORKNUM);
1024 blocks[nforks] = xlrec->blkno;
1025 nforks++;
1026
1027 /* Also tell xlogutils.c about it */
1029 }
1030
1031 /* Prepare for truncation of FSM and VM too */
1032 rel = CreateFakeRelcacheEntry(xlrec->rlocator);
1033
1034 if ((xlrec->flags & SMGR_TRUNCATE_FSM) != 0 &&
1035 smgrexists(reln, FSM_FORKNUM))
1036 {
1037 blocks[nforks] = FreeSpaceMapPrepareTruncateRel(rel, xlrec->blkno);
1038 if (BlockNumberIsValid(blocks[nforks]))
1039 {
1040 forks[nforks] = FSM_FORKNUM;
1041 old_blocks[nforks] = smgrnblocks(reln, FSM_FORKNUM);
1042 nforks++;
1043 need_fsm_vacuum = true;
1044 }
1045 }
1046 if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0 &&
1048 {
1049 blocks[nforks] = visibilitymap_prepare_truncate(rel, xlrec->blkno);
1050 if (BlockNumberIsValid(blocks[nforks]))
1051 {
1052 forks[nforks] = VISIBILITYMAP_FORKNUM;
1053 old_blocks[nforks] = smgrnblocks(reln, VISIBILITYMAP_FORKNUM);
1054 nforks++;
1055 }
1056 }
1057
1058 /* Do the real work to truncate relation forks */
1059 if (nforks > 0)
1060 {
1062 smgrtruncate(reln, forks, nforks, old_blocks, blocks);
1064 }
1065
1066 /*
1067 * Update upper-level FSM pages to account for the truncation. This is
1068 * important because the just-truncated pages were likely marked as
1069 * all-free, and would be preferentially selected.
1070 */
1071 if (need_fsm_vacuum)
1072 FreeSpaceMapVacuumRange(rel, xlrec->blkno,
1074
1076 }
1077 else
1078 elog(PANIC, "smgr_redo: unknown op code %u", info);
1079}
uint32 BlockNumber
Definition: block.h:31
#define InvalidBlockNumber
Definition: block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define Assert(condition)
Definition: c.h:812
#define PANIC
Definition: elog.h:42
#define elog(elevel,...)
Definition: elog.h:225
void FreeSpaceMapVacuumRange(Relation rel, BlockNumber start, BlockNumber end)
Definition: freespace.c:377
BlockNumber FreeSpaceMapPrepareTruncateRel(Relation rel, BlockNumber nblocks)
Definition: freespace.c:275
#define START_CRIT_SECTION()
Definition: miscadmin.h:149
#define END_CRIT_SECTION()
Definition: miscadmin.h:151
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
ForkNumber
Definition: relpath.h:56
@ FSM_FORKNUM
Definition: relpath.h:59
@ VISIBILITYMAP_FORKNUM
Definition: relpath.h:60
#define MAX_FORKNUM
Definition: relpath.h:70
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:677
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition: smgr.c:201
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:414
void smgrtruncate(SMgrRelation reln, ForkNumber *forknum, int nforks, BlockNumber *old_nblocks, BlockNumber *nblocks)
Definition: smgr.c:729
bool smgrexists(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:401
#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
BlockNumber visibilitymap_prepare_truncate(Relation rel, BlockNumber nheapblocks)
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2802
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417
void FreeFakeRelcacheEntry(Relation fakerel)
Definition: xlogutils.c:629
void XLogTruncateRelation(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber nblocks)
Definition: xlogutils.c:671
Relation CreateFakeRelcacheEntry(RelFileLocator rlocator)
Definition: xlogutils.c:582

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