Skip to content

Intermittent AlreadyExists 409 Error on Insert Mutation Retry #15883

Closed

Description

@waltaskew

Environment details

  • OS type and version: Linux, Debian
  • Python version: Python 3.12.2
  • pip version: pip 24.2
  • google-cloud-spanner version: google-cloud-spanner==3.57.0

Steps to reproduce

I'm intermittently experiencing AlreadyExists: 409 errors when performing batch writes using the mutations API. It appears the sequence of events is:

  • Attempt to commit a batch mutation insert
  • The commit aborts
  • The library retries the commit
  • The commit fails during the retry because the row(s) already exist

It's as if the transaction isn't being rolled back in between commit attempts, so the commit in the retry will fail.

I'm looking at
_retry_on_aborted_exception
and
Batch.commit

and I don't see explicit rollback requests between commits on retry.

Code example

with database.batch() as batch:
  for table, column, values in stuff_to_write:
    batch.insert(table=table, columns=columns, values=values)

Stack trace

with self.database.batch() as batch:
  File "lib/python3.12/site-packages/google/cloud/spanner_v1/database.py", line 1354, in __exit__
    self._batch.commit(
  File "lib/python3.12/site-packages/google/cloud/spanner_v1/batch.py", line 262, in commit
    response = _retry_on_aborted_exception(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "ib/python3.12/site-packages/google/cloud/spanner_v1/_helpers.py", line 522, in _retry_on_aborted_exception
    return func()
           ^^^^^^
  File "lib/python3.12/site-packages/google/cloud/spanner_v1/batch.py", line 260, in wrapped_method
    return commit_method()
           ^^^^^^^^^^^^^^^
  File "lib/python3.12/site-packages/google/cloud/spanner_v1/services/spanner/client.py", line 2133, in commit
    response = rpc(
               ^^^^
  File "lib/python3.12/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__
    return wrapped_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func
    return retry_target(
           ^^^^^^^^^^^^^
  File "lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target
    _retry_error_helper(
  File "lib/python3.12/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper
    raise final_exc from source_exc
  File "lib/python3.12/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target
    result = target()
             ^^^^^^^^
  File "lib/python3.12/site-packages/google/api_core/timeout.py", line 130, in func_with_timeout
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.12/site-packages/google/api_core/grpc_helpers.py", line 78, in error_remapped_callable
    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.AlreadyExists: 409 Row [a44...] in table ... already exists

Activity

  1. changed the title [-]AlreadyExists 409 Error on Insert Mutation Retry[/-] [+]Intermittent AlreadyExists 409 Error on Insert Mutation Retry[/+] on Oct 28, 2025
  2. chalmerlowe commented on Mar 2, 2026

    @chalmerlowe
    Contributor

    This issue was moved from the following repo as part of the monorepo migration: googleapis/python-spanner

  3. sinhasubham commented on May 12, 2026

    @sinhasubham
    Contributor

    Already fixed as part of this change: googleapis/python-spanner#1491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: spannerIssues related to the Spanner API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions