In order to prevent race conditions on the sequence, I would lock the table before getting the ```max(id)```. NB. You could use ```nextval``` that works also on empty tables.