如何使用Postgres触发器存储更改

时间:2009-08-18 18:53:06

标签: postgresql logging triggers

是否可以使用PostgreSQL中的触发器创建更新并插入已发生在表中的更改的sql语句,并将它们记录到文件中以便以后执行。

这只是暂时使用,所以只是快速而肮脏的事情。

5 个答案:

答案 0 :(得分:30)

来自的审计触发器示例 https://www.postgresql.org/docs/current/static/plpgsql-trigger.html

CREATE TABLE emp (
    empname           text NOT NULL,
    salary            integer
);

CREATE TABLE emp_audit(
    operation         char(1)   NOT NULL,
    stamp             timestamp NOT NULL,
    userid            text      NOT NULL,
    empname           text      NOT NULL,
    salary integer
);

CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$
    BEGIN
        --
        -- Create a row in emp_audit to reflect the operation performed on emp,
        -- make use of the special variable TG_OP to work out the operation.
        --
        IF (TG_OP = 'DELETE') THEN
            INSERT INTO emp_audit SELECT 'D', now(), user, OLD.*;
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN
            INSERT INTO emp_audit SELECT 'U', now(), user, NEW.*;
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
            INSERT INTO emp_audit SELECT 'I', now(), user, NEW.*;
            RETURN NEW;
        END IF;
        RETURN NULL; -- result is ignored since this is an AFTER trigger
    END;
$emp_audit$ LANGUAGE plpgsql;

CREATE TRIGGER emp_audit
AFTER INSERT OR UPDATE OR DELETE ON emp
    FOR EACH ROW EXECUTE PROCEDURE process_emp_audit();

答案 1 :(得分:8)

您确实需要存储在表中的查询的审核日志吗?获取包含所有已执行查询的文件的最简单方法是使用postgresql的内置日志记录。

在postgresql.conf中(通常在$ PG_DATA目录中),适当地设置以下选项:

log_directory '/path/to/log/dir'
log_filename = 'filename.log'
log_statement = 'mod'

最后一个选项使它记录所有INSERT,UPDATE,DELETE,TRUNCATE和COPY FROM语句。

Postgres文档中的更多详细信息:http://www.postgresql.org/docs/current/static/runtime-config-logging.html

答案 2 :(得分:4)

以下链接应指向正确的方向。

http://developer.postgresql.org/pgdocs/postgres/sql-createtrigger.html

根据您的目的,最好打开日志记录。

答案 3 :(得分:2)

PostgreSQL表日志作者:Andreas Scherbaum是一个Postgresql扩展,它使用触发器将特定表上的任何INSERT,UPDATE和DELETE记录到另一个表中。

用法很简单:您创建第二个表格,其格式与您想要关注的表格相同。此外,您还需要一些额外的列来维护记录的数据。

tablelog的第二部分能够在过去的任何时间恢复原始表或特定行的状态。

我自己没有尝试过,但它应该有效。

还有关于tablelog的讨论中的幻灯片,但由于一些stackoverflow反垃圾邮件怪异,我无法在此发布正确的链接:)(http:// andreas.scherbaum.la/writings/tablelog.pdf)。

http://pgfoundry.org/projects/tablelog/

http:// andreas.scherbaum.la/blog/archives/100-Log-Table-Changes-in-PostgreSQL-with-tablelog.html

答案 4 :(得分:2)

这是一个非常常见的要求,因此以下答案基于this article on my blog

SQL日志

如果仅对执行的语句感兴趣,则只需激活PostgreSQL语句日志。

为此,打开postgresql.conf文件并设置以下配置属性:

log_statement = 'all' 

随后,您将在以下路径下看到SQL语句记录在文件中:

$PG_DATA/pg_log/postgresql-YYYY-MM-DD_HHMMSS.log

但是,如果要记录行级更改,则需要一种可以使用触发器实现的审核日志记录机制,如下所示。

数据库表

让我们考虑一下以下数据库表:

The book and book_audit_log tables

book_audit_log将存储book表中发生的所有更改。

book_audit_log的创建方式如下:

CREATE TABLE IF NOT EXISTS book_audit_log (
    book_id bigint NOT NULL,
    old_row_data jsonb,
    new_row_data jsonb,
    dml_type dml_type NOT NULL,
    dml_timestamp timestamp NOT NULL,
    dml_created_by varchar(255) NOT NULL,
    PRIMARY KEY (book_id, dml_type, dml_timestamp)
)

book_id列存储当前执行中的DML语句插入,更新或删除的关联书表记录的标识符。

old_row_datanew_row_data列为JSONB类型,它们将捕获当前INSERT,UPDATE或DELETE语句执行前后的书行状态。

dml_type列存储当前正在执行的DML语句的类型(例如INSERT,UPDATE和DELETE)。 dml_type类型是PostgreSQL枚举类型,其创建方式如下:

CREATE TYPE dml_type AS ENUM ('INSERT', 'UPDATE', 'DELETE')

dml_timestamp列存储当前时间戳。

dml_created_by列存储生成当前INSERT,UPDATE或DELETE DML语句的应用程序用户。

PostgreSQL审核日志记录触发器

要捕获book表上的INSERT,UPDATE和DELETE DML语句,我们需要创建一个触发函数,其外观如下:

CREATE OR REPLACE FUNCTION book_audit_trigger_func()
RETURNS trigger AS $body$
BEGIN
   if (TG_OP = 'INSERT') then
       INSERT INTO book_audit_log (
           book_id,
           old_row_data,
           new_row_data,
           dml_type,
           dml_timestamp,
           dml_created_by
       )
       VALUES(
           NEW.id,
           null,
           to_jsonb(NEW),
           'INSERT',
           CURRENT_TIMESTAMP,
           current_setting('var.logged_user')
       );
             
       RETURN NEW;
   elsif (TG_OP = 'UPDATE') then
       INSERT INTO book_audit_log (
           book_id,
           old_row_data,
           new_row_data,
           dml_type,
           dml_timestamp,
           dml_created_by
       )
       VALUES(
           NEW.id,
           to_jsonb(OLD),
           to_jsonb(NEW),
           'UPDATE',
           CURRENT_TIMESTAMP,
           current_setting('var.logged_user')
       );
             
       RETURN NEW;
   elsif (TG_OP = 'DELETE') then
       INSERT INTO book_audit_log (
           book_id,
           old_row_data,
           new_row_data,
           dml_type,
           dml_timestamp,
           dml_created_by
       )
       VALUES(
           OLD.id,
           to_jsonb(OLD),
           null,
           'DELETE',
           CURRENT_TIMESTAMP,
           current_setting('var.logged_user')
       );
        
       RETURN OLD;
   end if;
     
END;
$body$
LANGUAGE plpgsql;

为了在插入,更新或删除书表记录后执行book_audit_trigger_func函数,我们必须定义以下触发器:

CREATE TRIGGER book_audit_trigger
AFTER INSERT OR UPDATE OR DELETE ON book
FOR EACH ROW EXECUTE FUNCTION book_audit_trigger_func();

dml_created_by列设置为var.logged_user PostgreSQL会话变量的值,该变量先前由应用程序使用当前登录的用户设置,如下所示:

SET LOCAL var.logged_user = 'Vlad Mihalcea'

测试时间

book表上执行INSERT语句时:

INSERT INTO book (
    id,
    author, 
    price_in_cents, 
    publisher, 
    title
) 
VALUES (
    1,
    'Vlad Mihalcea', 
    3990, 
    'Amazon', 
    'High-Performance Java Persistence 1st edition'
)

我们可以看到book_audit_log中插入了一条记录,该记录捕获了刚刚在book表上执行的INSERT语句:

| book_id | old_row_data | new_row_data                                                                                                                                  | dml_type | dml_timestamp              | dml_created_by |
|---------|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------|----------|----------------------------|----------------|
| 1       |              | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 3990} | INSERT   | 2020-08-25 13:19:57.073026 | Vlad Mihalcea  |

在更新book表行时:

UPDATE book 
SET price_in_cents = 4499 
WHERE id = 1

我们可以看到book_audit_log会将新记录添加到book_audit_trigger

| book_id | old_row_data                                                                                                                                  | new_row_data                                                                                                                                  | dml_type | dml_timestamp              | dml_created_by |
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|----------|----------------------------|----------------|
| 1       |                                                                                                                                               | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 3990} | INSERT   | 2020-08-25 13:19:57.073026 | Vlad Mihalcea  |
| 1       | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 3990} | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 4499} | UPDATE   | 2020-08-25 13:21:15.006365 | Vlad Mihalcea  |

删除book表行时:

DELETE FROM book 
WHERE id = 1

新记录由book_audit_log添加到book_audit_trigger

| book_id | old_row_data                                                                                                                                  | new_row_data                                                                                                                                  | dml_type | dml_timestamp              | dml_created_by |
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|----------|----------------------------|----------------|
| 1       |                                                                                                                                               | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 3990} | INSERT   | 2020-08-25 13:19:57.073026 | Vlad Mihalcea  |
| 1       | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 3990} | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 4499} | UPDATE   | 2020-08-25 13:21:15.006365 | Vlad Mihalcea  |
| 1       | {"id": 1, "title": "High-Performance Java Persistence 1st edition", "author": "Vlad Mihalcea", "publisher": "Amazon", "price_in_cents": 4499} |                                                                                                                                               | DELETE   | 2020-08-25 13:21:58.499881 | Vlad Mihalcea  |