There is a database and a very large table. It is necessary to read each entry in the table only once in blocks on several computers at the same time for subsequent long-term processing. The first thought was: to enter an additional StatusCode field, which would show that this record was already processed, but if one computer took one block of data for processing but did not save the changes yet, then the other computer reads the same data set and processes his second time, which is very bad.
For clarity, I wrote a small program in C #, although, I think, who does not know him, he will understand in principle
static void Main() { Console.WriteLine(Chek()); List<Task> tl = new List<Task>(); for (int i = 0; i < 20; i++) { tl.Add(Task.Factory.StartNew(Fun, TaskCreationOptions.LongRunning)); } Console.WriteLine("wait"); Task.WaitAll(tl.ToArray()); Console.WriteLine(Chek()); Console.WriteLine("removedCount: " + Reset()); } static void Fun() { using (ApplicationContext db = new ApplicationContext()) using (var transaction = db.Database.BeginTransaction()) { try { var emp = db.Employees.Where(i => i.StatusCode == 0).Take(10).ToList(); foreach (var item in emp) { //выполняем что-то с каждым элементом, что должны выполнить ОДИН раз item.StatusCode++; } db.SaveChanges(); transaction.Commit(); } catch (Exception ex) { if (ex.InnerException != null) Console.WriteLine(ex.InnerException.Message); else Console.WriteLine(ex.Message); transaction.Rollback(); } } } static int Reset() { using (ApplicationContext db = new ApplicationContext()) { return db.Database.ExecuteSqlCommand("UPDATE Employees SET StatusCode=0 WHERE StatusCode>0"); } } static int Chek() { using (ApplicationContext db = new ApplicationContext()) { return db.Employees.Where(i => i.StatusCode > 0).Count(); } } And the result
Expectedly, there should be 200 (20 streams change blocks of 10)
How to solve this problem?

select for updatequeries. The second computer performing such a cast for the same block will receive figvam, because the records are already locked in another transaction. then you just have to go to the next block. Etc. Sooner or later it will reach the free (not blocked) block. Like that. Specify the topic of blocking and isolation of transactions in Google. At a minimum there should be an option "do not wait for locking release". For example, in postgresselect тоси-боси for update nowait- Sergey