(资料图)
从原理上来看,MVCC需要给定事务ID后,能查询到事务的状态。
在PG中事务状态可以从几个路径获取:
在快照中查询(活跃事务)在元组头的状态为查询(不活跃事务)在CLOG中查询(不活跃事务)如果不看实现只看概念,不活跃事务提交状态也可以在XLOG中查询,CLOG可以视作一种XLOG commit/rollback日志的缓存、映射,一种事务提交状态的快速查询方式。
所以在write-WAL-before-data中,CLOG也会按照data来处理,只有XLOG属于WAL。
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) { ... }}
数据页面同理,也是先找到页面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); ...}
上一篇:九州娱乐城|全球快看点
下一篇:如何评价国产3D动画反斗天庭
5月17日,河南省南阳市博物馆,游客在观看春秋时期的青铜壶。5月18日是国际博物馆日,河南省南阳市多家博物馆开展线上线下活动,让游客感受
一条渠,绵延千里北上,滋润人间心田。2014年12月12日,南水北调中线工程正式通水。从此,甘甜可口的长江水奔腾不息地流经1432公里,流进千
5月14日下午,在上海疫情防控一线奋战39天,总计圆满完成130万管核酸检测任务的河南省援沪核酸检测医疗队队员乘坐飞机回到郑州,受到来自省
近日,中国气象服务协会公布了我国首批15个天气气候景观观赏地,江西庐山云海景观、安徽歙县坡山村云海景观、福建霞浦县三沙日出日落景观等
5月9日,宁西线豫陕交界处,中国铁路郑州局集团有限公司南阳工务段护路员工对线路两边山体的危树进行清理整治。立夏时节,秦岭山脉云雾缭绕
X 关闭
X 关闭