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

推荐订阅源

月光博客
月光博客
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
D
DataBreaches.Net
B
Blog RSS Feed
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
Netflix TechBlog - Medium
O
OpenAI News
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
Google Online Security Blog
Google Online Security Blog
SecWiki News
SecWiki News
H
Help Net Security
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
腾讯CDC
B
Blog
H
Heimdal Security Blog
博客园 - 三生石上(FineUI控件)
C
CERT Recently Published Vulnerability Notes
L
Lohrmann on Cybersecurity
G
Google Developers Blog
Scott Helme
Scott Helme
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
V
Visual Studio Blog
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
C
Cisco Blogs
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
S
Security Affairs
P
Privacy & Cybersecurity Law Blog
S
Secure Thoughts
V
V2EX
雷峰网
雷峰网
Security Latest
Security Latest
H
Hacker News: Front Page
TaoSecurity Blog
TaoSecurity Blog
M
MIT News - Artificial intelligence

博客园 - 过错

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)实现。