当前位置: 首页 > news >正文

PostgreSQL的扩展(extensions)-常用的扩展-pg_dirtyread

PostgreSQL的扩展(extensions)-常用的扩展-pg_dirtyread

pg_dirtyread 是 PostgreSQL 的一个特殊扩展,它允许读取已被删除但尚未被 VACUUM 清理的数据行,是数据恢复的重要工具。

原理:
pg_dirtyread 通过直接访问表的底层页面,绕过 PostgreSQL 正常的可见性规则检查:

  • 读取表的物理页面数据
  • 忽略 xmax 标记(删除事务ID)
  • 返回所有行版本,包括被删除的行

时间窗口限制:

  • 只能读取尚未被 VACUUM 清理的数据
  • 常规表:通常保留几小时到几天
  • 频繁更新的表:保留时间更短

不支持的场景:

  • TRUNCATE 操作删除的数据
  • DROP TABLE 删除的表
  • 已执行 VACUUM FULL 的表

一 下载并编译安装

1.1 下载

在这里插入图片描述

下载网址:
https://github.com/df7cb/pg_dirtyread/tags

1.2 编译安装

make
make install

1.3 创建 pg_dirtyread

–修改postgresql.conf文件

shared_preload_libraries = 'pg_stat_kcache,pg_stat_statements,auto_explain,pg_dirtyread'        # (change requires restart)

–创建extension

white=# create extension pg_dirtyread;
CREATE EXTENSION
white=# 
white=# select * from pg_EXTENSION;
  oid  |      extname       | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition 
-------+--------------------+----------+--------------+----------------+------------+-----------+--------------
 14270 | plpgsql            |       10 |           11 | f              | 1.0        |           | 
 17620 | pg_repack          |       10 |         2200 | f              | 1.5.0      |           | 
 17659 | pg_stat_statements |       10 |         2200 | t              | 1.10       |           | 
 17739 | pgstattuple        |       10 |         2200 | t              | 1.5        |           | 
 17840 | pg_bulkload        |       10 |         2200 | f              | 3.1.21     |           | 
 17861 | pg_dirtyread       |       10 |         2200 | t              | 2          |           | 
(6 rows)

二 测试

2.1 测试一:先delete,再关闭表的autovacuum。(找回失败)

white=# select count(*) from yewu1.t3;
 count 
-------
   100
(1 row)

white=# 
white=# delete from yewu1.t3 where id >10;
DELETE 90
white=# 
white=# select count(*) from yewu1.t3;
 count 
-------
    10
(1 row)

white=# ALTER TABLE yewu1.t3 SET (
white(#       autovacuum_enabled = false, toast.autovacuum_enabled = false
white(#     );
ALTER TABLE
white=# 
white=# SELECT * FROM pg_dirtyread('yewu1.t3') as t(id int, name varchar(20));
 id |  name   
----+---------
  1 | haha_1
  2 | haha_2
  3 | haha_3
  4 | haha_4
  5 | haha_5
  6 | haha_6
  7 | haha_7
  8 | haha_8
  9 | haha_9
 10 | haha_10
(10 rows)

white=# 

2.2 测试二:先关闭表的autovacuum,再delete。(找回成功)

white=# select count(*) from yewu1.t3;
 count 
-------
   100
(1 row)

white=# ALTER TABLE yewu1.t3 SET (
white(#       autovacuum_enabled = false, toast.autovacuum_enabled = false
white(#     );
ALTER TABLE
white=# 
white=# delete from yewu1.t3 where id > 10;
DELETE 90
white=# 
white=# select count(*) from yewu1.t3;
 count 
-------
    10
(1 row)

white=# 
white=# SELECT * FROM pg_dirtyread('yewu1.t3') as t(id int, name varchar(20));
 id  |   name   
-----+----------
   1 | haha_1
   2 | haha_2
   3 | haha_3
   4 | haha_4
   5 | haha_5
。。。省略。。。
  96 | haha_96
  97 | haha_97
  98 | haha_98
  99 | haha_99
 100 | haha_100
(100 rows)

white=# 
white=# 

查看autovacuum默认配置
在默认配置下,表发生较小的变化就会触发autovacuum,进而影响pg_dirtyread,减少了其可用性。

#autovacuum_work_mem = -1               # min 1MB, or -1 to use maintenance_work_mem
#log_autovacuum_min_duration = 10min    # log autovacuum activity;
#autovacuum = on                        # Enable autovacuum subprocess?  'on'
#autovacuum_max_workers = 3             # max number of autovacuum subprocesses
#autovacuum_naptime = 1min              # time between autovacuum runs
#autovacuum_vacuum_threshold = 50       # min number of row updates before
#autovacuum_vacuum_insert_threshold = 1000      # min number of row inserts
#autovacuum_analyze_threshold = 50      # min number of row updates before
#autovacuum_vacuum_scale_factor = 0.2   # fraction of table size before vacuum
#autovacuum_vacuum_insert_scale_factor = 0.2    # fraction of inserts over table
#autovacuum_analyze_scale_factor = 0.1  # fraction of table size before analyze
#autovacuum_freeze_max_age = 200000000  # maximum XID age before forced vacuum
#autovacuum_multixact_freeze_max_age = 400000000        # maximum multixact age
#autovacuum_vacuum_cost_delay = 2ms     # default vacuum cost delay for
                                        # autovacuum, in milliseconds;
#autovacuum_vacuum_cost_limit = -1      # default vacuum cost limit for
                                        # autovacuum, -1 means use

谨记:心存敬畏,行有所止。

http://www.dtcms.com/a/111669.html

相关文章:

  • 55.基于springboot+vue的汽车租赁管理系统
  • Flink CDC Pipeline mysql to doris
  • 关于JVM和OS中的指令重排以及JIT优化
  • 小刚说C语言刷题——第14讲 逻辑运算符
  • Jetpack Compose `ACTION_HOVER_EXIT` 事件异常解决方案
  • 纯个人整理,蓝桥杯使用的算法模板day2(0-1背包问题),手打个人理解注释,超全面,且均已验证成功(附带详细手写“模拟流程图”,全网首个
  • MySQL-SQL-DDL语句、表结构创建语句语法、表约束、表数据类型
  • Dive into Deep Learning - 2.4. Calculus (微积分)
  • Netty——连接超时 与 断开重连
  • Linux命令-grep
  • 人工智能爬虫导致维基共享资源带宽需求激增 50%
  • 计算机系统---GPU
  • 【小沐杂货铺】基于Three.JS绘制太阳系Solar System(GIS 、WebGL、vue、react)
  • centosububntu设置开机自启动
  • Upload-labs靶场通关
  • 06-31-自考数据结构(20331)- 查找技术-哈希表例题分析
  • 在CPU服务器上部署Ollama和Dify的过程记录
  • 批量图片文本识别重命名,批量ocr识别图片重命名,基于WPF和腾讯OCR云部署实,现批量对图片局部提取文字后重命名的操作详细步骤
  • PyTorch模型 train() 和 eval() 模式详解
  • Python 入门指南:从基础语法到应用场景
  • React-02初学hello_react(JSX,创建React根节点,引入对应React库,render渲染DOM)
  • MIT6.828 Lab3-3 Detect which pages have been accessed (hard)
  • MCP有哪些比较好的资源?
  • 数据一键导出为 Excel 文件
  • 每日一个小病毒(C++)EnumChildWindows+shellcode
  • 添加购物车功能
  • LeetCode热题100记录-【链表】
  • Linux内核物理内存组织结构
  • 【GPT入门】第33课 从应用场景出发,区分 TavilyAnswer 和 TavilySearchResults,代码实战
  • [每周一更]-(第138期):MySQL 子查询详解:原理、应用及优化方案