PostgreSQL的clog属于日志还是数据,需要遵守write-WAL-before-data吗?

news/2024/7/9 22:52:33 标签: postgresql, 数据库, group_lsn, clog

总结

从原理上来看,MVCC需要给定事务ID后,能查询到事务的状态。

在PG中事务状态可以从几个路径获取:

  1. 在快照中查询(活跃事务)
  2. 在元组头的状态为查询(不活跃事务)
  3. 在CLOG中查询(不活跃事务)

如果不看实现只看概念,不活跃事务提交状态也可以在XLOG中查询,CLOG可以视作一种XLOG的缓存、映射,一种事务提交状态的快速查询方式。

所以在write-WAL-before-data中,CLOG也会按照data来处理,只有XLOG属于WAL。

clogSlruPhysicalWritePage_17">Postgresql中clog写盘实现SlruPhysicalWritePage

postgresqlclog使用SLRU机制读写,在Slru写盘前,会有保证xlog先写的机制:

  • group_lsn表示32个事务一组中最大的日志序列号(LSN)。
  • group_lsn主要用于事务提交非同步落盘的场景。
static bool
SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruWriteAll fdata)
{
	...
	if (shared->group_lsn != NULL)
	{
		/*
		 * We must determine the largest async-commit LSN for the page. This
		 * is a bit tedious, but since this entire function is a slow path
		 * anyway, it seems better to do this here than to maintain a per-page
		 * LSN variable (which'd need an extra comparison in the
		 * transaction-commit path).
		 */
		XLogRecPtr	max_lsn;
		int			lsnindex,
					lsnoff;

		lsnindex = slotno * shared->lsn_groups_per_page;
		max_lsn = shared->group_lsn[lsnindex++];
		for (lsnoff = 1; lsnoff < shared->lsn_groups_per_page; lsnoff++)
		{
			XLogRecPtr	this_lsn = shared->group_lsn[lsnindex++];

			if (max_lsn < this_lsn)
				max_lsn = this_lsn;    <<<<<<<<<<<<<<<<<<<<<<<<< 找到最大的LSN
		}

		if (!XLogRecPtrIsInvalid(max_lsn))
		{
			/*
			 * As noted above, elog(ERROR) is not acceptable here, so if
			 * XLogFlush were to fail, we must PANIC.  This isn't much of a
			 * restriction because XLogFlush is just about all critical
			 * section anyway, but let's make sure.
			 */
			START_CRIT_SECTION();
			XLogFlush(max_lsn);      <<<<<<<<<<<<<<<<<<<<<<<<< 先保证XLOG写到这个位点!
			END_CRIT_SECTION();
		}
	}
  ...
  if (pg_pwrite(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
  {
    ...
  }
}

Postgresql中用户数据写盘实现FlushBuffer

数据页面同理,也是先找到页面lsn,刷xlog,在写数据。

static void
FlushBuffer(BufferDesc *buf, SMgrRelation reln)
{
	...
	buf_state = LockBufHdr(buf);

	/*
	 * Run PageGetLSN while holding header lock, since we don't have the
	 * buffer locked exclusively in all cases.
	 */
	recptr = BufferGetLSN(buf);   <<<<<<<<<<<<<<<<<<<<<<<<< 找到页面的LSN

	/* To check if block content changes while flushing. - vadim 01/17/97 */
	buf_state &= ~BM_JUST_DIRTIED;
	UnlockBufHdr(buf, buf_state);

	/*
	 * Force XLOG flush up to buffer's LSN.  This implements the basic WAL
	 * rule that log updates must hit disk before any of the data-file changes
	 * they describe do.
	 *
	 * However, this rule does not apply to unlogged relations, which will be
	 * lost after a crash anyway.  Most unlogged relation pages do not bear
	 * LSNs since we never emit WAL records for them, and therefore flushing
	 * up through the buffer LSN would be useless, but harmless.  However,
	 * GiST indexes use LSNs internally to track page-splits, and therefore
	 * unlogged GiST pages bear "fake" LSNs generated by
	 * GetFakeLSNForUnloggedRel.  It is unlikely but possible that the fake
	 * LSN counter could advance past the WAL insertion point; and if it did
	 * happen, attempting to flush WAL through that location would fail, with
	 * disastrous system-wide consequences.  To make sure that can't happen,
	 * skip the flush if the buffer isn't permanent.
	 */
	if (buf_state & BM_PERMANENT)
		XLogFlush(recptr);         <<<<<<<<<<<<<<<<<<<<<<<<< 先保证XLOG写到这个位点!
  
  ...
	smgrwrite(reln,
			  BufTagGetForkNum(&buf->tag),
			  buf->tag.blockNum,
			  bufToWrite,
			  false);
  ...
}

http://www.niftyadmin.cn/n/70854.html

相关文章

更专业、安全、可控!政企都选择WorkPlus私有化部署

现如今政企机构在信息化建设的过程中&#xff0c;内部的沟通协作都离不开即时通讯软件。但大多数企业使用的即时通讯软件都是Saas部署的&#xff0c;虽然使用Saas部署产品成本低&#xff0c;又方便快捷&#xff0c;但还是建议企业有条件最好使用私有化部署的即时通讯软件&#…

【数据治理-4-主数据和数据中台的区别】

数据治理-4-主数据和数据中台的区别主数据和数据中台的区别1.什么是主数据、数据中台2.主数据、数据中台主要解决的问题主数据和数据中台的区别 1.什么是主数据、数据中台 什么是主数据&#xff1a;跨多个业务系统共享的基础静态数据什么是数据中台&#xff1a;将多个业务系统…

大模型为什么是深度学习的未来?

人工智能 | 数据分析 | Chat GPT 深度学习 | 数据挖掘 | 高性能计算 当今社会是科技的社会&#xff0c;是算力快速发展的时代。随着数据中心、东数西算、高性能计算、数据分析、数据挖掘的快速发展&#xff0c;大模型得到了快速地发展。大模型是“大算力强算法”相结合的产物&…

[RK3568 Android12] AP6398S 之WiFi

1:Linux内核配置 根据实际使用的芯片进行驱动配置的开关,我们使用的是:AP6398S 下面是rockchip_defconfig中默认勾选的 CONFIG_WL_ROCKCHIP=y CONFIG_WIFI_BUILD_MODULE=y CONFIG_AP6XXX=m CONFIG_RTL8723CS=m CONFIG_RTL8821CS=m CONFIG_RTL8822BS=m 去掉后面三条RTL相关的 …

C++输入输出(cin和cout)

在C语言中&#xff0c;我们通常会使用 scanf 和 printf 来对数据进行输入输出操作。在C语言中&#xff0c;C语言的这一套输入输出库我们仍然能使用&#xff0c;但是 C 又增加了一套新的、更容易使用的输入输出库。【例1】简单的输入输出代码示例&#xff1a;#include<iostre…

最新版海豚调度dolphinscheduler-3.1.3配置windows本地开发环境

0 说明 本文基于最新版海豚调度dolphinscheduler-3.1.3配置windows本地开发环境&#xff0c;并在windows本地进行调试和开发 1 准备 1.1 安装mysql 可以指定为windows本地mysql&#xff0c;也可以指定为其他环境mysql&#xff0c;若指定为其他环境mysql则可跳过此步。 我这…

大家都在聊的自动化办公到底是什么?

自动化办公无非是excel、ppt、word、邮件、文件处理、数据分析处理、爬虫这些&#xff0c;下面就详细介绍一下&#xff01;文章最后分享了很不错的python学习教程&#xff0c;适合零基础初学的小伙伴&#xff0c;希望可以对你有所帮助&#xff01;&#xff01; excel自动化 我…

测试之路-我曾经跨过无数Bug,也怼过各大佬开发

前言&#xff1a;这是我从事测试的第五个年头的开端&#xff0c;忙忙碌碌到头发现好像忙了个寂寞。也忘了最初走向这条不归路的初心是什么。当时16年学完计算机出来找工作&#xff0c;看着茫茫人海&#xff0c;第一反应就是退缩&#xff0c;该找什么工作&#xff1f;开发&#…