Skip to content

feat(spanner): Add SELECT FOR UPDATE repeatable read sample - #14839

Open
iennae wants to merge 4 commits into
mainfrom
feat/spanner-select-for-update
Open

iennae wants to merge 4 commits into
mainfrom
feat/spanner-select-for-update

Conversation

@iennae

@iennae iennae commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Under IsolationLevel.REPEATABLE_READ, plain SELECT queries read at the transaction's snapshot timestamp without verifying whether concurrent transactions modify those cells before commit, so transactions that compute updates from read values must use SELECT ... FOR UPDATE to validate scanned cells at commit time and prevent lost updates.

Establishes the spanner/transactions Python 3.13+ sample package and adds spanner_select_for_update demonstrating SELECT ... FOR UPDATE with TransactionOptions.IsolationLevel.REPEATABLE_READ.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

Acquiring exclusive row locks during repeatable read transactions in Cloud Spanner requires combining IsolationLevel.REPEATABLE_READ with SELECT FOR UPDATE inside run_in_transaction.
@iennae
iennae requested review from a team as code owners November 17, 2026 18:48
@snippet-bot

snippet-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples. labels Nov 17, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new Python sample demonstrating pessimistic row locking (SELECT ... FOR UPDATE) under REPEATABLE_READ isolation in Cloud Spanner, along with its associated tests and configuration files. A review comment identifies a potential issue in select_for_update.py where querying non-existent albums or encountering NULL budget values could lead to IndexError or TypeError exceptions, and suggests adding appropriate validation and fallback logic.

Comment on lines +68 to +69
source_budget = int(source_rows[0][0]) - transfer_amount
target_budget = int(target_rows[0][0]) + transfer_amount

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If the requested albums do not exist in the database, source_rows or target_rows will be empty, causing an IndexError when attempting to access source_rows[0] or target_rows[0]. Additionally, if MarketingBudget is NULL, calling int() on None will raise a TypeError. It is safer to check if the rows exist and handle potential None values gracefully.

        if not source_rows or not target_rows:
            raise ValueError("Source or target album not found.")
        source_budget = (source_rows[0][0] or 0) - transfer_amount
        target_budget = (target_rows[0][0] or 0) + transfer_amount

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.

… UPDATE

Running the budget transfer against an unseeded Albums table or a row with a NULL MarketingBudget previously raised an unhandled IndexError or TypeError.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Prints only runtime resource and query results instead of hardcoded string literals.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants