postgresql - Custom constraints in postgres -


i have below table

 column   |           type           |                        modifiers                         -----------+--------------------------+---------------------------------------------------------  id        | integer                  | not null default nextval('votes_vote_id_seq'::regclass)  voter     | character varying        |   votee     | character varying        |   timestamp | timestamp time zone | default now() 

currently , have unique constraint voter , votee meaning there 1 vote per user

i enforce condition allows votes happen weekly using timestamp column. user can vote votee once week.

is there way can add custom constraints postgres? same thing functions?

a constraint special trigger in postgresql.

normal triggers won't do, because cannot see concurrent modifications of database, 2 concurrent transactions both see condition fulfilled, after commit, condition might violated.

the solution i'd recommend use serializable transactions throughout (everybody, including readers, has use them work) , verify condition in before trigger.
serializable guarantee above scenario cannot happen.


Comments