【世界聚看点】PostgreSQL的clog属于日志还是数据,需要遵守write-WAL-before-data吗?
发稿时间:2023-03-06 17:21:17 来源: 腾讯云
【资料图】
总结
从原理上来看,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); ...}
- 【世界聚看点】PostgreSQL的clog属于日志还是数据,需要遵守write-WAL-before-data吗?
- 春到峡江来 樱桃花开白
- 房贷利息抵扣个税必须是首套房吗?房贷利息抵扣个税需要满足什么条件?
- 今日热搜:求重生到未来的父子文谢
- 买房贷款审批完什么时候还房贷?银行审批通过会放款吗?
- 房屋抵押银行贷款需要什么条件?房屋抵押贷款办理流程及手续是什么?
- 花呗借呗不还会影响征信吗?花呗借呗不还有什么后果?
- 世界简讯:新集能源(601918)3月6日主力资金净卖出1083.05万元
- 美团借钱还不上怎么办?美团月付算不算网贷?
- 场内基金卖出是按实时价格吗?场内基金和股票的玩法一致吗?
- 新手买a类基金还是c类基金?新手买基金怎么选择?
- 基金6个月持有期是什么意思?基金6个月持有期风险大吗?
- 一周买几次基金比较合适?周五适合买入基金吗?
- 混合型基金是什么意思?为什么不建议新手买混合型基金?
- 基金为什么限购?基金被限购了怎么办?
- 基金周五卖出按哪一天净值?基金周五卖出好吗?
- 微信语音通话铃声怎么设置自己喜欢的音乐_微信语音通话铃声设置自己喜欢的音乐教程
- 基金持有期3个月好吗?基金持有三个月会不会亏钱?
- 基金一直跌不加仓能涨回来?基金一直跌怎么办?
- 基金和股票的区别是什么?基金和股票哪个收益更高?
- 全球速递!微信如何设置自己的主题皮肤
- 基金可以停止定投吗?基金定投停止但没赎回有收益吗?
- 红利指数基金是什么?红利指数有什么优缺点?
- 基金需要止损吗?基金怎么止损?
- 封闭基金会亏损吗?封闭基金亏损怎么办?
- 巢湖健康证办理电话汇总
- 当前报道:service unavailable是什么意思中文翻译_503 service unavailable什么意思
- 当前快报:邹鹰农牧股票最新消息_邹鹰农牧股票
- 月经量少吃什么中药可以调理_月经量少吃什么好
- 天天热点评!中国足彩网竞彩6日推荐:布伦特福德主场奏凯
延伸阅读
创业