Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

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

Go to the source code of this file.

Data Structures

struct  xl_clog_truncate
 

Macros

#define TRANSACTION_STATUS_IN_PROGRESS   0x00
 
#define TRANSACTION_STATUS_COMMITTED   0x01
 
#define TRANSACTION_STATUS_ABORTED   0x02
 
#define TRANSACTION_STATUS_SUB_COMMITTED   0x03
 
#define CLOG_ZEROPAGE   0x00
 
#define CLOG_TRUNCATE   0x10
 

Typedefs

typedef int XidStatus
 
typedef struct xl_clog_truncate xl_clog_truncate
 

Functions

void TransactionIdSetTreeStatus (TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
 
XidStatus TransactionIdGetStatus (TransactionId xid, XLogRecPtr *lsn)
 
Size CLOGShmemSize (void)
 
void CLOGShmemInit (void)
 
void BootStrapCLOG (void)
 
void StartupCLOG (void)
 
void TrimCLOG (void)
 
void CheckPointCLOG (void)
 
void ExtendCLOG (TransactionId newestXact)
 
void TruncateCLOG (TransactionId oldestXact, Oid oldestxid_datoid)
 
int clogsyncfiletag (const FileTag *ftag, char *path)
 
void clog_redo (XLogReaderState *record)
 
void clog_desc (StringInfo buf, XLogReaderState *record)
 
const char * clog_identify (uint8 info)
 

Macro Definition Documentation

◆ CLOG_TRUNCATE

#define CLOG_TRUNCATE   0x10

Definition at line 56 of file clog.h.

◆ CLOG_ZEROPAGE

#define CLOG_ZEROPAGE   0x00

Definition at line 55 of file clog.h.

◆ TRANSACTION_STATUS_ABORTED

#define TRANSACTION_STATUS_ABORTED   0x02

Definition at line 29 of file clog.h.

◆ TRANSACTION_STATUS_COMMITTED

#define TRANSACTION_STATUS_COMMITTED   0x01

Definition at line 28 of file clog.h.

◆ TRANSACTION_STATUS_IN_PROGRESS

#define TRANSACTION_STATUS_IN_PROGRESS   0x00

Definition at line 27 of file clog.h.

◆ TRANSACTION_STATUS_SUB_COMMITTED

#define TRANSACTION_STATUS_SUB_COMMITTED   0x03

Definition at line 30 of file clog.h.

Typedef Documentation

◆ XidStatus

typedef int XidStatus

Definition at line 25 of file clog.h.

◆ xl_clog_truncate

Function Documentation

◆ BootStrapCLOG()

void BootStrapCLOG ( void  )

Definition at line 831 of file clog.c.

832{
833 /* Zero the initial page and flush it to disk */
835}
#define XactCtl
Definition: clog.c:110
void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
Definition: slru.c:444

References SimpleLruZeroAndWritePage(), and XactCtl.

Referenced by BootStrapXLOG().

◆ CheckPointCLOG()

void CheckPointCLOG ( void  )

Definition at line 902 of file clog.c.

903{
904 /*
905 * Write dirty CLOG pages to disk. This may result in sync requests
906 * queued for later handling by ProcessSyncRequests(), as part of the
907 * checkpoint.
908 */
909 TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(true);
911 TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(true);
912}
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1347

References SimpleLruWriteAll(), and XactCtl.

Referenced by CheckPointGuts().

◆ clog_desc()

void clog_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 21 of file clogdesc.c.

22{
23 char *rec = XLogRecGetData(record);
24 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
25
26 if (info == CLOG_ZEROPAGE)
27 {
28 int64 pageno;
29
30 memcpy(&pageno, rec, sizeof(pageno));
31 appendStringInfo(buf, "page %" PRId64, pageno);
32 }
33 else if (info == CLOG_TRUNCATE)
34 {
35 xl_clog_truncate xlrec;
36
37 memcpy(&xlrec, rec, sizeof(xl_clog_truncate));
38 appendStringInfo(buf, "page %" PRId64 "; oldestXact %u",
39 xlrec.pageno, xlrec.oldestXact);
40 }
41}
uint8_t uint8
Definition: c.h:537
int64_t int64
Definition: c.h:536
#define CLOG_ZEROPAGE
Definition: clog.h:55
#define CLOG_TRUNCATE
Definition: clog.h:56
static char * buf
Definition: pg_test_fsync.c:72
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
int64 pageno
Definition: clog.h:34
TransactionId oldestXact
Definition: clog.h:35
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:410
#define XLogRecGetData(decoder)
Definition: xlogreader.h:415

References appendStringInfo(), buf, CLOG_TRUNCATE, CLOG_ZEROPAGE, xl_clog_truncate::oldestXact, xl_clog_truncate::pageno, XLogRecGetData, and XLogRecGetInfo.

◆ clog_identify()

const char * clog_identify ( uint8  info)

Definition at line 44 of file clogdesc.c.

45{
46 const char *id = NULL;
47
48 switch (info & ~XLR_INFO_MASK)
49 {
50 case CLOG_ZEROPAGE:
51 id = "ZEROPAGE";
52 break;
53 case CLOG_TRUNCATE:
54 id = "TRUNCATE";
55 break;
56 }
57
58 return id;
59}
#define XLR_INFO_MASK
Definition: xlogrecord.h:62

References CLOG_TRUNCATE, CLOG_ZEROPAGE, and XLR_INFO_MASK.

◆ clog_redo()

void clog_redo ( XLogReaderState record)

Definition at line 1062 of file clog.c.

1063{
1064 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1065
1066 /* Backup blocks are not used in clog records */
1068
1069 if (info == CLOG_ZEROPAGE)
1070 {
1071 int64 pageno;
1072
1073 memcpy(&pageno, XLogRecGetData(record), sizeof(pageno));
1075 }
1076 else if (info == CLOG_TRUNCATE)
1077 {
1078 xl_clog_truncate xlrec;
1079
1080 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
1081
1083
1085 }
1086 else
1087 elog(PANIC, "clog_redo: unknown op code %u", info);
1088}
#define PANIC
Definition: elog.h:42
#define elog(elevel,...)
Definition: elog.h:226
Assert(PointerIsAligned(start, uint64))
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition: slru.c:1433
void AdvanceOldestClogXid(TransactionId oldest_datfrozenxid)
Definition: varsup.c:355
#define XLogRecHasAnyBlockRefs(decoder)
Definition: xlogreader.h:417

References AdvanceOldestClogXid(), Assert(), CLOG_TRUNCATE, CLOG_ZEROPAGE, elog, xl_clog_truncate::oldestXact, xl_clog_truncate::pageno, PANIC, SimpleLruTruncate(), SimpleLruZeroAndWritePage(), XactCtl, XLogRecGetData, XLogRecGetInfo, and XLogRecHasAnyBlockRefs.

◆ CLOGShmemInit()

void CLOGShmemInit ( void  )

Definition at line 785 of file clog.c.

786{
787 /* If auto-tuning is requested, now is the time to do it */
788 if (transaction_buffers == 0)
789 {
790 char buf[32];
791
792 snprintf(buf, sizeof(buf), "%d", CLOGShmemBuffers());
793 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
795
796 /*
797 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
798 * However, if the DBA explicitly set transaction_buffers = 0 in the
799 * config file, then PGC_S_DYNAMIC_DEFAULT will fail to override that
800 * and we must force the matter with PGC_S_OVERRIDE.
801 */
802 if (transaction_buffers == 0) /* failed to apply it? */
803 SetConfigOption("transaction_buffers", buf, PGC_POSTMASTER,
805 }
807
808 XactCtl->PagePrecedes = CLOGPagePrecedes;
810 "pg_xact", LWTRANCHE_XACT_BUFFER,
811 LWTRANCHE_XACT_SLRU, SYNC_HANDLER_CLOG, false);
813}
#define CLOG_XACTS_PER_PAGE
Definition: clog.c:64
static int CLOGShmemBuffers(void)
Definition: clog.c:766
static bool CLOGPagePrecedes(int64 page1, int64 page2)
Definition: clog.c:1021
#define CLOG_LSNS_PER_PAGE
Definition: clog.c:93
int transaction_buffers
Definition: globals.c:167
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4337
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:114
@ PGC_S_OVERRIDE
Definition: guc.h:123
@ PGC_POSTMASTER
Definition: guc.h:74
#define snprintf
Definition: port.h:239
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition: slru.c:252
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition: slru.h:200
@ SYNC_HANDLER_CLOG
Definition: sync.h:38

References Assert(), buf, CLOG_LSNS_PER_PAGE, CLOG_XACTS_PER_PAGE, CLOGPagePrecedes(), CLOGShmemBuffers(), PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, SetConfigOption(), SimpleLruInit(), SlruPagePrecedesUnitTests, snprintf, SYNC_HANDLER_CLOG, transaction_buffers, and XactCtl.

Referenced by CreateOrAttachShmemStructs().

◆ CLOGShmemSize()

Size CLOGShmemSize ( void  )

Definition at line 779 of file clog.c.

780{
782}
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:198

References CLOG_LSNS_PER_PAGE, CLOGShmemBuffers(), and SimpleLruShmemSize().

Referenced by CalculateShmemSize().

◆ clogsyncfiletag()

int clogsyncfiletag ( const FileTag ftag,
char *  path 
)

Definition at line 1094 of file clog.c.

1095{
1096 return SlruSyncFileTag(XactCtl, ftag, path);
1097}
int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path)
Definition: slru.c:1856

References SlruSyncFileTag(), and XactCtl.

◆ ExtendCLOG()

void ExtendCLOG ( TransactionId  newestXact)

Definition at line 924 of file clog.c.

925{
926 int64 pageno;
927 LWLock *lock;
928
929 /*
930 * No work except at first XID of a page. But beware: just after
931 * wraparound, the first XID of page zero is FirstNormalTransactionId.
932 */
933 if (TransactionIdToPgIndex(newestXact) != 0 &&
935 return;
936
937 pageno = TransactionIdToPage(newestXact);
938 lock = SimpleLruGetBankLock(XactCtl, pageno);
939
941
942 /* Zero the page and make a WAL entry about it */
943 SimpleLruZeroPage(XactCtl, pageno);
944 XLogSimpleInsertInt64(RM_CLOG_ID, CLOG_ZEROPAGE, pageno);
945
946 LWLockRelease(lock);
947}
static int64 TransactionIdToPage(TransactionId xid)
Definition: clog.c:82
#define TransactionIdToPgIndex(xid)
Definition: clog.c:87
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_EXCLUSIVE
Definition: lwlock.h:112
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition: slru.c:375
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition: slru.h:175
Definition: lwlock.h:42
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define FirstNormalTransactionId
Definition: transam.h:34
XLogRecPtr XLogSimpleInsertInt64(RmgrId rmid, uint8 info, int64 value)
Definition: xloginsert.c:537

References CLOG_ZEROPAGE, FirstNormalTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SimpleLruGetBankLock(), SimpleLruZeroPage(), TransactionIdEquals, TransactionIdToPage(), TransactionIdToPgIndex, XactCtl, and XLogSimpleInsertInt64().

Referenced by GetNewTransactionId().

◆ StartupCLOG()

void StartupCLOG ( void  )

Definition at line 842 of file clog.c.

843{
845 int64 pageno = TransactionIdToPage(xid);
846
847 /*
848 * Initialize our idea of the latest page number.
849 */
850 pg_atomic_write_u64(&XactCtl->shared->latest_page_number, pageno);
851}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:483
uint32 TransactionId
Definition: c.h:658
FullTransactionId nextXid
Definition: transam.h:220
#define XidFromFullTransactionId(x)
Definition: transam.h:48
TransamVariablesData * TransamVariables
Definition: varsup.c:34

References TransamVariablesData::nextXid, pg_atomic_write_u64(), TransactionIdToPage(), TransamVariables, XactCtl, and XidFromFullTransactionId.

Referenced by StartupXLOG().

◆ TransactionIdGetStatus()

XidStatus TransactionIdGetStatus ( TransactionId  xid,
XLogRecPtr lsn 
)

Definition at line 733 of file clog.c.

734{
735 int64 pageno = TransactionIdToPage(xid);
736 int byteno = TransactionIdToByte(xid);
737 int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
738 int slotno;
739 int lsnindex;
740 char *byteptr;
741 XidStatus status;
742
743 /* lock is acquired by SimpleLruReadPage_ReadOnly */
744
745 slotno = SimpleLruReadPage_ReadOnly(XactCtl, pageno, xid);
746 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
747
748 status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
749
750 lsnindex = GetLSNIndex(slotno, xid);
751 *lsn = XactCtl->shared->group_lsn[lsnindex];
752
754
755 return status;
756}
#define CLOG_XACT_BITMASK
Definition: clog.c:65
#define TransactionIdToBIndex(xid)
Definition: clog.c:89
#define CLOG_BITS_PER_XACT
Definition: clog.c:62
#define TransactionIdToByte(xid)
Definition: clog.c:88
#define GetLSNIndex(slotno, xid)
Definition: clog.c:95
int XidStatus
Definition: clog.h:25
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition: slru.c:630

References CLOG_BITS_PER_XACT, CLOG_XACT_BITMASK, GetLSNIndex, LWLockRelease(), SimpleLruGetBankLock(), SimpleLruReadPage_ReadOnly(), TransactionIdToBIndex, TransactionIdToByte, TransactionIdToPage(), and XactCtl.

Referenced by TransactionIdGetCommitLSN(), and TransactionLogFetch().

◆ TransactionIdSetTreeStatus()

void TransactionIdSetTreeStatus ( TransactionId  xid,
int  nsubxids,
TransactionId subxids,
XidStatus  status,
XLogRecPtr  lsn 
)

Definition at line 181 of file clog.c.

183{
184 int64 pageno = TransactionIdToPage(xid); /* get page of parent */
185 int i;
186
189
190 /*
191 * See how many subxids, if any, are on the same page as the parent, if
192 * any.
193 */
194 for (i = 0; i < nsubxids; i++)
195 {
196 if (TransactionIdToPage(subxids[i]) != pageno)
197 break;
198 }
199
200 /*
201 * Do all items fit on a single page?
202 */
203 if (i == nsubxids)
204 {
205 /*
206 * Set the parent and all subtransactions in a single call
207 */
208 TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
209 pageno, true);
210 }
211 else
212 {
213 int nsubxids_on_first_page = i;
214
215 /*
216 * If this is a commit then we care about doing this correctly (i.e.
217 * using the subcommitted intermediate status). By here, we know
218 * we're updating more than one page of clog, so we must mark entries
219 * that are *not* on the first page so that they show as subcommitted
220 * before we then return to update the status to fully committed.
221 *
222 * To avoid touching the first page twice, skip marking subcommitted
223 * for the subxids on that first page.
224 */
225 if (status == TRANSACTION_STATUS_COMMITTED)
226 set_status_by_pages(nsubxids - nsubxids_on_first_page,
227 subxids + nsubxids_on_first_page,
229
230 /*
231 * Now set the parent and subtransactions on same page as the parent,
232 * if any
233 */
234 pageno = TransactionIdToPage(xid);
235 TransactionIdSetPageStatus(xid, nsubxids_on_first_page, subxids, status,
236 lsn, pageno, false);
237
238 /*
239 * Now work through the rest of the subxids one clog page at a time,
240 * starting from the second page onwards, like we did above.
241 */
242 set_status_by_pages(nsubxids - nsubxids_on_first_page,
243 subxids + nsubxids_on_first_page,
244 status, lsn);
245 }
246}
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn, int64 pageno, bool all_xact_same_page)
Definition: clog.c:291
static void set_status_by_pages(int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition: clog.c:255
#define TRANSACTION_STATUS_ABORTED
Definition: clog.h:29
#define TRANSACTION_STATUS_SUB_COMMITTED
Definition: clog.h:30
#define TRANSACTION_STATUS_COMMITTED
Definition: clog.h:28
int i
Definition: isn.c:77

References Assert(), i, set_status_by_pages(), TRANSACTION_STATUS_ABORTED, TRANSACTION_STATUS_COMMITTED, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdSetPageStatus(), and TransactionIdToPage().

Referenced by TransactionIdAbortTree(), TransactionIdAsyncCommitTree(), and TransactionIdCommitTree().

◆ TrimCLOG()

void TrimCLOG ( void  )

Definition at line 857 of file clog.c.

858{
860 int64 pageno = TransactionIdToPage(xid);
861 LWLock *lock = SimpleLruGetBankLock(XactCtl, pageno);
862
864
865 /*
866 * Zero out the remainder of the current clog page. Under normal
867 * circumstances it should be zeroes already, but it seems at least
868 * theoretically possible that XLOG replay will have settled on a nextXID
869 * value that is less than the last XID actually used and marked by the
870 * previous database lifecycle (since subtransaction commit writes clog
871 * but makes no WAL entry). Let's just be safe. (We need not worry about
872 * pages beyond the current one, since those will be zeroed when first
873 * used. For the same reason, there is no need to do anything when
874 * nextXid is exactly at a page boundary; and it's likely that the
875 * "current" page doesn't exist yet in that case.)
876 */
877 if (TransactionIdToPgIndex(xid) != 0)
878 {
879 int byteno = TransactionIdToByte(xid);
880 int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
881 int slotno;
882 char *byteptr;
883
884 slotno = SimpleLruReadPage(XactCtl, pageno, false, xid);
885 byteptr = XactCtl->shared->page_buffer[slotno] + byteno;
886
887 /* Zero so-far-unused positions in the current byte */
888 *byteptr &= (1 << bshift) - 1;
889 /* Zero the rest of the page */
890 MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);
891
892 XactCtl->shared->page_dirty[slotno] = true;
893 }
894
895 LWLockRelease(lock);
896}
#define MemSet(start, val, len)
Definition: c.h:1020
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:527

References CLOG_BITS_PER_XACT, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemSet, TransamVariablesData::nextXid, SimpleLruGetBankLock(), SimpleLruReadPage(), TransactionIdToBIndex, TransactionIdToByte, TransactionIdToPage(), TransactionIdToPgIndex, TransamVariables, XactCtl, and XidFromFullTransactionId.

Referenced by StartupXLOG().

◆ TruncateCLOG()

void TruncateCLOG ( TransactionId  oldestXact,
Oid  oldestxid_datoid 
)

Definition at line 966 of file clog.c.

967{
968 int64 cutoffPage;
969
970 /*
971 * The cutoff point is the start of the segment containing oldestXact. We
972 * pass the *page* containing oldestXact to SimpleLruTruncate.
973 */
974 cutoffPage = TransactionIdToPage(oldestXact);
975
976 /* Check to see if there's any files that could be removed */
978 return; /* nothing to remove */
979
980 /*
981 * Advance oldestClogXid before truncating clog, so concurrent xact status
982 * lookups can ensure they don't attempt to access truncated-away clog.
983 *
984 * It's only necessary to do this if we will actually truncate away clog
985 * pages.
986 */
987 AdvanceOldestClogXid(oldestXact);
988
989 /*
990 * Write XLOG record and flush XLOG to disk. We record the oldest xid
991 * we're keeping information about here so we can ensure that it's always
992 * ahead of clog truncation in case we crash, and so a standby finds out
993 * the new valid xid before the next checkpoint.
994 */
995 WriteTruncateXlogRec(cutoffPage, oldestXact, oldestxid_datoid);
996
997 /* Now we can remove the old CLOG segment(s) */
998 SimpleLruTruncate(XactCtl, cutoffPage);
999}
static void WriteTruncateXlogRec(int64 pageno, TransactionId oldestXact, Oid oldestXactDb)
Definition: clog.c:1043
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1816
bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: slru.c:1737

References AdvanceOldestClogXid(), SimpleLruTruncate(), SlruScanDirCbReportPresence(), SlruScanDirectory(), TransactionIdToPage(), WriteTruncateXlogRec(), and XactCtl.

Referenced by vac_truncate_clog().