Skip to content

feat(spanner): Add PostgreSQL UPDATE DML RETURNING sample - #14864

Open
iennae wants to merge 3 commits into
feat/spanner-postgresql-dml-last-statementfrom
feat/spanner-postgresql-update-dml-returning
Open

iennae wants to merge 3 commits into
feat/spanner-postgresql-dml-last-statementfrom
feat/spanner-postgresql-update-dml-returning

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

PostgreSQL-dialect Spanner databases use UPDATE ... RETURNING to return updated column values directly from a read-write transaction.

Adds spanner_postgresql_update_dml_returning using the canonical cross-language region tag.

Note: Stacked on #14851 (feat/spanner-postgresql-dml-last-statement) so this PR shows only this sample's diff. It will merge into main after #14851.

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

PostgreSQL-dialect Spanner uses UPDATE ... RETURNING to inspect modified columns without issuing a separate SELECT.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:48
@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
@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

@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 adds a new sample script and tests demonstrating how to execute a PostgreSQL-dialect Spanner UPDATE DML statement with a RETURNING clause. The feedback recommends safely accessing results.stats to prevent a potential AttributeError if the server does not return query statistics.

)
for row in results:
budgets.append(int(row[0] or 0))
row_counts.append(int(results.stats.row_count_exact))

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

The results.stats property can be None if the server does not return query statistics. To prevent a potential AttributeError: 'NoneType' object has no attribute 'row_count_exact', you should safely access row_count_exact using a conditional check or helper.

Suggested change
row_counts.append(int(results.stats.row_count_exact))
row_count = results.stats.row_count_exact if results.stats else 0
row_counts.append(int(row_count))

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.

Uses a nonlocal row counter with a defensive results.stats check and omits project identifiers from stdout and stderr.
…l_returning

Problem: multi-line SQL and DDL statements were written as adjacent string
fragments. That style is easy to get wrong (a missing trailing space joins
two SQL tokens) and is harder to read than the statement itself, and the
samples in this set mixed it with triple-quoted SQL.

Solution: write each multi-line statement as a single triple-quoted string,
matching the layout already used by the other samples. Only whitespace
between SQL tokens changes; the statements are otherwise identical.

Files updated:
  - spanner/postgresql/src/spanner_postgresql/postgresql_update_dml_returning.py
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