【问题标题】:Delete in Postgres citus columnar storage alternatives在 Postgres citus 列式存储替代方案中删除
【发布时间】:2022-10-24 20:09:40
【问题描述】:

我计划使用 citus 将系统日志存储最多 n 天,之后它们应该被删除。Citus 列式存储看起来是完美的数据库,直到我阅读 this 其中提到不能对柱状执行删除。 所以我的问题是在列式存储中实现删除的替代方法吗?

【问题讨论】:

    标签: postgresql postgresql-9.3 citus


    【解决方案1】:

    您可以暂时将表访问方法切换到行模式以删除或更新表。然后在操作后您可以切换回列访问方法。示例用法如下所示:

    -- create table and fill with generated data until 20 days before
    CREATE TABLE logs (
      id int not null,
      log_date timestamp
    );
    
    -- set access method columnar
    SELECT alter_table_set_access_method('logs', 'columnar');
    
    -- fill the table with generated data which goes until 20 days before
    INSERT INTO logs select i, now() - interval '1 hour' * i from generate_series(1,480) i;
        
    -- now you want to drop last 10 days data, you can switch to row access method temporarily to execute delete or updates
    SELECT alter_table_set_access_method('logs', 'heap');
    DELETE FROM logs WHERE log_date < (now() - interval '10 days');
    
    -- switch back to columnar access method
    SELECT alter_table_set_access_method('logs', 'columnar');
    

    日志归档的更好选择:我们正在创建源表的完整副本以拥有一个具有新访问方法的表。表越大,消耗的资源就越多。更好的选择是,如果您可以将日志表划分为天或月的分区,您只需要更改单个分区的访问方法。请注意,您应该分别为每个分区设置访问方法。 Columnar 目前不支持直接设置分区表的访问方式。

    学到更多:

    【讨论】:

    • 感谢 Aykut 的快速响应,听起来不错,但您能判断 alter_table_set_access_method 是否占用 CPU 和 io
    • 我们正在创建源表的完整副本以拥有一个具有新访问方法的表。表越大,消耗的资源就越多。更好的选择是,如果您可以将日志表划分为天或月的分区,您只需要更改单个分区的访问方法。请注意,您应该分别为每个分区设置访问方法。 Columnar 目前不支持直接设置分区表的访问方式。我编辑了答案,以便您可以访问附加链接以获取有关柱状的更多信息。
    • 感谢您花时间回答我的问题。现在一切都很清楚了。
    猜你喜欢
    • 2023-03-26
    • 2020-08-16
    • 2014-03-01
    • 2014-07-30
    • 2013-11-24
    • 2023-04-04
    • 2021-10-03
    • 2021-09-28
    相关资源
    最近更新 更多