惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
月光博客
月光博客
S
Security Affairs
宝玉的分享
宝玉的分享
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
PCI Perspectives
PCI Perspectives
B
Blog RSS Feed
腾讯CDC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
T
The Blog of Author Tim Ferriss
H
Help Net Security
Vercel News
Vercel News
W
WeLiveSecurity
U
Unit 42
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
S
Schneier on Security
The Register - Security
The Register - Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
博客园 - Franky
H
Hacker News: Front Page
WordPress大学
WordPress大学
I
Intezer
M
MIT News - Artificial intelligence
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
V
V2EX
Help Net Security
Help Net Security
小众软件
小众软件
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs

博客园 - 过错

vscode的continue中配置第三方模型。以agnes-ai为例 在 Docker 容器中运行 .NET 6 并使用 libgdiplus(通常是为了支持 System.Drawing.Common 进行图片处理),你需要完成以下两个核心步骤 vs code调试netcore 为Ubuntu24生成netcore10的镜像, linux安装netcore nginx postgresql ssh 各种加速 docker加速 python的一些设置 netcore 发布命令 一些代码库 The database cluster initialisation failed but was not the same version as initdb的解决办法(postgresql) NDVI计算 ,c#和python代码实现 使用c#调用chatgpt 。以下代码由ai自动生成。 vs Commit message的使用 Fiddler 替换资源 docker 部署 rabbitmq(持久化) 和postgresql redis mysql 油猴子常用脚本 通过nginx做身份验证网关的方法 ngnix 常用配置 Nginx配置之实现多台服务器负载均衡 Nginx配置优化详解 ngnix代理grpc
不使用Debezium,记录PostgreSQL中的数据的数据前后变化
过错 · 2024-06-04 · via 博客园 - 过错

如果不使用Debezium,可以使用触发器(Trigger)来记录PostgreSQL中的数据的变化。触发器是一种特殊的存储过程,当对表执行INSERT、UPDATE或DELETE操作时,会自动执行触发器中的代码。

以下是一个简单的示例,展示了如何使用触发器记录数据的变化:

1 创建一个用于存储变化记录的表:

CREATE TABLE data_changes (
    id SERIAL PRIMARY KEY,
    operation CHAR(1),
    old_data JSONB,
    new_data JSONB,
    change_time TIMESTAMP NOT NULL DEFAULT NOW()
);

2 为需要监控变化的表创建触发器函数:

CREATE OR REPLACE FUNCTION record_data_changes() RETURNS TRIGGER AS $$
DECLARE
    old_data JSONB;
    new_data JSONB;
BEGIN
    IF (TG_OP = 'DELETE') THEN
        old_data = row_to_json(OLD);
        INSERT INTO data_changes (operation, old_data) VALUES ('D', old_data);
        RETURN OLD;
    ELSIF (TG_OP = 'UPDATE') THEN
        old_data = row_to_json(OLD);
        new_data = row_to_json(NEW);
        INSERT INTO data_changes (operation, old_data, new_data) VALUES ('U', old_data, new_data);
        RETURN NEW;
    ELSIF (TG_OP = 'INSERT') THEN
        new_data = row_to_json(NEW);
        INSERT INTO data_changes (operation, new_data) VALUES ('I', new_data);
        RETURN NEW;
    END IF;
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;

3 为需要监控变化的表创建触发器:

CREATE TRIGGER data_changes_trigger
AFTER INSERT OR UPDATE OR DELETE ON your_table
FOR EACH ROW EXECUTE FUNCTION record_data_changes();

此方法使用触发器,功能上能实现,但是性能有损失。

其他方法 可以使用 flink sql(推荐,需要flink。可能会需要使用java代码),Debezium (使用kafka,比较麻烦,老版本用的zk。新版可以不用zk)实现。