Skip to content

feat(spanner): Add PostgreSQL DML last_statement optimization sample - #14851

Open
iennae wants to merge 6 commits into
mainfrom
feat/spanner-postgresql-dml-last-statement
Open

iennae wants to merge 6 commits into
mainfrom
feat/spanner-postgresql-dml-last-statement

Conversation

@iennae

@iennae iennae commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Description

PostgreSQL-dialect Spanner databases support last_statement=True with $1/$2 positional parameter bindings (p1/p2) to commit inline with the final DML statement.

Establishes the spanner/postgresql Python 3.13+ sample package and adds spanner_postgresql_dml_last_statement.

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 transactions support last_statement=True with positional $1/$2 parameters, replacing an untagged inline documentation snippet.
@iennae
iennae requested review from a team as code owners November 17, 2026 23:47
@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 2 region tags.

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 introduces a new set of Python samples for Cloud Spanner's PostgreSQL dialect, specifically demonstrating DML updates with the last_statement=True transaction optimization. It includes the sample script, setup and configuration files, and unit/integration tests. The review feedback recommends a more robust implementation for retrieving the updated row count from the transaction callback: instead of appending to a mutable list (which can accumulate duplicate values if the transaction is retried), the callback should return the row count directly, as database.run_in_transaction returns the callback's return value.

Comment on lines +39 to +51
updated_counts: list[int] = []

def update_singers(transaction: spanner_v1.Transaction) -> None:
row_count = transaction.execute_update(
"UPDATE Singers SET LastName = $1 WHERE SingerId = $2",
params={"p1": "Doe", "p2": 54214},
param_types={"p1": param_types.STRING, "p2": param_types.INT64},
last_statement=True,
)
updated_counts.append(int(row_count))

try:
database.run_in_transaction(update_singers)

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

Instead of using a mutable list (updated_counts) to capture the row count from within the transaction callback, it is more idiomatic and robust to return the value from the callback function. database.run_in_transaction returns the value returned by the callback. This avoids accumulating duplicate values in the list if the transaction is retried due to aborts.

Suggested change
updated_counts: list[int] = []
def update_singers(transaction: spanner_v1.Transaction) -> None:
row_count = transaction.execute_update(
"UPDATE Singers SET LastName = $1 WHERE SingerId = $2",
params={"p1": "Doe", "p2": 54214},
param_types={"p1": param_types.STRING, "p2": param_types.INT64},
last_statement=True,
)
updated_counts.append(int(row_count))
try:
database.run_in_transaction(update_singers)
def update_singers(transaction: spanner_v1.Transaction) -> int:
row_count = transaction.execute_update(
"UPDATE Singers SET LastName = $1 WHERE SingerId = $2",
params={"p1": "Doe", "p2": 54214},
param_types={"p1": param_types.STRING, "p2": param_types.INT64},
last_statement=True,
)
return int(row_count)
try:
updated_count = database.run_in_transaction(update_singers)

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.

else:
print(f"Resource ID: {database.name}")
print("Last Statement Optimization: True")
print(f"Updated Rows: {updated_counts[-1]}")

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

Update this line to print the updated_count variable returned by run_in_transaction.

Suggested change
print(f"Updated Rows: {updated_counts[-1]}")
print(f"Updated Rows: {updated_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.

…mple

Replaces the outer list cell with a nonlocal integer, isolates live test row setup/teardown, and omits project_id from output.
Prints only runtime resource and query results instead of hardcoded string literals.
Dropping a sequence referenced by a column DEFAULT requires detaching the column default before executing DROP SEQUENCE. Add a standalone spanner_postgresql_drop_sequence sample and test suite.
Problem: review of this sample identified correctness and robustness
improvements in the sample and its tests.

Solution: apply the accepted review suggestions and extend the tests to
cover the updated behavior. Details are in the review thread replies.

Files updated:
  - spanner/postgresql/src/spanner_postgresql/postgresql_drop_sequence.py
  - spanner/postgresql/tests/postgresql_drop_sequence_test.py
…statement

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/tests/postgresql_dml_last_statement_test.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