zoukankan      html  css  js  c++  java
  • 使用PostgreSQL存储时序数据

    操作系统 :CentOS7.3.1611_x64

    PostgreSQL版本 :9.6

    问题描述

    在InfluxDB中存储时序数据时,当tag值和时间戳都相同时会执行覆盖操作。在PostgreSQL中能不能这么用呢?

    解决方案

    可以借助唯一索引和update来实现,这里记录下以备后用。

    1、创建带有唯一索引的表,比如:

    drop table if exists stock_data;
    create table stock_data (
        id      bigserial primary key,
        stock_id varchar(32),
        trans_date date,
        open_price decimal,
        close_price decimal
    );
    
    create unique index stock_idx on stock_data(stock_id,trans_date);

    这里创建一个stock_data表,并创建唯一索引stock_idx。

    2、写入数据

    insert into stock_data (stock_id,trans_date,open_price,close_price) values ('sh000001',date '19901219',96.05,99.98);

    但上述代码第二次执行时会报错,可以通过如下方式解决这个问题并实现数据的写入:

    insert into stock_data (stock_id,trans_date,open_price,close_price) values ('sh000001',date '19901219',196.05,199.98)
    on conflict(stock_id,trans_date) do update set open_price=excluded.open_price,close_price=excluded.close_price;

    好,就这些了,希望对你有帮助。

    本文github地址:

    https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20171118_使用PostgreSQL存储时序数据.rst

    欢迎补充

  • 相关阅读:
    盛大自动化运维
    Redis used_cpu_sys used_cpu_user meaning (redis info中cpu信息的含义)
    redis info 详解
    htop详解
    线程问题排查思路
    网络协议基础 -- 东哥
    线程通讯
    进程
    day14
    day13
  • 原文地址:https://www.cnblogs.com/MikeZhang/p/PostgresSQLTimeSeriesData20171118.html
Copyright © 2011-2022 走看看