news 2026/4/29 2:43:20

DATA_CHECKSUMS

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
DATA_CHECKSUMS

data_checksums 介绍

由于存储介质故障,突然断电等问题,导致数据块中存储的信息丢失,或由于没来得及写入完成导致的块断裂,坏块通常会带来数据损坏甚至数据丢失的风险,为了保证数据的完整性,通常需要对数据块进行校验和。

data_checksums 如何工作:

  • checksum 校验发生在数据页落盘之前和进入 sharedbuffer 之前。
  • 当将页面从缓冲区缓存写入到操作系统页面缓存时,就会计算 checksum 值并写入到数据页中。
  • 每次读取该块时,checksum 值就会重新计算,并与所存储的 checksum 值进行比较,这可以检验数据是否损坏。

如何开启data_checksums

查看是否有启用

select * from pg_catalog.pg_settings wherenamelike'%data_checksums%';

或者

[postgres@192 root]$ pg_controldata |grep checksum

data_checksum 默认是关闭,当你调⽤initdb程序时, 添加‑‑data‑checksums标志(如果你更喜欢短参数,则添加‑k),你崭新的Postgres集群将启⽤数据校验和。如果一开始初始化没有指定,后续也可以通过 po_checksum 进行启用。

Postgres的第12 版引⼊了⼀个新的内置程序pg_checksum,它可以在现有的⽆校验和数据库上启⽤校验和。

[postgres@192 root]$ pg_checksums --help pg_checksums enables, disables, or verifies data checksums in a PostgreSQL database cluster. Usage: pg_checksums [OPTION]... [DATADIR] Options: [-D, --pgdata=]DATADIR data directory -c, --check check data checksums (default) -d, --disable disable data checksums -e, --enable enable data checksums -f, --filenode=FILENODE check only relation with specified filenode -N, --no-sync do not wait for changes to be written safely to disk -P, --progress show progress information --sync-method=METHOD set method for syncing files to disk -v, --verbose output verbose messages -V, --version output version information, then exit -?, --help show this help, then exit If no data directory (DATADIR) is specified, the environment variable PGDATA is used. Report bugs to <pgsql-bugs@lists.postgresql.org>. PostgreSQL home page: <https://www.postgresql.org/> data_checksums启用 pg_checksums -D $PGDATA --enable --progress #启用校验和时,所有数据块都能产生校验和信息并写入到数据库 data_checksums禁用 pg_checksums -D $PGDATA --disable --progress #禁用校验和只会更新pg_control文件,读写数据块操作时将不进行校验 不管是启用或者禁用,都要在数据库关闭后进行

注入式攻击-嫁接式数据块

data_checksums 在未启用的环境下容易被非正常 insert 注入,下面我们来做个实验看一下:

#新建两张新表,表结构要一致 postgres=# create table t1 (a int); CREATE TABLE postgres=# insert into t1 values(1); INSERT 0 1 postgres=# create table t2 (a int); CREATE TABLE postgres=# insert into t2 values(1111); INSERT 0 1 #查看表的位置 postgres=# select pg_relation_filepath('t1'); pg_relation_filepath ---------------------- base/5/16426 (1 row) postgres=# select pg_relation_filepath('t2'); pg_relation_filepath ---------------------- base/5/16429 (1 row) [root@192 5]# dd bs=8192 count=1 seek=1 of=16426 if=16429 1+0 records in 1+0 records out 8192 bytes (8.2 kB, 8.0 KiB) copied, 0.000207962 s, 39.4 MB/s [postgres@192 root]$ pg_ctl stop [postgres@192 root]$ pg_ctl start postgres=# select * from t1; a ------ 1 1111 (2 rows)

如果这时候开启 data_checksum,会发现一条报错信息,因为有一个块是我们非法插入的。

pg_checksums: error: could not read block 1 in file "/data/pgsql/data/base/5/16429": read 1 of 8192

如何读取坏块里的数据

data_checksums 启用环境下操作。

忽略 checksum 报错,读出坏块中的数据

set ignore_checksum_failure=on;

注意:ignore_checksum_failure 该参数仅在开启 checksum 下生效,作用就是忽略 checksum 的报错,读出坏块中的数据。但是假如 page header 错误,要么就读不出来,事务依旧会终止

set zero_damaged_pages=on;

zero_damaged_pages 这个参数会导致数据块会在内存中将损坏的 page 都置为空,然后获取剩下的结果,即使 page_header 损坏,执行事务也不会退出,只会报 warning。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/29 2:39:08

网页视频资源捕获:如何突破技术限制实现视频自主下载

网页视频资源捕获&#xff1a;如何突破技术限制实现视频自主下载 【免费下载链接】VideoDownloadHelper Chrome Extension to Help Download Video for Some Video Sites. 项目地址: https://gitcode.com/gh_mirrors/vi/VideoDownloadHelper VideoDownloadHelper是一款专…

作者头像 李华
网站建设 2026/4/29 2:38:31

如何用ImageToSTL将图片转换为3D打印模型:5分钟快速指南

如何用ImageToSTL将图片转换为3D打印模型&#xff1a;5分钟快速指南 【免费下载链接】ImageToSTL This tool allows you to easily convert any image into a 3D print-ready STL model. The surface of the model will display the image when illuminated from the left side…

作者头像 李华
网站建设 2026/4/29 2:37:21

银发经济新解法:魔珐星云 SDK 构建低延迟、高温情养老陪伴智能大屏

前言 2026 年我国 60 岁以上人口突破 3 亿&#xff0c;正式进入中度老龄化社会&#xff0c;万亿级银发经济赛道迎来爆发。但当前多数适老化智能设备&#xff0c;仅停留在放大字体、提高音量等表面优化&#xff0c;既没有真正跨越老年人的数字使用鸿沟&#xff0c;也无法弥补空巢…

作者头像 李华
网站建设 2026/4/29 2:33:51

5步掌握缠论可视化:自动化你的技术分析工作流

5步掌握缠论可视化&#xff1a;自动化你的技术分析工作流 【免费下载链接】Indicator 通达信缠论可视化分析插件 项目地址: https://gitcode.com/gh_mirrors/ind/Indicator 还在为手工分析复杂的缠论结构而烦恼吗&#xff1f;面对K线图上密密麻麻的价格波动&#xff0c;…

作者头像 李华