Postgres date overlapping constraint

前端 未结 4 1133
鱼传尺愫
鱼传尺愫 2020-12-14 08:22

I have a table like this:

date_start    date_end     account_id    product_id
2001-01-01    2001-01-31   1             1
2001-02-01    2001-02-20   1                 


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 08:47

    In up to date postgres versions (I tested it in 9.6 but I assume it's working in >=9.2) you can use the build in function tstzrange() as mentioned in some other comments. Null values will be treated as positive or negative infinity by default and the CHECK contraint is then not explicitly needed anymore (if you are fine that the check is only <= and a range can start and end with the same date). Only the extension btree_gist is still needed:

    CREATE EXTENSION btree_gist;
    
    CREATE TABLE test (
        from_ts TIMESTAMPTZ,
        to_ts TIMESTAMPTZ,
        account_id INTEGER DEFAULT 1,
        product_id INTEGER DEFAULT 1,
        CONSTRAINT overlapping_times EXCLUDE USING GIST (
            account_id WITH =,
            product_id WITH =,
            TSTZRANGE(from_ts, to_ts) WITH &&
        )
    );
    

提交回复
热议问题