title: State machine represented on DB, enforcement of state transition
body: <p>I've got a finite state machine which represents the phases of a job.
  I need to represent the states in a Postgres database. I would like to enforce the code correctness by forbidding updates from one state to the other unless the state machine allows so.</p>
  
  <p>A naive way to accomplish my goal could be the acquisition of an <em>exclusive lock</em> on the table, within the transaction check the current state and the next state, abort with errors in case of invalid update.</p>
  
  <p>This is clearly a performances killer, since I'm going to lock the <em>Job</em> table at each state transition.</p>
  
  <p>Is there a way to accomplish the same goal via constraints?</p>