Repository navigation
Conversation
PostgreSQL-dialect Spanner transactions support last_statement=True with positional $1/$2 parameters, replacing an untagged inline documentation snippet.
|
Here is the summary of changes. You are about to add 2 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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]}") |
There was a problem hiding this comment.
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
Description
PostgreSQL-dialect Spanner databases support
last_statement=Truewith$1/$2positional parameter bindings (p1/p2) to commit inline with the final DML statement.Establishes the
spanner/postgresqlPython 3.13+ sample package and addsspanner_postgresql_dml_last_statement.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions