K.N*_*zyk 4 postgresql cascade relational-database
我继承了一个相当大的 postgreql 数据库。我们有一个每月运行的作业,用于备份现有数据库并使用我们收到的更新的供应商数据创建一个新数据库。
目前有一个小问题。在不深入了解表设置、数据建模内容等细节的情况下,我相信它可以通过简单的删除查询来修复,因为表设置为使用级联删除。
然而,从供应商提供的源生成此数据库大约需要 9 个小时,因此我总是犹豫是否要引入新的更改。我目前有该数据库的副本,我打算首先在其上运行查询,以使用户“可以”成功运行。然而,一般来说,sql 的缺点之一是,当进行删除时,输出通常只有以下几行:
查询成功返回:x 行受影响,y 毫秒执行时间。
postgres 有没有办法确定使用级联删除时从哪些表中删除了哪些行?我想在我的副本上运行查询,看看所删除的内容是否是我所期望的,至少是在哪些表被命中时。这可能吗?
Is there a way in postgres to determine what rows were removed from which tables when using cascading deletes?
Unfortunately no simple built-in way, but it's a great idea. Since cascading deletes are implemented under the hood by triggers, all you'd have to do is modify the referential integrity triggers so that cascade deletes raise a LOG level message with with the row information before deleting it. This would require changes to the PostgreSQL source code and a recompile.
Alternately, you can log all deletes by creating a new AFTER DELETE ... FOR EACH ROW trigger on each table referred to by a cascade constraint. It can be as simple as:
CREATE OR REPLACE FUNCTION log_delete() RETURNS trigger AS $$
BEGIN
RAISE LOG 'Deleting row % (statement is %)', OLD, current_query();
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
这样您就不需要修改数据库,尽管您确实需要在每个需要级联删除的表上安装触发器。
为了获得奖励积分,您甚至可以通过查询外键关系来自动创建它information_schema,但这可能比它的价值更麻烦。
| 归档时间: |
|
| 查看次数: |
2644 次 |
| 最近记录: |