系统与基础设施sqlite.org
SQLite:Wal2 模式
SQLite: Wal2 Mode
2022 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
SQLite 官方文档介绍实验性的 wal2 模式:用两个 WAL 文件交替写入,当一个写满后切换到另一个,从而让前一个可以被 checkpoint 后复用。这解决了传统 WAL 模式下恢复日志可能增长过大的问题,但该模式尚未达到生产稳定状态。
为什么经久不衰:它展示了 SQLite 如何用一个简单优雅的双文件设计解决 WAL checkpoint 的经典难题,对理解数据库日志机制很有启发。
HN 评论精选
认为双文件交替的设计非常合理,不解为何最初没这样实现
wal2 模式用两个 WAL 文件交替写入:先写第一个,写满后切换到第二个,此时第一个可以被 checkpoint 后覆盖,如此往复。这个设计如此合乎逻辑,我不明白当初 WAL 模式为什么不这么做——大概是被当作过早优化而否决了吧。期待它正式发布。
miroljub查看英文原文
> In wal2 mode, the system uses two wal files instead of one. The files are named "<database>-wal" and "<database>-wal2", where "<database>" is of course the name of the database file. When data is written to the database, the writer begins by appending the new data to the first wal file. Once the first wal file has grown large enough, writers switch to appending data to the second wal file. At this point the first wal file can be checkpointed (after which it can be overwritten). Then, once the second wal file has grown large enough and the first wal file has been checkpointed, writers switch back to the first wal file. And so on. Looks so logical that I don't understand why WAL mode was not implemented like this from the get go. Probably an optimization wrongly dismissed as premature? Anyways, looking forward to this mode reaching general availability.
指出 wal2 解决 WAL 日志过大的问题,但还不稳定
SQLite 有不同的模式来支持恢复,新的 WAL2 模式解决的是 WAL(版本 1)中恢复日志可能变得非常大的问题。它解决的是真实问题,但尚未被认为可用于生产环境。
ikawe查看英文原文
SQLite has different “modes” to facilitate recovery, this new WAL2 mode addresses the problem in WAL (version 1) where the recovery log could potentially grow very large. It’s solving a real problem, but not considered stable for production yet.
类比双缓冲机制,一语点破设计本质
所以这基本上就是双缓冲,只不过用在数据库上?说得通。
runiq查看英文原文
So it's basically double buffering, but for databases? That makes sense.
质疑 WAL 批量落盘是否削弱了其崩溃恢复的保障
题外话:一直困扰我的是,WAL 本应保障数据完整性、支持崩溃恢复,但这个文件本身却是批量写入磁盘而非每次变更后立即写入,显然是为了性能。这不是违背了初衷吗?为什么没出问题?不限于 SQLite,是数据库的通病,一直没找到答案。
raj555查看英文原文
tangential point: one thing that always bothered me about WAL is that it is supposed to exist to help maintain data integrity, recover from crash etc but that file itself is written (committed to disk reliably) in batches and not after every change to the database, apparently to gain performance. Doesn't that defeat the purpose? How haven't things broken down despite this? Not specific to sqlite but databases in general. Never found an answer to this.
历次讨论
同类还值得读
TTY 揭秘(2008)
The TTY demystified (2008)
经典文章深入剖析了 Unix/Linux 中 TTY 子系统的工作原理,包括线路规程、终端驱动、信号与控制字符等机制。
系统与基础设施上首页 11 次 · 2009–2025linusakesson.net编辑精选为什么 GNU grep 这么快
Why GNU grep is Fast
FreeBSD 邮件列表上一篇广为流传的帖子,解释 GNU grep 为什么快:核心在于 Boyer-Moore 字符串搜索算法配合精心优化的内部循环和 I/O,让程序在搜索时“…
系统与基础设施上首页 9 次 · 2010–2022lists.freebsd.orgXplain:为普通人讲解 X11
Xplain – Explaining X11 for the rest of us
《Xplain》是作者(HN 用户 Jasper_)写的一系列 X11 长文,用他在浏览器中重建的 X11 绘图模型做交互演示,讲解 X11 的绘图模型与窗口系统设计。
系统与基础设施上首页 6 次 · 2013–2020magcius.github.io编辑精选旋风教程:为 Linux 创建极小 ELF 可执行文件(1999)
A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux (1999)
breadbox 写于 1999 年的经典教程,主题是如何为 Linux 手工构造并裁剪出体积极小的 ELF 可执行文件。
系统与基础设施上首页 6 次 · 2007–2025muppetlabs.comPostgreSQL 内部原理
The internals of PostgreSQL
《The Internals of PostgreSQL》是一本免费在线书籍,系统讲解 PostgreSQL 的内部实现,包括查询处理、并发控制、WAL、缓冲区管理等核心机制。
系统与基础设施上首页 5 次 · 2017–2025interdb.jp编辑精选