On our forum we have “chat” threads which see a fairly rapid rate of post creation. We recently switched to running Unicorn and Rails 4, and have also started seeing these errors around the same time:
ActiveRecord::RecordNotUnique: ERROR: duplicate key value violates unique constraint "index_posts_on_topic_id_and_post_number" DETAIL: Key (topic_id, post_number)=(2068, 9104) already exists.
It looks like this happens when two Unicorn processes try to create a post at the same time, but end up setting the same post number as a result. This is the relevant part of the stack trace from New Relic.