Repository navigation
Conversation
PostgreSQL-dialect primary keys avoid write hotspots using BIT_REVERSED_POSITIVE sequences with nextval('Seq') defaults. Add spanner_postgresql_create_sequence creating Seq and inserting Customers.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new script, postgresql_create_sequence.py, along with its corresponding tests, to demonstrate creating a bit-reversed PostgreSQL sequence and inserting rows using a RETURNING clause in Spanner. The feedback suggests splitting the DDL and DML operations into separate try-except blocks for more precise error handling and using standard exception handling for consistency. Additionally, it recommends returning the customer IDs directly from the transaction callback instead of using nonlocal state mutation to make the code more idiomatic.
| customer_ids: list[int] = [] | ||
|
|
||
| def insert_customers(transaction: spanner_v1.transaction.Transaction) -> None: | ||
| nonlocal customer_ids | ||
| rows = list( | ||
| transaction.execute_sql( | ||
| "INSERT INTO Customers (CustomerName) VALUES " | ||
| "('Alice'), ('David'), ('Marc') " | ||
| "RETURNING CustomerId" | ||
| ) | ||
| ) | ||
| customer_ids = [int(row[0]) for row in rows] | ||
|
|
||
| try: | ||
| operation = admin_client.update_database_ddl(request=ddl_request) | ||
| print(f"Waiting for PostgreSQL sequence 'Seq' creation on '{database_id}'...") | ||
| operation.result(timeout=300) | ||
| database.run_in_transaction(insert_customers) | ||
| except (exceptions.AlreadyExists, exceptions.FailedPrecondition) as err: | ||
| print( | ||
| f"error: sequence 'Seq' or table 'Customers' already exists on " | ||
| f"database '{database_id}' in instance '{instance_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to create PostgreSQL sequence on database " | ||
| f"'{database_id}' in instance '{instance_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| else: | ||
| print(f"Resource ID: {database_id}") | ||
| for cid in customer_ids: | ||
| print(f"Customer ID: {cid}") | ||
| print(f"Inserted Customers: {len(customer_ids)}") |
There was a problem hiding this comment.
There are two main areas of improvement in this block:
- Misleading Error Handling: Currently, the
try-exceptblock wraps both the DDL sequence/table creation and the DML transaction. Splitting these into separatetry-exceptblocks ensures precise error reporting. To maintain consistency with other code samples in this repository, we should use standard exception handling rather than introducing specific library exception classes likeGoogleAPICallError. - Idiomatic Transaction Callback: In Python's Spanner SDK,
database.run_in_transactionreturns the value returned by the callback function. Returning the list of IDs directly frominsert_customersis more idiomatic and avoids the need fornonlocalstate mutation.
def insert_customers(transaction: spanner_v1.transaction.Transaction) -> list[int]:
rows = list(
transaction.execute_sql(
"INSERT INTO Customers (CustomerName) VALUES "
"('Alice'), ('David'), ('Marc') "
"RETURNING CustomerId"
)
)
return [int(row[0]) for row in rows]
try:
operation = admin_client.update_database_ddl(request=ddl_request)
print(f"Waiting for PostgreSQL sequence 'Seq' creation on '{database_id}'...")
operation.result(timeout=300)
except Exception as err:
print(
f"error: failed to create PostgreSQL sequence on database "
f"'{database_id}' in instance '{instance_id}': {err}",
file=sys.stderr,
)
return
try:
customer_ids = database.run_in_transaction(insert_customers)
except Exception as err:
print(
f"error: failed to insert customers on database "
f"'{database_id}' in instance '{instance_id}': {err}",
file=sys.stderr,
)
return
print(f"Resource ID: {database_id}")
for cid in customer_ids:
print(f"Customer ID: {cid}")
print(f"Inserted Customers: {len(customer_ids)}")References
- For code samples, prioritize consistency with existing samples in the repository over introducing specific library exception classes (like 'google.api_core.exceptions.GoogleAPIError') if they are not already commonly used.
There was a problem hiding this comment.
Thanks! Keeping the current structure: the sample catches specific google.api_core.exceptions types so readers see concrete error handling, and the callback records its result in an enclosing variable for consistency with the rest of this sample set. A TimeoutError handler for the DDL wait was added separately (8935efe).
amcolin
left a comment
There was a problem hiding this comment.
Left some non-blocking suggestions.
| import sys | ||
|
|
||
| from google.api_core import exceptions | ||
| from google.cloud import spanner |
There was a problem hiding this comment.
I believe this is a redundant import, we could just call the client like spanner_v1.Client(project=project_id)
There was a problem hiding this comment.
Thanks for the suggestion! I'm going to keep this one as is: the samples in this set consistently create the client through spanner.Client, which is the entry point used in the product documentation, and spanner_v1 is only imported here for the transaction callback's type annotation. Switching just this sample to spanner_v1.Client would make it the odd one out.
| f"database '{database_id}' in instance '{instance_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: |
There was a problem hiding this comment.
Missing to handle Timeout error:
except TimeoutError as err:
print(
f"error: timed out waiting to drop PostgreSQL sequence "
f"'{sequence_name}' on database '{database_id}' in instance "
f"'{instance_id}': {err}",
file=sys.stderr,
)There was a problem hiding this comment.
Good catch, thank you! Added an except TimeoutError handler and a unit test for it (8935efe). One note on the wording: the timeout only bounds how long the client waits and does not cancel the schema change, so the message says the wait timed out and that the operation may still be running.
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_create_sequence.py - spanner/postgresql/tests/postgresql_create_sequence_test.py
…quence 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_create_sequence.py
Description
Adds
spanner_postgresql_create_sequencecreating aBIT_REVERSED_POSITIVEPostgreSQL sequence and inserting rows withRETURNING.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions