Postgresql源码(35)vacuum freeze起始位点逻辑分析

2023-10-19 12:10

本文主要是介绍Postgresql源码(35)vacuum freeze起始位点逻辑分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

查询指令

https://blog.csdn.net/jackgo73/article/details/122700702


SELECT n.nspname as "Schema", c.relname as "Name", c.relfrozenxid,age(c.relfrozenxid) 
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY c.relfrozenxid::text::bigint DESC;select datname,age(datfrozenxid),2^31-age(datfrozenxid) age_remain from pg_database order by age(datfrozenxid) desc;
select current_database(),rolname,nspname,relkind,relname,age(relfrozenxid),2^31-age(relfrozenxid) age_remain from pg_authid t1 join pg_class t2 on t1.oid=t2.relowner join pg_namespace t3 on t2.relnamespace=t3.oid where t2.relkind in ($$t$$,$$r$$) order by age(relfrozenxid) desc limit 5;select datname,usename,query,xact_start,now()-xact_start xact_duration,query_start,now()-query_start query_duration,state from pg_stat_activity where state<>$$idle$$ and (backend_xid is not null or backend_xmin is not null) and now()-xact_start > interval $$30 min$$ order by xact_start;
select name,statement,prepare_time,now()-prepare_time,parameter_types,from_sql from pg_prepared_statements where now()-prepare_time > interval $$30 min$$ order by prepare_time;

关键参数

系统级


# (final模式默认两亿,把回收xid位点limit设为oldestXmin,然后把limit之前的全部freeze,同时开始告警)
autovacuum_freeze_max_age = 200000000      # 如果表的事务ID年龄大于该值, 即使未开启autovacuum也会强制触发FREEZE, 并告警Preventing Transaction ID Wraparound Failures# (lazy模式默认五千万,把回收xid位点limit设为oldestXmin减五千万,回收垃圾元组的同时顺便freeze)
vacuum_freeze_min_age = 50000000           # 手动或自动垃圾回收时, 如果记录的事务ID年龄大于该值, 将被FREEZE    # (eager模式默认一亿五千万,把回收xid位点limit设为oldestXmin减一亿五千万,按vm按需扫页面然后freeze)
vacuum_freeze_table_age = 150000000        # 手动垃圾回收时, 如果表的事务ID年龄大于该值, 将触发FREEZE. 该参数的上限值为 %95 autovacuum_freeze_max_age    # 组合事务ID限制,同上述配置。
autovacuum_multixact_freeze_max_age = 400000000      # 如果表的并行事务ID年龄大于该值, 即使未开启autovacuum也会强制触发FREEZE, 并告警Preventing Transaction ID Wraparound Failures 
vacuum_multixact_freeze_min_age = 5000000            # 手动或自动垃圾回收时, 如果记录的并行事务ID年龄大于该值, 将被FREEZE    
vacuum_multixact_freeze_table_age = 150000000        # 手动垃圾回收时, 如果表的并行事务ID年龄大于该值, 将触发FREEZE. 该参数的上限值为 %95 autovacuum_multixact_freeze_max_age    # 其他配置
autovacuum_vacuum_cost_delay       # 自动垃圾回收时, 每轮回收周期后的一个休息时间, 主要防止垃圾回收太耗资源. -1 表示沿用vacuum_cost_delay的设置    
autovacuum_vacuum_cost_limit       # 自动垃圾回收时, 每轮回收周期设多大限制, 限制由vacuum_cost_page_hit,vacuum_cost_page_missvacuum_cost_page_dirty参数以及周期内的操作决定. -1 表示沿用vacuum_cost_limit的设置    
vacuum_cost_delay                  # 手动垃圾回收时, 每轮回收周期后的一个休息时间, 主要防止垃圾回收太耗资源.    
vacuum_cost_limit                  # 手动垃圾回收时, 每轮回收周期设多大限制, 限制由vacuum_cost_page_hit,vacuum_cost_page_missvacuum_cost_page_dirty参数以及周期内的操作决定.    

表级

# 系统级:autovacuum_freeze_max_age = 500000000
# 表级分段配置:
alter table t set (autovacuum_freeze_max_age=210000000);
alter table t set (autovacuum_freeze_max_age=220000000);
alter table t set (autovacuum_freeze_max_age=230000000);
..

计算cutoff位点

起始freeze的位点


void
vacuum_set_xid_limits(Relation rel,int freeze_min_age,int freeze_table_age,int multixact_freeze_min_age,int multixact_freeze_table_age,TransactionId *oldestXmin,TransactionId *freezeLimit,TransactionId *xidFullScanLimit,MultiXactId *multiXactCutoff,MultiXactId *mxactFullScanLimit)
{int			freezemin;int			mxid_freezemin;int			effective_multixact_freeze_max_age;TransactionId limit;TransactionId safeLimit;MultiXactId oldestMxact;MultiXactId mxactLimit;MultiXactId safeMxactLimit;/** We can always ignore processes running lazy vacuum.  This is because we* use these values only for deciding which tuples we must keep in the* tables.  Since lazy vacuum doesn't write its XID anywhere, it's safe to* ignore it.  In theory it could be problematic to ignore lazy vacuums in* a full vacuum, but keep in mind that only one vacuum process can be* working on a particular table at any time, and that each vacuum is* always an independent transaction.*/*oldestXmin =TransactionIdLimitedForOldSnapshots(GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM), rel);Assert(TransactionIdIsNormal(*oldestXmin));/** Determine the minimum freeze age to use: as specified by the caller, or* vacuum_freeze_min_age, but in any case not more than half* autovacuum_freeze_max_age, so that autovacuums to prevent XID* wraparound won't occur too frequently.*/// vacuum_freeze_min_age这个值太大了会被置为autovacuum_freeze_max_age的1/2。// 配的太大了会频繁的做freezefreezemin = freeze_min_age;if (freezemin < 0)freezemin = vacuum_freeze_min_age;freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);Assert(freezemin >= 0);/** Compute the cutoff XID, being careful not to generate a "permanent" XID*/limit = *oldestXmin - freezemin;if (!TransactionIdIsNormal(limit))limit = FirstNormalTransactionId;/** If oldestXmin is very far back (in practice, more than* autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum* freeze age of zero.*/safeLimit = ReadNewTransactionId() - autovacuum_freeze_max_age;if (!TransactionIdIsNormal(safeLimit))safeLimit = FirstNormalTransactionId;

下面判断逻辑会根据情况调整freeze起始位点
在这里插入图片描述

	if (TransactionIdPrecedes(limit, safeLimit)){ereport(WARNING,(errmsg("oldest xmin is far in the past"),errhint("Close open transactions soon to avoid wraparound problems.")));limit = *oldestXmin;}*freezeLimit = limit;/** Compute the multixact age for which freezing is urgent.  This is* normally autovacuum_multixact_freeze_max_age, but may be less if we are* short of multixact member space.*/effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();/** Determine the minimum multixact freeze age to use: as specified by* caller, or vacuum_multixact_freeze_min_age, but in any case not more* than half effective_multixact_freeze_max_age, so that autovacuums to* prevent MultiXact wraparound won't occur too frequently.*/mxid_freezemin = multixact_freeze_min_age;if (mxid_freezemin < 0)mxid_freezemin = vacuum_multixact_freeze_min_age;mxid_freezemin = Min(mxid_freezemin,effective_multixact_freeze_max_age / 2);Assert(mxid_freezemin >= 0);/* compute the cutoff multi, being careful to generate a valid value */oldestMxact = GetOldestMultiXactId();mxactLimit = oldestMxact - mxid_freezemin;if (mxactLimit < FirstMultiXactId)mxactLimit = FirstMultiXactId;safeMxactLimit =ReadNextMultiXactId() - effective_multixact_freeze_max_age;if (safeMxactLimit < FirstMultiXactId)safeMxactLimit = FirstMultiXactId;if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit)){ereport(WARNING,(errmsg("oldest multixact is far in the past"),errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));/* Use the safe limit, unless an older mxact is still running */if (MultiXactIdPrecedes(oldestMxact, safeMxactLimit))mxactLimit = oldestMxact;elsemxactLimit = safeMxactLimit;}*multiXactCutoff = mxactLimit;if (xidFullScanLimit != NULL){int			freezetable;Assert(mxactFullScanLimit != NULL);/** Determine the table freeze age to use: as specified by the caller,* or vacuum_freeze_table_age, but in any case not more than* autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly* VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples* before anti-wraparound autovacuum is launched.*/freezetable = freeze_table_age;if (freezetable < 0)freezetable = vacuum_freeze_table_age;freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);Assert(freezetable >= 0);/** Compute XID limit causing a full-table vacuum, being careful not to* generate a "permanent" XID.*/limit = ReadNewTransactionId() - freezetable;if (!TransactionIdIsNormal(limit))limit = FirstNormalTransactionId;*xidFullScanLimit = limit;/** Similar to the above, determine the table freeze age to use for* multixacts: as specified by the caller, or* vacuum_multixact_freeze_table_age, but in any case not more than* autovacuum_multixact_freeze_table_age * 0.95, so that if you have* e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to* freeze multixacts before anti-wraparound autovacuum is launched.*/freezetable = multixact_freeze_table_age;if (freezetable < 0)freezetable = vacuum_multixact_freeze_table_age;freezetable = Min(freezetable,effective_multixact_freeze_max_age * 0.95);Assert(freezetable >= 0);/** Compute MultiXact limit causing a full-table vacuum, being careful* to generate a valid MultiXact value.*/mxactLimit = ReadNextMultiXactId() - freezetable;if (mxactLimit < FirstMultiXactId)mxactLimit = FirstMultiXactId;*mxactFullScanLimit = mxactLimit;}else{Assert(mxactFullScanLimit == NULL);}
}

验证

实测


select txid_current(); # 14076185# 创建表时relfrozenxid就是当前的事务ID,age=0(currentxid - 创建表时relfrozenxid就是当前的事务ID,age)
create table t9(i int);select age(relfrozenxid),relfrozenxid,relname,pg_size_pretty(pg_total_relation_size(oid)) from pg_class where relname='t9';  age | relfrozenxid | relname | pg_size_pretty 
-----+--------------+---------+----------------1 |     14076186 | t9      | 0 bytesinsert into t9 values (1);
insert into t9 values (2);postgres=# select t_xmin,t_xmax,t_infomask2,t_infomask from heap_page_items(get_raw_page('t9', 0));t_xmin  | t_xmax | t_infomask2 | t_infomask 
----------+--------+-------------+------------14076187 |      0 |           1 |       204814076188 |      0 |           1 |       2048postgres=# select * from pg_stat_user_tables  where relname='t9';
-[ RECORD 1 ]-------+-------
relid               | 135092
schemaname          | public
relname             | t9
seq_scan            | 0
seq_tup_read        | 0
idx_scan            | 
idx_tup_fetch       | 
n_tup_ins           | 2
n_tup_upd           | 0
n_tup_del           | 0
n_tup_hot_upd       | 0
n_live_tup          | 2
n_dead_tup          | 0
n_mod_since_analyze | 2
last_vacuum         |    (执行了,不管清理没清理,都会更新时间)
last_autovacuum     | 
last_analyze        | 
last_autoanalyze    | 
vacuum_count        | 0
autovacuum_count    | 0
analyze_count       | 
autoanalyze_count   | 0【1】启动长事务
【2】启动消耗事务ID:pgbench -M prepared -n -r -P 1 -f ./test.sql -c 32 -j 32 -T 10  # (1)到vacuum_freeze_min_age=5000W时,因为长事务存在,不能freeze。
# (2)不会有报错
# (3)但会提示最老事务的事务ID:oldest xmin: 14076189(就是那个长事务)
vacuum (freeze,verbose) t9;
INFO:  vacuuming "public.t9"
INFO:  "t9": found 0 removable, 2 nonremovable row versions in 1 out of 1 pages
DETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 14076189
There were 0 unused item pointers.
Skipped 0 pages due to buffer pins, 0 frozen pages.
0 pages are entirely empty.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
VACUUMpostgres=# select age(relfrozenxid),relfrozenxid,relname,pg_size_pretty(pg_total_relation_size(oid)) from pg_class where relname='t9';  age    | relfrozenxid | relname | pg_size_pretty 
----------+--------------+---------+----------------59056502 |     14076189 | t9      | 40 kB# (1)到vacuum_freeze_table_age=15000W时,因为长事务存在,不能freeze。
# (2)不会有报错
# (3)但会提示最老事务的事务ID:oldest xmin: 14076189(就是那个长事务)
postgres=# vacuum (freeze,verbose) t9;
vacuum (freeze,verbose) t9;
INFO:  vacuuming "public.t9"
INFO:  "t9": found 0 removable, 2 nonremovable row versions in 1 out of 1 pages
DETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 14076189
There were 0 unused item pointers.
Skipped 0 pages due to buffer pins, 0 frozen pages.
0 pages are entirely empty.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
VACUUMpostgres=# select age(relfrozenxid),relfrozenxid,relname,pg_size_pretty(pg_total_relation_size(oid)) from pg_class where relname='t9';  age    | relfrozenxid | relname | pg_size_pretty 
-----------+--------------+---------+----------------153257008 |     14076189 | t9      | 40 kB# (1)到autovacuum_freeze_max_age=20000W时,因为长事务存在,不能freeze。
# (2)开始报警,但是没报错
# (3)但会提示最老事务的事务ID:oldest xmin: 14076189(就是那个长事务)
postgres=# vacuum (freeze,verbose) t9;
WARNING:  oldest xmin is far in the past
HINT:  Close open transactions soon to avoid wraparound problems.
INFO:  vacuuming "public.t9"
INFO:  "t9": found 0 removable, 2 nonremovable row versions in 1 out of 1 pages
DETAIL:  0 dead row versions cannot be removed yet, oldest xmin: 14076189
There were 0 unused item pointers.
Skipped 0 pages due to buffer pins, 0 frozen pages.
0 pages are entirely empty.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
VACUUM
postgres=# select age(relfrozenxid),relfrozenxid,relname,pg_size_pretty(pg_total_relation_size(oid)) from pg_class where relname='t9';  age    | relfrozenxid | relname | pg_size_pretty 
-----------+--------------+---------+----------------213075933 |     14076189 | t9      | 40 kB

这篇关于Postgresql源码(35)vacuum freeze起始位点逻辑分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/239655

相关文章

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

Java ArrayList扩容机制 (源码解读)

结论:初始长度为10,若所需长度小于1.5倍原长度,则按照1.5倍扩容。若不够用则按照所需长度扩容。 一. 明确类内部重要变量含义         1:数组默认长度         2:这是一个共享的空数组实例,用于明确创建长度为0时的ArrayList ,比如通过 new ArrayList<>(0),ArrayList 内部的数组 elementData 会指向这个 EMPTY_EL

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者

MOLE 2.5 分析分子通道和孔隙

软件介绍 生物大分子通道和孔隙在生物学中发挥着重要作用,例如在分子识别和酶底物特异性方面。 我们介绍了一种名为 MOLE 2.5 的高级软件工具,该工具旨在分析分子通道和孔隙。 与其他可用软件工具的基准测试表明,MOLE 2.5 相比更快、更强大、功能更丰富。作为一项新功能,MOLE 2.5 可以估算已识别通道的物理化学性质。 软件下载 https://pan.quark.cn/s/57

工厂ERP管理系统实现源码(JAVA)

工厂进销存管理系统是一个集采购管理、仓库管理、生产管理和销售管理于一体的综合解决方案。该系统旨在帮助企业优化流程、提高效率、降低成本,并实时掌握各环节的运营状况。 在采购管理方面,系统能够处理采购订单、供应商管理和采购入库等流程,确保采购过程的透明和高效。仓库管理方面,实现库存的精准管理,包括入库、出库、盘点等操作,确保库存数据的准确性和实时性。 生产管理模块则涵盖了生产计划制定、物料需求计划、

衡石分析平台使用手册-单机安装及启动

单机安装及启动​ 本文讲述如何在单机环境下进行 HENGSHI SENSE 安装的操作过程。 在安装前请确认网络环境,如果是隔离环境,无法连接互联网时,请先按照 离线环境安装依赖的指导进行依赖包的安装,然后按照本文的指导继续操作。如果网络环境可以连接互联网,请直接按照本文的指导进行安装。 准备工作​ 请参考安装环境文档准备安装环境。 配置用户与安装目录。 在操作前请检查您是否有 sud

线性因子模型 - 独立分量分析(ICA)篇

序言 线性因子模型是数据分析与机器学习中的一类重要模型,它们通过引入潜变量( latent variables \text{latent variables} latent variables)来更好地表征数据。其中,独立分量分析( ICA \text{ICA} ICA)作为线性因子模型的一种,以其独特的视角和广泛的应用领域而备受关注。 ICA \text{ICA} ICA旨在将观察到的复杂信号

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。