在 PostgreSQL 中,支持多种类型的表 — 临时表、普通表,以及无日志表,顾名思义,”无日志”其优势在于不用记录 WAL,那么写入速度自然也杠杠的,同理,无日志表在备库上没有数据 (只有一个壳),也无法进行访问,会提示 ERROR: cannot access temporary or unlogged relations during recovery,pg_basebackup 的时候也会跳过无日志表 (除了 init 分支)。那么无日志表又有哪些鲜为人知的细节呢?
分析
首先,让我们思考一下,无日志表的数据会落盘吗?或者说,按照常识,执行正常检查点的时候会落盘吗?
However, they are not crash-safe: an unlogged table is automatically truncated after a crash or unclean shutdown. The contents of an unlogged table are also not replicated to standby servers. Any indexes created on an unlogged table are automatically unlogged as well.
/* * Unless this is a shutdown checkpoint or we have been explicitly told, * we write only permanent, dirty buffers. But at shutdown or end of * recovery, we write all dirty buffers. */ if (!((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY | CHECKPOINT_FLUSH_ALL)))) mask |= BM_PERMANENT;
/* * mdextend() -- Add a block to the specified relation. * * The semantics are nearly the same as mdwrite(): write at the * specified position. However, this is to be used for the case of * extending a relation (i.e., blocknum is at or beyond the current * EOF). Note that we assume writing a block beyond current EOF * causes intervening file space to become filled with zeroes. */ void mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum, constvoid *buffer, bool skipFsync) { off_t seekpos; int nbytes; MdfdVec *v;
见微知著
简单浏览了一下代码,CHECKPOINT_FLUSH_ALL 一共在两处进行了调用
CreateDatabaseUsingFileCopy:Create a new database using the FILE_COPY strategy. FILE_COPY 可以参照官网
/* * Perform a checkpoint --- either during shutdown, or on-the-fly * * flags is a bitwise OR of the following: * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown. * CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery. * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP, * ignoring checkpoint_completion_target parameter. * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occurred * since the last one (implied by CHECKPOINT_IS_SHUTDOWN or * CHECKPOINT_END_OF_RECOVERY). * CHECKPOINT_FLUSH_ALL: also flush buffers of unlogged tables.
评论区按需加载,正文阅读不会被第三方服务拖慢。