PostgreSQL数据库WAL——BootStrapXLOG

本文详细介绍了数据库系统安装时的关键步骤,包括BootStrapXLOG、BootStrapCLOG、BootStrapCommitTs和BootStrapSUBTRANS等。这些步骤涉及设置系统标识符、创建初始日志和段文件,以及初始化事务和多事务管理。通过对时间戳、随机nonce的生成,确保了系统的唯一性和安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值