Skip to content

feat(spanner): Add PostgreSQL-dialect online ALTER TABLE sample - #14936

Open
iennae wants to merge 2 commits into
feat/spanner-postgresql-dml-last-statementfrom
feat/spanner-postgresql-add-column
Open

iennae wants to merge 2 commits into
feat/spanner-postgresql-dml-last-statementfrom
feat/spanner-postgresql-add-column

Conversation

@iennae

@iennae iennae commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Description

Adds spanner_postgresql_add_column adding a BIGINT column via DatabaseAdminClient.update_database_ddl on a PostgreSQL-dialect database.

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 schema evolution guides add columns online using UpdateDatabaseDdlRequest. Add a standalone spanner_postgresql_add_column sample adding MarketingBudget BIGINT with unit and live tests.
@iennae
iennae requested review from a team as code owners November 18, 2026 05:14
@snippet-bot

snippet-bot Bot commented Nov 18, 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 18, 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 script and corresponding tests to add a MarketingBudget column to the Albums table in a PostgreSQL-dialect Spanner database. The reviewer suggests explicitly catching TimeoutError when waiting for the DDL operation to complete to prevent unhandled tracebacks in case of a timeout.

Comment on lines +38 to +54
try:
operation = client.update_database_ddl(request=request)
print(f"Waiting for PostgreSQL column '{column_name}' addition...")
operation.result(timeout=300)
except (exceptions.AlreadyExists, exceptions.FailedPrecondition) as err:
print(
f"error: column '{column_name}' already exists or preconditions "
f"failed on database '{database_id}' in instance '{instance_id}': "
f"{err.message}",
file=sys.stderr,
)
except exceptions.GoogleAPICallError as err:
print(
f"error: failed to add column '{column_name}' on database "
f"'{database_id}' in instance '{instance_id}': {err.message}",
file=sys.stderr,
)

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 operation.result(timeout=300) call can raise a TimeoutError (or concurrent.futures.TimeoutError) if the DDL operation takes longer than 5 minutes. Since this is not a subclass of GoogleAPICallError, it will result in an unhandled traceback. It is recommended to catch TimeoutError explicitly to provide a clean and user-friendly error message.

    try:
        operation = client.update_database_ddl(request=request)
        print(f"Waiting for PostgreSQL column '{column_name}' addition...")
        operation.result(timeout=300)
    except (exceptions.AlreadyExists, exceptions.FailedPrecondition) as err:
        print(
            f"error: column '{column_name}' already exists or preconditions "
            f"failed on database '{database_id}' in instance '{instance_id}': "
            f"{err.message}",
            file=sys.stderr,
        )
    except exceptions.GoogleAPICallError as err:
        print(
            f"error: failed to add column '{column_name}' on database "
            f"'{database_id}' in instance '{instance_id}': {err.message}",
            file=sys.stderr,
        )
    except TimeoutError as err:
        print(
            f"error: timeout waiting for column '{column_name}' addition: {err}",
            file=sys.stderr,
        )

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.

Good catch, done: added an except TimeoutError handler and a unit test. Since the timeout only bounds the client-side wait, the message notes the operation may still be running (16b64b2).

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_add_column.py
  - spanner/postgresql/tests/postgresql_add_column_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.

1 participant