在 Postgres 中,我想存储表的上次更新/插入时间。 Microsoft SQL Server 提供了一种由数据库自动维护的类型
timestamp
。
但是
timestamp
在 Postgres 中的工作方式不同,它不会自动更新并且列始终为空。
在postgresql中,你必须使用触发器。您可以点击此链接了解如何操作https://x-team.com/blog/automatic-timestamps-with-postgresql/ .
要总结本文,您可以执行以下操作:
创建将被触发的Pl/Pgsql函数:
CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
创建您的表格
CREATE TABLE mytable (
id SERIAL NOT NULL PRIMARY KEY,
content TEXT,
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
最后添加触发器:
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
希望对你有帮助。
扩展 SofienM 的精彩答案(以及 lolung 的评论更正),我们还可以通过自动更新具有行创建者的用户名(角色)的列,并使用 最后一个要修改的用户的用户名更新另一列来扩展此功能现有行。以下是您可以运行的完整测试(我使用两个虚构用户(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”列......)。