移除时间范围完全早于(或晚于)指定时间的数据块。显示已删除数据块的列表,样式与 show_chunks 函数相同。

数据块受开始时间和结束时间约束,并且开始时间始终在结束时间之前。如果数据块的结束时间早于 older_than 时间戳,或者如果给定了 newer_than,则其开始时间晚于 newer_than 时间戳,则会删除该数据块。

请注意,由于仅当数据块的时间范围完全早于(或晚于)指定时间戳时才删除数据块,因此剩余数据可能仍包含早于(或晚于)指定时间戳的时间戳。

数据块只能根据其时间间隔删除。它们不能根据哈希分区删除。

名称类型描述
relationREGCLASS从中删除数据块的超表或连续聚合。
名称类型描述
older_thanANY截止点的规范,任何早于此时间戳的数据块都应删除。
newer_thanANY截止点的规范,任何晚于此时间戳的数据块都应删除。
verboseBOOLEAN设置为 true 时,显示有关重新排序命令进度的消息。默认为 false。
created_beforeANY截止点的规范,任何在此时间戳之前创建的数据块都应删除。
created_afterANY截止点的规范,任何在此时间戳之后创建的数据块都应删除。

older_thannewer_than 参数可以通过两种方式指定

  • interval 类型: 截止点计算为 now() - older_than,类似地 now() - newer_than。如果提供了 INTERVAL,并且时间列不是 TIMESTAMPTIMESTAMPTZDATE 之一,则返回错误。

  • timestamp、date 或 integer 类型: 截止点显式给定为 TIMESTAMP / TIMESTAMPTZ / DATESMALLINT / INT / BIGINT。时间戳或整数的选择必须遵循超表的时间列类型。

created_beforecreated_after 参数可以通过两种方式指定

  • interval 类型: 截止点计算为 now() - created_before,类似地 now() - created_after。这使用相对于当前时间的块创建时间进行过滤。

  • timestamp、date 或 integer 类型: 截止点显式给定为 TIMESTAMP / TIMESTAMPTZ / DATESMALLINT / INT / BIGINT。整数值的选择必须遵循超表的分区列类型。否则,将使用块创建时间进行过滤。

警告

当仅使用 interval 类型时,该函数假定您正在删除过去的内容。如果想要删除未来的数据,例如删除错误条目,请使用时间戳。

当同时使用 older_thannewer_than 参数时,该函数返回两个结果范围的交集。例如,指定 newer_than => 4 monthsolder_than => 3 months 会删除所有介于 3 到 4 个月之间的数据块。同样,指定 newer_than => '2017-01-01'older_than => '2017-02-01' 会删除 '2017-01-01' 和 '2017-02-01' 之间的所有数据块。指定不会导致两个范围之间重叠交集的参数会导致错误。

当同时使用 created_beforecreated_after 参数时,该函数返回两个结果范围的交集。例如,指定 created_after => 4 monthscreated_before=> 3 months 会删除所有从现在起 3 到 4 个月之间创建的数据块。同样,指定 created_after=> '2017-01-01'created_before => '2017-02-01' 会删除 '2017-01-01' 和 '2017-02-01' 之间创建的所有数据块。指定不会导致两个范围之间重叠交集的参数会导致错误。

注意

created_before/created_after 参数不能与 older_than/newer_than 一起使用。

删除超表 conditions 中早于 3 个月的所有数据块

SELECT drop_chunks('conditions', INTERVAL '3 months');

示例输出

drop_chunks
----------------------------------------
_timescaledb_internal._hyper_3_5_chunk
_timescaledb_internal._hyper_3_6_chunk
_timescaledb_internal._hyper_3_7_chunk
_timescaledb_internal._hyper_3_8_chunk
_timescaledb_internal._hyper_3_9_chunk
(5 rows)

删除超表 conditions 中在 3 个月之前创建的所有数据块

SELECT drop_chunks('conditions', created_before => now() - INTERVAL '3 months');

删除超表 conditions 中未来 3 个月以上的所有数据块。这对于纠正使用不正确时钟摄取的数据非常有用

SELECT drop_chunks('conditions', newer_than => now() + interval '3 months');

删除超表 conditions 中 2017 年之前的所有数据块

SELECT drop_chunks('conditions', '2017-01-01'::date);

删除超表 conditions 中 2017 年之前的所有数据块,其中时间列以自 UNIX 纪元以来的毫秒为单位给出

SELECT drop_chunks('conditions', 1483228800000);

删除超表 conditions 中早于 3 个月前且晚于 4 个月前的所有数据块

SELECT drop_chunks('conditions', older_than => INTERVAL '3 months', newer_than => INTERVAL '4 months')

删除超表 conditions 中在 3 个月前和 4 个月前创建的所有数据块

SELECT drop_chunks('conditions', created_before => INTERVAL '3 months', created_after => INTERVAL '4 months')

删除所有超表中早于 3 个月前的所有数据块

SELECT drop_chunks(format('%I.%I', hypertable_schema, hypertable_name)::regclass, INTERVAL '3 months')
FROM timescaledb_information.hypertables;

关键词

在此页面上发现问题?报告问题 或 在 GitHub 上编辑此页