跟踪Postgres中一行的最后修改时间戳

时间:2018-09-20 13:47:05

标签: postgresql timestamp

在Postgres中,我想存储表的最后更新/插入时间。 Microsoft SQL Server提供类型timestamp,该类型由数据库自动维护。

但是Postgres中的timestamp的工作方式有所不同,它不会自动更新,并且该列始终为空。

2 个答案:

答案 0 :(得分:2)

在postgresql中,您必须使用触发器。您可以按以下链接操作https://x-team.com/blog/automatic-timestamps-with-postgresql/

要总结这篇文章,您可以执行以下操作:

  1. 创建将要触发的Pl / Pgsql函数:

    CREATE OR REPLACE FUNCTION trigger_set_timestamp()
    RETURNS TRIGGER AS $$
    BEGIN
      NEW.updated_at = NOW();
      RETURN NEW;
    END;
    $$ LANGUAGE plpgsql;
    
  2. 创建表格

    CREATE TABLE mytable (
      id SERIAL NOT NULL PRIMARY KEY,
      content TEXT,
      updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
    );
    
  3. 最后添加触发器:

    CREATE TRIGGER set_timestamp
    BEFORE UPDATE ON mytable
    FOR EACH ROW
    EXECUTE PROCEDURE trigger_set_timestamp();
    

您可以在此处找到有关问题的更多信息:https://dba.stackexchange.com/questions/58214/getting-last-modification-date-of-a-postgresql-database-table

希望它会为您提供帮助。

答案 1 :(得分:0)

扩展 SofienM 的精彩回答(和 lolung 的评论更正),我们还可以通过自动更新具有行创建者的用户名(角色)的列,并使用 the 的用户名更新另一列来扩展它最后一个用户修改现有行。下面是一个可以运行的完整测试(我使用两个假想用户(user1 和 user2)来演示测试):

--create dummy table, execute as user1
CREATE TABLE public.test_table (
  id SERIAL PRIMARY KEY,
  comments VARCHAR(250),
  create_by VARCHAR(250) DEFAULT user,
  create_dat TIMESTAMPTZ DEFAULT now(),
  modify_by VARCHAR(250) DEFAULT user,
  modify_dat TIMESTAMPTZ DEFAULT NOW()
);

--create function trigger to change a timestamp value upon an update
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
  NEW.modify_dat = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

--create a trigger to execute the function
CREATE TRIGGER set_timestamp
BEFORE UPDATE ON public.test_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

--test the created by and created date columns, execute this as user1
INSERT INTO public.test_table (comments)
VALUES ('hello world'),
       ('hello system'),
       ('hello universe')
;

--wait a minute or two, execute this query as user1 and observe 'modify_dat' column
UPDATE public.test_table
SET comments = 'hello secret'
WHERE comments = 'hello world';

--create function trigger to change a username value upon an update
CREATE OR REPLACE FUNCTION trigger_set_usertimestamp()
RETURNS TRIGGER AS $$
BEGIN
  NEW.modify_by = user;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

--create a trigger to execute the function
CREATE TRIGGER set_usertimestamp
BEFORE UPDATE ON public.test_table
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_usertimestamp();

--test the created by and created date columns, execute this as user2 for variation
INSERT INTO public.test_table (comments)
VALUES ('goodbye world'),
       ('goodbye system'),
       ('goodbye universe')
;

--wait a minute or two, execute this query as user2 and observe 'modify_by' column
UPDATE public.test_table
SET comments = 'hello unknown'
WHERE comments = 'hello system';

改进这一点的是让触发器应用于给定模式中的任何/每个表,而不仅仅是一个特定的表(当然,假设每个表都有 'modify_dat' 和 'modify_by' 列......)。