BootStrapXLOG
void BootStrapXLOG(void) {
CheckPoint checkPoint;
XLogLongPageHeader longpage;
XLogRecord *record;
char *recptr;
bool use_existent;
pg_crc32c crc;
为安装选择唯一的系统标识符。
struct timeval tv;
uint64 sysidentifier;
gettimeofday(&tv, NULL);
sysidentifier = ((uint64) tv.tv_sec) << 32;
sysidentifier |= ((uint64) tv.tv_usec) << 12;
sysidentifier |= getpid() & 0xFFF;
产生随机的nonce,主要用于用户不存在时认证失败时的认证请求。nonce用于为不存在的用户创建genuine-looking密码challenge,用于代替真实存储的密码。
char mock_auth_nonce[MOCK_AUTH_NONCE_LEN];
if (!pg_strong_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
ereport(PANIC,(errcode(ERRCODE_INTERNAL_ERROR),errmsg("could not generate secret authorization token")));
申请两个XLOG_BLCKSZ的内存,并将其强制为XLogPageHeader的指针。
char *buffer;
XLogPageHeader page;
/* page buffer must be aligned suitably for O_DIRECT */
buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
memset(page, 0, XLOG_BLCKSZ);
为初始的checkpoint record建立信息
/* First timeline ID is always 1 */
ThisTimeLineID = 1;
checkPoint.redo = wal_segment_size + SizeOfXLogLongPHD;
checkPoint.ThisTimeLineID = ThisTimeLineID;
checkPoint.PrevTimeLineID = ThisTimeLineID;
checkPoint.fullPageWrites = fullPageWrites;
checkPoint.nextFullXid = FullTransactionIdFromEpochAndXid(0, FirstNormalTransactionId);
checkPoint.nextOid = FirstBootstrapObjectId;
checkPoint.nextMulti = FirstMultiXactId;
checkPoint.nextMultiOffset = 0;
checkPoint.oldestXid = FirstNormalTransactionId;
checkPoint.oldestXidDB = TemplateDbOid;
checkPoint.oldestMulti = FirstMultiXactId;
checkPoint.oldestMultiDB = TemplateDbOid;
checkPoint.oldestCommitTsXid = InvalidTransactionId;
checkPoint.newestCommitTsXid = InvalidTransactionId;
checkPoint.time = (pg_time_t) time(NULL);
checkPoint.oldestActiveXid = InvalidTransactionId;
BootStrapCLOG
创建初始CLOG段文件
/* This func must be called ONCE on system install. It creates
* the initial CLOG segment. (The CLOG directory is assumed to
* have been created by initdb, and CLOGShmemInit must have been
* called already.) */
void BootStrapCLOG(void) {
int slotno;
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
/* Create and zero the first page of the commit log */
slotno = ZeroCLOGPage(0, false);
/* Make sure it's written out */
SimpleLruWritePage(ClogCtl, slotno);
Assert(!ClogCtl->shared->page_dirty[slotno]);
LWLockRelease(CLogControlLock);
}
BootStrapCommitTs
/* This function must be called ONCE on system install.
* (The CommitTs directory is assumed to have been created by initdb, and
* CommitTsShmemInit must have been called already.) */
void BootStrapCommitTs(void) {
/* Nothing to do here at present, unlike most other SLRU modules; segments
* are created when the server is started with this module enabled. See
* ActivateCommitTs. */
}
BootStrapSUBTRANS
/* This func must be called ONCE on system install. It creates
* the initial SUBTRANS segment. (The SUBTRANS directory is assumed to
* have been created by the initdb shell script, and SUBTRANSShmemInit
* must have been called already.)
* Note: it's not really necessary to create the initial segment now,
* since slru.c would create it on first write anyway. But we may as well
* do it to be sure the directory is set up correctly. */
void BootStrapSUBTRANS(void) {
int slotno;
LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
/* Create and zero the first page of the subtrans log */
slotno = ZeroSUBTRANSPage(0);
/* Make sure it's written out */
SimpleLruWritePage(SubTransCtl, slotno);
Assert(!SubTransCtl->shared->page_dirty[slotno]);
LWLockRelease(SubtransControlLock);
}
BootStrapMultiXact
/* This func must be called ONCE on system install. It creates the initial
* MultiXact segments. (The MultiXacts directories are assumed to have been
* created by initdb, and MultiXactShmemInit must have been called already.) */
void BootStrapMultiXact(void) {
int slotno;
LWLockAcquire(MultiXactOffsetControlLock, LW_EXCLUSIVE);
/* Create and zero the first page of the offsets log */
slotno = ZeroMultiXactOffsetPage(0, false);
/* Make sure it's written out */
SimpleLruWritePage(MultiXactOffsetCtl, slotno);
Assert(!MultiXactOffsetCtl->shared->page_dirty[slotno]);
LWLockRelease(MultiXactOffsetControlLock);
LWLockAcquire(MultiXactMemberControlLock, LW_EXCLUSIVE);
/* Create and zero the first page of the members log */
slotno = ZeroMultiXactMemberPage(0, false);
/* Make sure it's written out */
SimpleLruWritePage(MultiXactMemberCtl, slotno);
Assert(!MultiXactMemberCtl->shared->page_dirty[slotno]);
LWLockRelease(MultiXactMemberControlLock);
}