cardano-db-sync icon indicating copy to clipboard operation
cardano-db-sync copied to clipboard

Insert in Parallel

Open kderme opened this issue 2 years ago • 3 comments

Currently all db insertions are done by a single thread. We could try and use more. We have to be careful about shared resources

  • Different threads should use different connections, as conenctions is not thread safe.
  • The monad stack needs to change, as ReaderT SqlBackend .. means only a single connection is available.
  • Two connections mean two differnt postgres transactions. We should make sure that if any of the transaction is rollbacked so is the other. We want to avoid cases where a block is partially inserted.

Parallelism provides more benefits when there is enough workload for each thread. So this can have more benefits after https://github.com/orgs/input-output-hk/projects/52/views/7

kderme avatar Nov 04 '22 13:11 kderme

I tried this. It does not work, Postgres only allows inserts into the database from a single thread.

I tried this specifically for reward and epoch stake tables. Separate connections were used.

erikd avatar Nov 05 '22 05:11 erikd

Do you remember the error? It should be possible to do parallel insertions, they just have to be from different connections.

kderme avatar Nov 05 '22 18:11 kderme

It absolutely 100% was separate connections. The error message from Postgres was that parallel insertions were not allowed. This may have been due to the large number of foreign keys we were using at the time.

erikd avatar Nov 05 '22 22:11 erikd