PostgreSQL中的VACUUM

本文详细介绍了PostgreSQL的VACUUM操作,包括其任务:删除死亡元组和冻结事务ID,以及不同模式如Concurrent和Full VACUUM的工作原理。VACUUM还涉及VM(可见性映射)、Freeze处理、删除不必要的Clog文件和Autovacuum机制。此外,文章讨论了VACUUM的优化,如VM的增强版和eager模式下的Freeze处理改进。
摘要由CSDN通过智能技术生成

最近搞了一个公众号PostgreSQL运维技术,欢迎来踩~

悄悄放一张:

PostgreSQL运维技术 

 

我在之前的一篇介绍MVCC的文章中提到,MVCC有事务ID回卷的问题,它的解决方案是VACUUM。但是没有展开讲。这章的话,会详细地介绍下VACUUM。

VACUUM的两个主要任务是删除死亡元组和冻结事务ID

为了删除死亡元组,VACUUM提供了两种模式,即Concurrent Vacuum和Full Vacuum

Concurrent Vacuum通常简称为VACUUM。它在删除表文件中的死亡元组时,其他事务仍可以在此进程运行时读取表。而Full VACUUM在运行时其他事务不能访问表。

 

本章将主要描述以下内容

VM(可见性映射)

Freeze

删除不必要的clog

AutoVacuum

Full VACUUM

 

 

注:本文是The Internals of PostgreSQL第六章


 

6.1. VACUUM的概述

 

VACUUM对数据库中的表执行以下的任务:

1、删除死亡元组

    删除死亡元组和整理每个页面的活元组。

    删除指向死元组的索引元组。

2、冻结old txids

    如有必要,冻结元组的old txids。

    更新冻结的txid相关的系统目录(pg_database和pg_class)。

    如果可能,清除clog不必要的部分。

3、其他

    更新已处理表的FSM和VM。

    更新一些统计信息(pg_stat_all_tables等)。

 

下面的伪代码描述了VACUUM处理。


(1)  FOR each table
(2)       Acquire ShareUpdateExclusiveLock lock for the target table

          /* The first block */
(3)       Scan all pages to get all dead tuples, and freeze old tuples if necessary 
(4)       Remove the index tuples that point to the respective dead tuples if exists

          /* The second block */
(5)       FOR each page of the table
(6)            Remove the dead tuples, and Reallocate the live tuples in the page
(7)            Update FSM and VM
           END FOR

          /* The third block */
(8)       Clean up indexes
(9)       Truncate the last page if possible
(10       Update both the statistics and system catalogs of the target table
           Release ShareUpdateExclusiveLock lock
       END FOR

        /* Post-processing */
(11)  Update statistics and system catalogs
(12)  Remove both unnecessary files and page
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值