没有内置的到期功能,但是如果您的目标是自动使字段到期并在数据库中包含逻辑(因此没有像cron作业那样的外部依赖项),则可以随时编写触发器。下面是一个触发器示例,该触发器从时间戳中删除早于1分钟的表中的行。每当在同一表中插入新行时,就会执行该命令。您显然可以将触发器设置为在其他条件下并根据需要在各种到期日期执行。我以以下网站为基础:http : //www.the-art-of-web.com/sql/trigger-delete-old/
CREATE TABLE expire_table (
timestamp timestamp NOT NULL DEFAULT NOW(),
name TEXT NOT NULL
);
INSERT INTO expire_table (name) VALUES ('a');
INSERT INTO expire_table (name) VALUES ('b');
INSERT INTO expire_table (name) VALUES ('c');
select * from expire_table;
timestamp | name
----------------------------+------
2014-09-26 15:33:43.243356 | a
2014-09-26 15:33:45.222202 | b
2014-09-26 15:33:47.347131 | c
(3 rows)
CREATE FUNCTION expire_table_delete_old_rows() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM expire_table WHERE timestamp < NOW() - INTERVAL '1 minute';
RETURN NEW;
END;
$$;
CREATE TRIGGER expire_table_delete_old_rows_trigger
AFTER INSERT ON expire_table
EXECUTE PROCEDURE expire_table_delete_old_rows();
INSERT INTO expire_table (name) VALUES ('d');
select * from expire_table;
timestamp | name
----------------------------+------
2014-09-26 15:36:56.132596 | d
(1 row)