游标长时间open导致表无法vacuum问题
一、问题描述
用户在实际中可能会碰到类似以下 dead rows 无法 vacuum的问题,一个可能的原因是由于游标未结束的原因。
test=# vacuum(verbose) t1;
INFO: vacuuming "public.t1"
INFO: "t1": found 0 removable, 985 nonremovable row versions in 66 out of 67 pages
DETAIL: 788 dead row versions cannot be removed yet, oldest xmin: 4996
There were 0 unused item identifiers.
Skipped 1 page 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.
INFO: vacuuming "pg_toast.pg_toast_29558"
INFO: index "pg_toast_29558_index" now contains 0 row versions in 1 pages
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO: "pg_toast_29558": found 0 removable, 0 nonremovable row versions in 0 out of 0 pages
DETAIL: 0 dead row versions cannot be removed yet, oldest xmin: 4996
There were 0 unused item identifiers.
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.
二、举例说明
1、事务不提交的情况
创建测试数据:
create table t1(id integer,name text);
create table t2(id integer);
insert insert t1 select generate_series(1,1000),repeat('a',500);
创建测试过程:
create or replace procedure proc01_nocommit as
cursor c01 is select id from t1;
v_id integer;
begin
open c01;
fetch c01 into v_id;
while (c01%FOUND) loop
insert into t2 values(v_id);
perform pg_sleep(1);
fetch c01 into v_id;
end loop;
end;
/
session 1:
call proc01_nocommit();
session 2:
test=# select * from pg_locks where relation='t1'::regclass;
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+--------+-----------------+---------+----------
relation | 29504 | 29558 | | | | | | | | 4/2737 | 360612 | AccessShareLock | t | t
(1 row) test=# delete from t1 where mod(id,5)<>1;
DELETE 200
无法 vacuum
test=# vacuum(verbose) t1;
INFO: vacuuming "public.t1"
INFO: "t1": found 0 removable, 985 nonremovable row versions in 66 out of 67 pages
DETAIL: 788 dead row versions cannot be removed yet, oldest xmin: 4996
There were 0 unused item identifiers.
Skipped 1 page 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.
INFO: vacuuming "pg_toast.pg_toast_29558"
INFO: index "pg_toast_29558_index" now contains 0 row versions in 1 pages
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO: "pg_toast_29558": found 0 removable, 0 nonremovable row versions in 0 out of 0 pages
DETAIL: 0 dead row versions cannot be removed yet, oldest xmin: 4996
There were 0 unused item identifiers.
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
结论:可以看到对象有 AccessShareLock 锁(实际验证,不管只要 cursor 定义了,不管是否open ,都会有 AccessShareLock 锁)。
2、事务提交的情景
修改过程块如下:
create or replace procedure proc01_commit as
cursor c01 is select id from t1;
v_id integer;
begin
open c01;
fetch c01 into v_id;
while (c01%FOUND) loop
insert into t2 values(v_id);
perform pg_sleep(1);
fetch c01 into v_id;
commit;
end loop;
end;
/
可以看到在游标内部加了commit;
PS:KingbaseES 支持游标跨事务的场景。在 commit时,会将剩余未完成游标的结果取回到临时文件,这样可以保证MVCC 机制。
Session 1:
call proc01_commit()
Session 2:
test=# select * from pg_locks where relation='t1'::regclass;
locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
----------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+--------+-----------------+---------+----------
(0 row) test=# delete from t1 where mod(id,5)<>0;
DELETE 800
结论:没有锁,可以vacuum。
test=# vacuum (verbose) t1;
INFO: vacuuming "public.t1"
INFO: "t1": removed 800 row versions in 67 pages
INFO: "t1": found 800 removable, 200 nonremovable row versions in 67 out of 67 pages
DETAIL: 0 dead row versions cannot be removed yet, oldest xmin: 4857
There were 0 unused item identifiers.
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.
INFO: vacuuming "pg_toast.pg_toast_29549"
INFO: index "pg_toast_29549_index" now contains 0 row versions in 1 pages
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s.
INFO: "pg_toast_29549": found 0 removable, 0 nonremovable row versions in 0 out of 0 pages
DETAIL: 0 dead row versions cannot be removed yet, oldest xmin: 4857
There were 0 unused item identifiers.
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
游标长时间open导致表无法vacuum问题的更多相关文章
- 防止shell脚本长时间执行导致ssh超时
在一些对安全性要求较高的场景下.ssh的超时时间是管理员预先设置好的,在闲置一段时间后ssh连接会自己主动断开. 这样的情况下假设通过ssh运行脚本,而脚本运行时间又比較长的话.会导致sshclien ...
- JVM 调优 —— GC 长时间停顿问题及解决方法
零. 简介 垃圾收集器长时间停顿,表现在 Web 页面上可能是页面响应码 500 之类的服务器错误问题,如果是个支付过程可能会导致支付失败,将造成公司的直接经济损失,程序员要尽量避免或者说减少此类情况 ...
- SQL一次查出相关类容避免长时间占用表(下)
/* server: db: EDI */ -- 以下案例多次查询同一张表,仅有Name条件不同 --可以使用一次查出相关类容避免长时间占用表 USE EDI GO DECLARE @FileType ...
- SQL一次查出相关类容避免长时间占用表(上)
/* server: db: EDI */ -- 以下案例多次查询同一张表,仅有组合条件Name+Direction不同 --可以使用一次查出相关类容避免长时间占用表 USE EDI GO DECLA ...
- kali长时间未使用导致数字签名过期无法更新源解决办法
kali长时间未使用,数字签名会过期,从而导致无法更新源. apt-get update:从源(Source)服务器那里下载最新的软件包列: apt-get upgrade:对已经安装的软件包本身进行 ...
- 查看Oracle中存储过程长时间被卡住的原因
1:查V$DB_OBJECT_CACHE SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CUX_OE_ORDER_RPT_PKG' AND LOCKS!='0 ...
- ASP.NET 工作流:支持长时间运行操作的 Web 应用程序
ASP.NET 工作流 支持长时间运行操作的 Web 应用程序 Michael Kennedy 代码下载位置:MSDN 代码库 在线浏览代码 本文将介绍以下内容: 独立于进程的工作流 同步和异步活 ...
- ios之申请后台延时执行和做一个假后台的方法(系统进入长时间后台后,再进入前台部分功能不能实现)
转自:http://sis hu ok.com/forum/blogCategory/showByCategory.html?categories_id=138&user_id=10385 ...
- 为什么drop table的时候要在checking permissions花很长时间?
昨天,我drop一个表的时候在checking permissions花了20s+,这个时间花在哪里了呢?经常查找发现我的配置文件innodb_file_per_table=1的,innodb需要遍历 ...
随机推荐
- SAP APO-主数据设置
可以在SAP APO的相关组件中创建主数据,也可以将其从SAP R / 3传输到SAP APO. 可以使用核心接口(CIF)将其传输到SAP APO模块. 在主数据集成模型中,您定义将主数据传输到SA ...
- 2022giao考游记
Day -12: 今年高考准备去考着玩玩,考前心态十分稳健.~~毕竟我才高一/cy~~ 这次高考我倒是没啥目标,主要是来试试水,感受一下高考的氛围,体会一下自己和高三应届生们的水平的差距.也算是丰富自 ...
- rhel安装vmtools
第一步,vmware登录虚拟机,菜单栏找到"虚拟机"--"安装TOOLS" //如果打开虚拟机的光驱后没有文件.那么重复以上操作. 第二步,拷贝压缩文件到桌面: ...
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
更多技术交流.求职机会.试用福利,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 ClickHouse 作为目前业内主流的列式存储数据库(DBMS)之一,拥有着同类型 DBMS 难以企及 ...
- Learning Latent Graph Representations for Relational VQA
The key mechanism of transformer-based models is cross-attentions, which implicitly form graphs over ...
- Error Code: 1054. Unknown column '字段名' in 'field list'
问题描述: j博主在java开发过程中,通过读取excel中表名和字段名,动态创建insert的SQL语句,在mysql可视化工具中执行此SQL语句时,一直提示"Error Code: 10 ...
- git.exe pull --progress -v --no-rebase "origin" master
git.exe pull --progress -v --no-rebase "origin" master remote: You do not have permission ...
- MySQL--创建计算字段
存储在数据库表中的数据一般不是应用程序所需要的格式.下面举几个例子. 如果想在一个字段中既显示公司名,又显示公司的地址,但这两个信息一般包含在不同的表列中. 城市.州和邮政编码存储在不同的列中 ...
- python将命令输出写入文件或临时缓存
python将命令输出写入文件 将文件写入到对应文件,方便后期处理或保存 def write_file(file_path): with open(file=file_path, mode=" ...
- javascript引用奇趣
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...