用于捕获 table 数据以在 postgres 中进行审计的通用触发器
generic trigger to capture table data for audit in postgres
需要帮助创建一个通用触发器来记录所有 tables。
我有 table 命名为 "system" 并且需要记录它。
日志 table 名称 system_audit 是用 "system" table 的所有列以及名为 table 的另外三列创建的
modified_dt、modified_by 和 modified_type.
modified_dt 将是 current_timestamp
modified_by 将是用户
和modified_type指定它是插入,更新还是删除。(需要为insert/update捕获新数据,为删除删除旧数据)
如何编写函数来捕获上述数据。它还需要是动态的,这样我就可以在我的架构
中的所有 table 中使用它
注意:所有审核 table 都包含强制性的 modified_dt、modified_by 和 modified_type。
我从网上得到了一些代码,但是没有用,我以前用过 oracle,刚接触 postgres,不知道如何编码 properly.Please 帮助
我已经设法创建了一个通用函数及其工作方式fine.Thanks以寻求帮助。
create or replace function audit.fn__audit()
returns trigger as
$func$
declare
col_name text:='';
audit_table_name text := TG_TABLE_NAME || '_audit';
begin
if TG_OP = 'UPDATE' or TG_OP = 'INSERT' THEN
EXECUTE format('INSERT INTO audit.%1$I SELECT ().*,current_timestamp,user,'''||TG_OP||'''',audit_table_name) using NEW;
else
EXECUTE format('INSERT INTO audit.%1$I SELECT ().*,current_timestamp,user,'''||TG_OP||'''',audit_table_name) using old;
end if;
return new;
END $func$
LANGUAGE plpgsql VOLATILE;
需要帮助创建一个通用触发器来记录所有 tables。
我有 table 命名为 "system" 并且需要记录它。
日志 table 名称 system_audit 是用 "system" table 的所有列以及名为 table 的另外三列创建的 modified_dt、modified_by 和 modified_type.
modified_dt 将是 current_timestamp modified_by 将是用户 和modified_type指定它是插入,更新还是删除。(需要为insert/update捕获新数据,为删除删除旧数据)
如何编写函数来捕获上述数据。它还需要是动态的,这样我就可以在我的架构
中的所有 table 中使用它注意:所有审核 table 都包含强制性的 modified_dt、modified_by 和 modified_type。
我从网上得到了一些代码,但是没有用,我以前用过 oracle,刚接触 postgres,不知道如何编码 properly.Please 帮助
我已经设法创建了一个通用函数及其工作方式fine.Thanks以寻求帮助。
create or replace function audit.fn__audit()
returns trigger as
$func$
declare
col_name text:='';
audit_table_name text := TG_TABLE_NAME || '_audit';
begin
if TG_OP = 'UPDATE' or TG_OP = 'INSERT' THEN
EXECUTE format('INSERT INTO audit.%1$I SELECT ().*,current_timestamp,user,'''||TG_OP||'''',audit_table_name) using NEW;
else
EXECUTE format('INSERT INTO audit.%1$I SELECT ().*,current_timestamp,user,'''||TG_OP||'''',audit_table_name) using old;
end if;
return new;
END $func$
LANGUAGE plpgsql VOLATILE;