Skip to content

fix: do not reserve statement name on prepare failure - #4953

Merged
olavloite merged 2 commits into
postgresql-dialectfrom
do-not-preserve-statement-name
Sep 28, 2026
Merged

olavloite merged 2 commits into
postgresql-dialectfrom
do-not-preserve-statement-name

Conversation

@olavloite

Copy link
Copy Markdown
Collaborator

Deregister prepared statements from the connection session if statement analysis fails during PREPARE. This ensures that a failed PREPARE does not reserve the statement name or block subsequent attempts.

Also align duplicate prepared statement handling with PostgreSQL:

  • Return SQLSTATE 42P05 (DuplicatePreparedStatement) instead of dropping the connection on duplicate statement names.
  • Fold unquoted statement names to lowercase and strip identifier quotes in PREPARE to match EXECUTE and DEALLOCATE behavior.
  • Reject zero-length delimited identifiers (PREPARE "").

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

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 improves PGAdapter's handling of prepared statements by checking for duplicate statement names, handling case-insensitive and quoted identifiers, and throwing errors for zero-length delimited identifiers instead of throwing an IllegalStateException. It also adds comprehensive tests to verify these behaviors. The review feedback suggests catching Exception instead of Throwable to avoid swallowing critical JVM errors, and adding a defensive null check on the parsed statement name to prevent potential NullPointerExceptions.

Comment thread src/main/java/com/google/cloud/spanner/pgadapter/statements/PrepareStatement.java Outdated
Comment thread src/main/java/com/google/cloud/spanner/pgadapter/statements/PrepareStatement.java Outdated
Deregister prepared statements from the connection session if statement
analysis fails during PREPARE. This ensures that a failed PREPARE does
not reserve the statement name or block subsequent attempts.

Also align duplicate prepared statement handling with PostgreSQL:
- Return SQLSTATE 42P05 (DuplicatePreparedStatement) instead of dropping
  the connection on duplicate statement names.
- Fold unquoted statement names to lowercase and strip identifier quotes
  in PREPARE to match EXECUTE and DEALLOCATE behavior.
- Reject zero-length delimited identifiers (`PREPARE ""`).
@olavloite
olavloite force-pushed the do-not-preserve-statement-name branch from 0c0d9ba to cbe03f3 Compare September 24, 2026 15:03
@olavloite

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

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 improves the handling of duplicate prepared statements and statement name identifiers in PGAdapter. Instead of throwing an IllegalStateException when a prepared statement name is reused, PGAdapter now gracefully returns a DuplicatePreparedStatement error (42P05) to the client, keeping the original statement intact. Additionally, it adds support for case-folding and unquoting statement identifiers, throwing an error for zero-length delimited identifiers. Comprehensive unit and integration tests have been added to verify these behaviors. I have no additional feedback to provide as the implementation is robust and well-tested.

@olavloite
olavloite merged commit 6e6aec9 into postgresql-dialect Sep 28, 2026
51 checks passed
@olavloite
olavloite deleted the do-not-preserve-statement-name branch September 28, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants