Skip to content

OracleBackend survives two writers starting on one table or one key at once - #531

Merged
fede-kamel merged 1 commit into
mainfrom
fix/oracle-save-first-write-race
Nov 17, 2026
Merged

fede-kamel merged 1 commit into
mainfrom
fix/oracle-save-first-write-race

Conversation

@fede-kamel

Copy link
Copy Markdown
Member

Found while running the Oracle pair of tests/integration/test_checkpoint_index_concurrency.py (added in #526) against a live database: two adapters saving to one thread raised ORA-00001 on the shared <thread_id>:latest row, and two adapters starting on a fresh table raised ORA-00955, ORA-00054 or ORA-04020 from the first-time DDL. Both are pre-existing races in OracleBackend, unrelated to the index work that exposed them.

What changes

  • save() repeats its MERGE once on ORA-00001. For a key no writer has seen, two concurrent MERGEs both take the INSERT branch and the loser meets the primary key. The row exists once the winner commits, so the repeated statement takes the UPDATE branch. The pool is kept, since nothing was wrong with the connection.
  • _ensure_table() provisions once per process and tolerates the other process. Every concurrent first save used to run CREATE TABLE and CREATE INDEX. The DDL now runs under a lock bound to the event loop (locus.core.loop_bound), a CREATE another process won reads as created (ORA-00955), and a lock collision with that process's DDL or first writes (ORA-00054, ORA-04020) is retried up to five times with a short, growing wait. Anything else, or a collision that never clears, propagates. This is the shape the PostgreSQL backend took in fix(memory): PostgreSQL first-time table creation is serialised, so two processes starting together both succeed #523.

Tests

Against the fake Oracle connection of the index tests: the repeated MERGE, a violation that persists (surfaces after three attempts), other errors surfacing at once, both CREATEs lost, a lock collision on the index that clears after two waits, one that never clears (surfaces after five attempts, backend not marked initialised), and twenty concurrent first saves running the DDL exactly once.

Verification

  • pytest tests/unit: 11,631 passed, 657 skipped; the version-vs-installed-metadata test was deselected, as it fails for any checkout of current main in a venv whose editable install predates 5.6.0 and is unrelated to this change.
  • ruff check, ruff format --check, mypy on the changed module: clean.
  • Live against Oracle AI Database 26ai Free in a local container: a probe that creates a fresh table per round and has two adapters make ten concurrent first saves each, ten rounds, zero failures, with the retries observed firing 23 times for lost first writes and 18 times for lost CREATEs (before the change the first round failed with ORA-00955 and then ORA-04020). The Oracle concurrency and time-travel integration tests, 18 tests, passed three times in a row.

…t once

Found by the Oracle pair of test_checkpoint_index_concurrency.py on a live
database: two adapters saving to one thread raised ORA-00001 on the shared
<thread_id>:latest row, and two adapters starting on a fresh table raised
ORA-00955, ORA-00054 or ORA-04020 from the first-time DDL.

save() is a MERGE; for a key no writer has seen both take the INSERT branch
and the loser meets the primary key. The row exists once the winner commits,
so save() now repeats the MERGE once on ORA-00001 and the UPDATE branch
takes it. The pool is kept: nothing was wrong with the connection.

_ensure_table() had every concurrent first save run CREATE TABLE and
CREATE INDEX. A backend now provisions once per process under a lock bound
to the event loop (locus.core.loop_bound), a CREATE another process won
reads as created (ORA-00955), and a lock collision with that process's DDL
or first writes (ORA-00054, ORA-04020) is retried up to five times with a
short, growing wait; anything else, or a collision that never clears,
propagates. This is the shape the PostgreSQL backend took in #523 for its
own first-time DDL race.

Tests against the fake connection of the index tests: the repeated MERGE,
a violation that persists, other errors surfacing at once, both CREATEs
lost, a lock collision on the index that clears, one that never does, and
twenty concurrent first saves running the DDL once.

Signed-off-by: Federico Kamelhar <federico.kamelhar@oracle.com>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 17, 2026
@fede-kamel
fede-kamel merged commit c9b98e9 into main Nov 17, 2026
27 of 29 checks passed
@fede-kamel
fede-kamel deleted the fix/oracle-save-first-write-race branch November 17, 2026 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant