I need to be sure, that for a certain timestamp, there will be only one row in table
foo
forfoo.barid
And by "timestamp" you seem to mean a certain period of time.
An exclusion constraint on a range type, combined with equality on barid
(utilizing the additional module btree_gist
) would be the perfect solution.
CREATE EXTENSION btree_gist; -- needed once per database
CREATE TABLE foo (
fooid serial PRIMARY KEY
, barid integer NOT NULL REFERENCES bar(barid)
, bazid integer NOT NULL REFERENCES baz(bazid)
, time_range tsrange NOT NULL -- replaces startdate & enddate
, EXCLUDE USING gist (barid WITH =, time_range WITH &&)
);
This requires Postgres 9.2 or later.
Related: