Skip to content

Conversation

@tianzhou
Copy link
Contributor

Fix #241

Functions using BEGIN ATOMIC (SQL-standard multi-statement body, PG14+) were incorrectly wrapped with AS $$...$$ delimiters, producing invalid SQL.

  • Add check for BEGIN ATOMIC prefix alongside existing RETURN check
  • Apply same fix to procedures
  • Add test case with add_with_tax function using BEGIN ATOMIC syntax

Functions using BEGIN ATOMIC (SQL-standard multi-statement body, PG14+)
were incorrectly wrapped with AS $$...$$ delimiters, producing invalid SQL.

- Add check for BEGIN ATOMIC prefix alongside existing RETURN check
- Apply same fix to procedures
- Add test case with add_with_tax function using BEGIN ATOMIC syntax

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 15, 2026 03:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #241 where PostgreSQL functions and procedures using the SQL-standard BEGIN ATOMIC syntax (introduced in PostgreSQL 14) were incorrectly wrapped with AS $$ ... $$ delimiters, producing invalid SQL.

Changes:

  • Extended the SQL-standard body detection to recognize BEGIN ATOMIC syntax in addition to the existing RETURN clause detection
  • Applied the fix to both functions (function.go) and procedures (procedure.go)
  • Added test case demonstrating the fix with an add_with_tax function using BEGIN ATOMIC syntax

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/diff/function.go Added BEGIN ATOMIC detection to prevent wrapping SQL-standard function bodies with AS $$ delimiters
internal/diff/procedure.go Added BEGIN ATOMIC detection to prevent wrapping SQL-standard procedure bodies with AS $$ delimiters
testdata/diff/create_function/add_function/new.sql Added test function using BEGIN ATOMIC syntax to reproduce issue #241
testdata/diff/create_function/add_function/plan.txt Updated expected plan output to include the new add_with_tax function
testdata/diff/create_function/add_function/plan.sql Updated expected SQL output showing correct BEGIN ATOMIC formatting without AS $$ wrapper
testdata/diff/create_function/add_function/plan.json Updated expected JSON plan to include the new function creation step
testdata/diff/create_function/add_function/diff.sql Updated expected diff output showing the new function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

- Clarify comment in procedure.go that RETURN check is for consistency
  with function handling, not for procedure return values
- Add test case for procedures with BEGIN ATOMIC syntax (validate_input)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@tianzhou tianzhou merged commit 4bea633 into main Jan 15, 2026
8 checks passed
alecthomas pushed a commit to alecthomas/pgschema that referenced this pull request Jan 26, 2026
…ex#244)

* fix: handle BEGIN ATOMIC syntax in SQL-standard function bodies (pgplex#241)

Functions using BEGIN ATOMIC (SQL-standard multi-statement body, PG14+)
were incorrectly wrapped with AS $$...$$ delimiters, producing invalid SQL.

- Add check for BEGIN ATOMIC prefix alongside existing RETURN check
- Apply same fix to procedures
- Add test case with add_with_tax function using BEGIN ATOMIC syntax

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: address PR review feedback

- Clarify comment in procedure.go that RETURN check is for consistency
  with function handling, not for procedure return values
- Add test case for procedures with BEGIN ATOMIC syntax (validate_input)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
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.

functions with BEGIN ATOMIC produce a syntax error when dumped

1 participant