PostgreSQL的clog属于日志还是数据,需要遵守write-WAL-before-data吗?|全球观热点

来源:腾讯云 时间:2023-03-07 04:21:29


(资料图)

总结

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

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

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

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

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

Postgresql中clog写盘实现SlruPhysicalWritePage

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

group_lsn表示32个事务一组中最大的日志序列号(LSN)。group_lsn主要用于事务提交非同步落盘的场景。
static boolSlruPhysicalWritePage(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). */XLogRecPtrmax_lsn;intlsnindex,lsnoff;lsnindex = slotno * shared->lsn_groups_per_page;max_lsn = shared->group_lsn[lsnindex++];for (lsnoff = 1; lsnoff < shared->lsn_groups_per_page; lsnoff++){XLogRecPtrthis_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 voidFlushBuffer(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);  ...}

X 关闭

PostgreSQL的clog属于日志还是数据,需要遵守write-WAL-before-data吗?|全球观热点

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

2023-03-07

糖原生成_关于糖原生成介绍

1、糖原生成(英语:Glycogenesis)是指生物体中糖原合成的过程,其中葡萄糖分子被添加到糖原链上以用于储存。

2023-03-06

男士适合什么牌子的手表 全球快看点

合适的有欧米茄海马300MasterCo-Axial、豪利时BigCrownProPilotGMT计时表、波尔EngineerHydroca

2023-03-06

哥本哈根在哪

1、哥本哈根(丹麦语:København;英语:Copenhagen),是丹麦王国的首都、最大城市及最大港口,也是北欧最

2023-03-06

环球要闻:死亡25例!内蒙古卫健委发布最新报告

死亡25例!内蒙古卫健委发布最新报告

2023-03-06

土耳其:至少247名涉嫌参与建造劣质建筑人员被列入拘捕名单_微动态

据土耳其阿纳多卢通讯社当地时间3月6日报道,土耳其南部2月6日发生强震后,在针对受损毁的建筑物的调查中,该国司法部门目前

2023-03-06

69堂

1、最简单的事情往往有最大的学问在里面,也总是最难做好!譬如说话,既然出口,就再也不能改变,荣耀悔恨往往决定在上下两唇轻

2023-03-06

陕西煤业:2022年净利润351.49亿元,同比增64.1%

36氪获悉,陕西煤业发布业绩快报,2022年公司营业收入为1692 09亿元,同比增长11 1%;净利润351 49亿元,同比增长64 1%;基本每股收益3 6

2023-03-06

【环球聚看点】创新奇智涨超13% 机构称MMOC提高客户满意度和复购率

创新奇智(02121)午盘上涨13 33%,现报18 54港元,成交额5780 94万港元。国海证券表示,创新奇智MMOC平台是支持AI解决方案创新、研发和交付

2023-03-06

世界资讯:上海老庙黄金价格今天多少一克(2023年03月06日)

上海老庙黄金价格今天多少一克(2023年03月06日)每日更新

2023-03-06

Copyright   2015-2022 青年科技网版权所有  备案号: 皖ICP备2022009963号-20   联系邮箱:39 60 291 42@qq.com