Commit indexing on 5.x releases sometimes fails with a set of errors like this:
2017-08-18 07:21:24,320 ERROR [commit-indexing:thread-2] o.h.e.jdbc.spi.SqlExceptionHelper Batch entry 16 insert into cs_repo_membership (cs_id, repository_id) values ('069a0fa3e1565c66083ec7b4d8684920f31817cc', 10661) was aborted: ERROR: insert or update on table "cs_repo_membership" violates foreign key constraint "fk_repo_membership_changeset" Detail: Key (cs_id)=(069a0fa3e1565c66083ec7b4d8684920f31817cc) is not present in table "changeset". Call getNextException to see other errors in the batch. 2017-08-18 07:21:24,320 ERROR [commit-indexing:thread-2] o.h.e.jdbc.spi.SqlExceptionHelper ERROR: insert or update on table "cs_repo_membership" violates foreign key constraint "fk_repo_membership_changeset" Detail: Key (cs_id)=(069a0fa3e1565c66083ec7b4d8684920f31817cc) is not present in table "changeset". 2017-08-18 07:21:24,321 ERROR [commit-indexing:thread-2] o.h.i.ExceptionMapperStandardImpl HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute batch]
This appears to be related to the upgrade to Hibernate 5.2.10, which has had some bugs reported related to how inserts and updates are ordered. While those bugs have been fixed, a Hibernate 5.2.11 release including them has not yet been finalized. Bitbucket Server should work around the ordering problems by using Session.flush() to force the correct order.
Potentially related: