Skip to content

Latest commit

 

History

History
178 lines (64 loc) · 3.7 KB

20170612_03.md

File metadata and controls

178 lines (64 loc) · 3.7 KB

数据保留时间窗口的使用

作者

digoal

日期

2017-06-12

标签

PostgreSQL , 保留窗口 , ttl , stream , continuous view , pipelinedb


背景

类似mongodb的rotate collate(设置表的上限容量、上限记录数,持续写入,自动覆盖最老的记录),PostgreSQL通过pipelinedb也能实现类似的功能。

此类功能非常适合日志数据,无需维护成本,持续写入,自动覆盖最老的记录。

pipelinedb计划会在2017-07月份转换为postgresql插件,届时使用会更加便利。

如果不使用pipelinedb的cv ttl功能,直接使用postgresql的话,可以使用PostgreSQL 10的worker process,设置删除任务,自动调度,删除记录,效果也是一样的。

pipelinedb cv ttl例子

创建一个流(定义数据的格式)

CREATE STREAM s1 (id int, info text, crt_time timestamp default now());  

创建cv ttl,例如根据crt_time字段,保留1分钟。

CREATE CONTINUOUS VIEW cv1 WITH (ttl = '1 min', ttl_column = 'crt_time') AS  
  SELECT id,info,crt_time FROM s1;  

激活cv

activate cv1;  

测试

写入数据

pipeline=# insert into s1 values (1,'test',now());  
INSERT 0 1  

查看数据

pipeline=# select * from cv1;  
 id | info |          crt_time            
----+------+----------------------------  
  1 | test | 2017-06-12 17:11:45.774904  
(1 row)  

1分钟后查看数据,自动被删除。

修改ttl,保留10秒

pipeline=# select set_ttl('cv1', '10 second', 'crt_time');  
 set_ttl   
---------  
 (10,3)  
(1 row)  

重新测试,数据写入10秒后,自动删除。

参考

http://docs.pipelinedb.com/continuous-views.html#time-to-live-ttl-expiration

《PostgreSQL 10.0 preview 功能增强 - 后台运行(pg_background)》

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

digoal's wechat