Skip to content

fix(plpgsql-parser): multi-function scripts no longer all get the first function's body#302

Merged
pyramation merged 1 commit into
mainfrom
feat/plpgsql-parser-multifn-bodies
Jul 20, 2026
Merged

fix(plpgsql-parser): multi-function scripts no longer all get the first function's body#302
pyramation merged 1 commit into
mainfrom
feat/plpgsql-parser-multifn-bodies

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

In plpgsql-parser, a script with multiple LANGUAGE plpgsql functions deparsed every function with the FIRST function's body:

CREATE FUNCTION f1() ... $$ BEGIN RAISE NOTICE 'one'; END $$ ...;
CREATE FUNCTION f2() ... $$ BEGIN RAISE NOTICE 'two'; END $$ ...;
-- transformSync output: both bodies were 'one'

Root cause: extractFunctionInfo called parsePlPgSQLSync(fullSql) — the whole script — so each ParsedFunction.plpgsql.raw contained ALL functions, and deparse/deparseSync pair each statement with plpgsql_funcs?.[0], i.e. always the first.

Fix in parse(): slice each statement's own SQL from the source using rawStmt.stmt_location/stmt_len (byte offsets, via Buffer) and pass only that to parsePlPgSQLSync, so plpgsql_funcs has exactly one entry per function and the existing [0] pairing is correct.

Tests: transform round-trip asserting each of three functions (incl. a trigger fn) keeps its own body exactly once, and a parse()-level test asserting functions[i].plpgsql.raw contains only its own function's AST. Full workspace suite passes.

This was the bug noted in constructive-db's kitchen-sink fixture ("multiple PL/pgSQL functions in one file all receive the first function's body"), which forced its transform fixtures into a single comprehensive function.

Link to Devin session: https://app.devin.ai/sessions/eeb8deba0c04475091df703c88877302
Requested by: @pyramation

@pyramation pyramation self-assigned this Jul 19, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@pyramation
pyramation merged commit 45c05b7 into main Jul 20, 2026
9 checks passed
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.

1 participant