Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

After moving a batch job's transactions to PostgreSQL REPEATABLE READ to get consistent reports, the nightly job that also updates a status column fails intermittently with 'ERROR: could not serialize access due to concurrent update'. The team proposes going back to READ COMMITTED. What do you recommend?

Try it yourself

Isolation levels: what a second read sees

Step through two transactions and see exactly when read committed and repeatable read give different answers.

Read this example as text

T1 at read committed

  1. T1 begins. Transaction T1 starts with isolation level READ COMMITTED. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under read committed each statement looks afresh at what is committed when the statement starts.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 reads 150. The same query, inside one transaction, returned 100 and then 150. This is a non-repeatable read, and read committed allows it.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100, 100, then 150: a non-repeatable read.

T1 at repeatable read

  1. T1 begins. Transaction T1 starts with isolation level REPEATABLE READ. The row holds balance 100.
  2. T1 reads the balance. T1 reads 100. Under repeatable read this first read fixes T1's snapshot: T1 will keep seeing the data as it was committed at this moment.
  3. T2 changes the row. A second transaction, T2, sets the balance to 150 but has not committed. The database keeps both versions: 100 is still the committed one.
  4. T1 reads again, before T2 commits. T1 reads 100 again. Neither level shows another transaction's uncommitted change, so there is no dirty read. T1 does not wait for T2 either.
  5. T2 commits. T2 commits. The committed balance is now 150.
  6. T1 reads a third time. T1 still reads 100. Its snapshot was fixed at its first read, so T2's commit is invisible to it. The read is repeatable.
  7. T1 commits. T1 ends. A transaction that starts now reads 150 at either level.

T1 read 100 three times: its reads were repeatable.

What this example leaves out
  • Modelled on PostgreSQL and MySQL InnoDB, where a plain SELECT reads a snapshot and never waits for a writer.
  • SQL Server's default read committed uses locks instead: there, T1's second read would wait for T2 to finish.
  • Only reads are compared. What happens when T1 also writes the row (a lost update, or an error in PostgreSQL) is not modelled.

The error is not a regression; it is snapshot isolation doing its job, because the job tried to update rows that another transaction changed and committed after the job's snapshot was taken. I would not go back to READ COMMITTED for everything. Either split the job so the report runs as a read-only REPEATABLE READ transaction and the status updates run in small READ COMMITTED transactions with conditional updates, or keep REPEATABLE READ and retry each short transaction from the start.

Why this question is asked

Teams often move to a stronger isolation level for one reason and then meet a side effect they did not expect. Here the reason was a consistent report, and the side effect is serialization failures on writes. The tempting reaction is to undo the change, which removes the error by removing the protection that caused it. Interviewers ask this to see whether we understand why the error happens and can design the job so both needs are met. In this answer, we will explain the error, look at two good options, discuss what going back would cost, and see how to write the retry correctly in Spring.

1Why the Error Appears

At REPEATABLE READ, PostgreSQL gives the transaction one snapshot, taken at its first statement, and every read uses it. That is exactly what made the report consistent. But when the transaction later tries to UPDATE a row, PostgreSQL checks whether a transaction that committed after the snapshot already changed that row. If so, it cannot apply our update without either ignoring that change or acting on data our snapshot does not show, so it aborts with ERROR: could not serialize access due to concurrent update, SQLSTATE 40001.

A nightly job is the worst case for this. Its transaction runs long, so its snapshot gets old, and during that time the online application keeps updating the same orders. The longer the job runs before it writes, the more rows have changed under it, which is why the failure is intermittent: it depends on whether a customer touched one of those orders during the job.

Step throughWhy the nightly job fails
Nightly jobPostgreSQLOnline app
BEGIN REPEATABLE READ; report queries (snapshot at 01:00)
UPDATE order 812 status, commit (01:07)
UPDATE order 812 SET status = 'BILLED' (01:20)
ERROR 40001: row changed after your snapshot

4 messages in order. Play them, or step through them one at a time.

2Option 1: Split the Job by What Each Part Needs

The report and the updates want different things. The report needs one consistent picture and never writes. The updates need fresh data and safety against overwriting the online application's changes. So we give each its own transactions.

Java
// Part 1: the report, one read-only snapshot
@Transactional(isolation = Isolation.REPEATABLE_READ, readOnly = true)
public Report buildReport(LocalDate day) { ... } // SET TRANSACTION READ ONLY; can never hit this error
// Part 2: the status updates, small batches at READ COMMITTED
@Transactional // READ COMMITTED, the PostgreSQL default
public int markBilled(List<Long> ids) { // NamedParameterJdbcTemplate
return jdbc.update(
"UPDATE orders SET status = 'BILLED' WHERE id IN (:ids) AND status = 'SHIPPED'",
Map.of("ids", ids)); // 498 of 500 rows: 2 had changed state
}

A read-only transaction never writes, so it can never get this error, and its snapshot gives the report its consistency. The updates run in batches of, say, 500 rows, each in its own short transaction. The condition in the WHERE clause, status = 'SHIPPED', is the important part: at READ COMMITTED, PostgreSQL re-checks it against the latest committed row, so if the online application already moved an order to CANCELLED, our update simply skips it instead of overwriting it. The affected row count tells us how many we changed. This is usually the best design for a batch job.

3Option 2: Keep REPEATABLE READ and Retry

If the updates really must be based on the same snapshot as the report, we keep REPEATABLE READ and treat 40001 as a normal outcome. The failed transaction is rolled back and run again from the start; its new snapshot shows the online change, and its logic decides again. For this to be practical the transactions must be short: retrying a 40-minute transaction is not a plan, but retrying a two-second batch is. So we still split the work into many small transactions and retry each one a bounded number of times.

4What Going Back to READ COMMITTED Costs

Going back for everything removes the error, but it also removes the consistent report the team moved for in the first place: each query would see a slightly different database. And it can reintroduce lost updates if the job reads a status in Java and then writes an absolute value. The error was the database telling us that the job was about to act on stale data. Silencing it might be acceptable, but it should be a conscious decision recorded in the job's design, not a reflex.

5Where the Retry Belongs in Spring

In Spring, SQLSTATE 40001 from PostgreSQL is translated into a ConcurrencyFailureException subtype, typically CannotAcquireLockException or PessimisticLockingFailureException depending on your version, so we catch ConcurrencyFailureException. The retry must wrap the whole @Transactional call from outside, through the proxy.

Java
@Component
class BatchRunner {
private final BillingTx tx; // a different bean, so each call goes through its proxy
void runBatch(List<Long> ids) {
for (int attempt = 1; ; attempt++) {
try {
tx.billBatch(ids); // new REPEATABLE READ transaction every attempt
return;
} catch (ConcurrencyFailureException e) {
if (attempt == 3) throw e; // give up and report after three tries
}
}
}
}

Notice that the loop lives in a different bean from the @Transactional method. If the loop were inside the transactional method, every attempt would run in the same, already failed transaction.

6Follow-up questions

Why must the retry not happen inside the same transaction?

After an error, PostgreSQL marks the transaction as aborted: every further statement fails with current transaction is aborted, commands ignored until end of transaction block. Even if it did not, the transaction would still have the same old snapshot, so the retry would see the same stale data and fail the same way. Only a new transaction gets a new snapshot. In Spring, that means the loop must call the @Transactional method through its proxy, from another bean or with TransactionTemplate, so each attempt starts, and rolls back, a transaction of its own.

Would SERIALIZABLE help here?

Not with this error. SERIALIZABLE includes the same write-conflict check, so it would fail the same way, plus aborts for read-write dependencies. It helps when the problem is write skew, not here.

Does a long read-only report transaction have any cost?

It cannot hit write conflicts, but it holds its snapshot for as long as it runs, and VACUUM cannot remove row versions that snapshot might still need. A report of a few minutes is harmless; one that runs for hours on a busy primary can cause bloat, so very long reports are better run on a replica or from an exported snapshot.

7Conclusion

In this answer, we saw that the could not serialize access error is snapshot isolation refusing to update a row changed after the job's snapshot, which is likely in a long nightly job. The best fix is usually to split the job: the report as a read-only REPEATABLE READ transaction and the updates as small READ COMMITTED transactions with conditional updates. Keeping REPEATABLE READ with retries also works if transactions are short. Going back to READ COMMITTED everywhere gives up the consistent report, and in Spring the retry must wrap the whole @Transactional call from outside.

What to say in the interviewAbout 80 seconds aloud

Open with

I'd push back on going back to READ COMMITTED. The error is expected snapshot isolation behaviour: the job's transaction took its snapshot at the start, the online app changed and committed some of the same orders during the run, and when the job tried to update those rows PostgreSQL refused, because applying the update would mean acting on data the snapshot doesn't show.

Then, if they want more

It's intermittent because it depends on whether a customer touched those orders during the job. My first recommendation is to split the job. The report runs in one REPEATABLE READ, read-only transaction, which can never get this error and stays consistent.

The status updates run in small READ COMMITTED batches with conditional updates, like where status = 'SHIPPED', so PostgreSQL re-checks the latest row and we skip anything the app already changed, and the row count tells us what happened. The alternative is to keep REPEATABLE READ and retry each short transaction from the start.

Going back to READ COMMITTED everywhere loses the consistent report and can bring back lost updates, so that should be a deliberate choice. In Spring the error arrives as a ConcurrencyFailureException subtype, and the retry has to wrap the whole transactional call from another bean, never run inside it.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "After moving a batch job's transactions to PostgreSQL REPEATABLE READ to get consistent...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/f695f1e920/