Skip to content

fix: correct LANGUAGE for overloaded functions with mixed languages (#368)#370

Merged
tianzhou merged 1 commit intomainfrom
fix/issue-368-overloaded-function-language
Mar 24, 2026
Merged

fix: correct LANGUAGE for overloaded functions with mixed languages (#368)#370
tianzhou merged 1 commit intomainfrom
fix/issue-368-overloaded-function-language

Conversation

@tianzhou
Copy link
Contributor

Summary

  • Fixed cross-join in the information_schema.routinespg_proc JOIN that caused overloaded functions with different languages to get the wrong LANGUAGE assigned
  • Added OID matching via specific_name to all 4 affected queries (GetFunctions, GetProcedures, GetFunctionsForSchema, GetProceduresForSchema)
  • Merged mixed-language overload test into existing issue_191_function_procedure_overload test case

Fixes #368

Test plan

🤖 Generated with Claude Code

…368)

The JOIN between information_schema.routines and pg_proc matched only on
function name and namespace, causing a cross-join for overloaded functions.
This resulted in wrong LANGUAGE (and other attributes) being assigned when
overloads had different languages (e.g., sql vs plpgsql).

Fix by adding OID matching via specific_name to all 4 affected queries.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 24, 2026 04:29
@greptile-apps
Copy link

greptile-apps bot commented Mar 24, 2026

Greptile Summary

This PR fixes a cross-join bug in the information_schema.routinespg_proc JOIN that caused overloaded functions with different languages to be assigned the wrong LANGUAGE in the dump output. The fix adds AND p.oid = (regexp_match(r.specific_name, '_(\d+)$'))[1]::oid to the JOIN condition in all 4 affected queries, using the OID embedded in specific_name to uniquely identify each function/procedure row.

Key changes:

  • ir/queries/queries.sql and ir/queries/queries.sql.go: All 4 queries (GetFunctions, GetProcedures, GetFunctionsForSchema, GetProceduresForSchema) now include the OID-based join condition.
  • testdata/dump/issue_191_function_procedure_overload/: Test scenario extended with provide_tx(VARIADIC text[]) (sql) and provide_tx(uuid) (plpgsql) mixed-language overloads to exercise the fix.
  • cmd/dump/dump_integration_test.go: Adds TestDumpCommand_Issue191FunctionProcedureOverload as the first proper test function wired to the existing issue_191 test data directory.

The approach is sound: information_schema.routines.specific_name in PostgreSQL is always formatted as <routine_name>_<oid>, making the regex _(\d+)$ a reliable, well-established way to extract the OID for an unambiguous JOIN. The proname and pronamespace conditions are technically redundant once the OID is matched, but they serve as harmless defensive guards. The version header change in the snapshot file (PostgreSQL 17.518.0) is cosmetic — normalizeSchemaOutput strips those lines before comparison.

Confidence Score: 5/5

  • Safe to merge — targeted, correct fix with comprehensive test coverage and no side-effects on non-overloaded functions.
  • The OID-based join via specific_name is the canonical PostgreSQL approach to disambiguating overloaded routines in information_schema.routines. All 4 affected queries are updated consistently in both the SQL source and the generated Go file, and the new mixed-language test scenario directly reproduces the reported bug. No regressions are introduced since the OID condition only tightens the existing name + namespace match.
  • No files require special attention.

Important Files Changed

Filename Overview
ir/queries/queries.sql Core SQL fix: adds AND p.oid = (regexp_match(r.specific_name, '_(\d+)$'))[1]::oid to the LEFT JOIN in all 4 queries (GetFunctions, GetProcedures, GetFunctionsForSchema, GetProceduresForSchema), correctly resolving the cross-join that assigned wrong LANGUAGEs to overloaded functions.
ir/queries/queries.sql.go Auto-generated Go file kept in sync with queries.sql; all 4 affected queries consistently updated with the OID-matching join condition.
cmd/dump/dump_integration_test.go Adds TestDumpCommand_Issue191FunctionProcedureOverload as the first proper test function for the issue_191 test data directory; the test infrastructure (runExactMatchTest / normalizeSchemaOutput) strips version headers so the snapshot is version-agnostic.
testdata/dump/issue_191_function_procedure_overload/pgschema.sql Expected pgschema dump snapshot updated with both provide_tx overloads using their correct LANGUAGE (sql vs plpgsql); version header change (17.5→18.0) is cosmetic and stripped at comparison time.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[information_schema.routines row\ne.g. provide_tx with specific_name='provide_tx_12345'] --> B{JOIN pg_proc}

    subgraph OLD["Before fix (cross-join)"]
        B1["ON proname = routine_name\nAND pronamespace = schema_oid"]
        B1 --> C1["Matches ALL overloads\nwith same name"]
        C1 --> D1["provide_tx_12345 gets LANGUAGE plpgsql\n(from wrong overload)"]
    end

    subgraph NEW["After fix (OID-pinned)"]
        B2["ON proname = routine_name\nAND pronamespace = schema_oid\nAND oid = extract_oid(specific_name)"]
        B2 --> C2["Matches EXACTLY ONE pg_proc row\nvia OID from specific_name"]
        C2 --> D2["provide_tx_12345 gets LANGUAGE sql ✓\nprovide_tx_67890 gets LANGUAGE plpgsql ✓"]
    end

    B --> B1
    B --> B2

    E["specific_name format\n'provide_tx_12345'\n→ regexp_match('_(\d+)$')\n→ OID 12345\n→ cast to ::oid"] --> B2
Loading

Reviews (1): Last reviewed commit: "fix: correct LANGUAGE for overloaded fun..." | Re-trigger Greptile

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 routine introspection so overloaded functions/procedures are joined to the correct pg_proc row, preventing mixed-language overloads from being dumped with an incorrect LANGUAGE (fixing #368) and expanding coverage in the existing overload dump fixture (#191).

Changes:

  • Updates the information_schema.routinespg_proc join to additionally match the routine OID derived from r.specific_name (applied to 4 queries).
  • Extends the issue_191_function_procedure_overload dump fixture to include a same-name overload pair with different languages (sql vs plpgsql).
  • Adds an integration test entry to run the issue_191_function_procedure_overload exact-match dump fixture.

Reviewed changes

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

Show a summary per file
File Description
ir/queries/queries.sql Fixes routine ↔ pg_proc joining by uniquely matching on OID extracted from specific_name, preventing overload cross-joins.
ir/queries/queries.sql.go Regenerates the sqlc output to reflect the updated SQL joins.
testdata/dump/issue_191_function_procedure_overload/raw.sql Adds mixed-language overload DDL to reproduce/cover #368 in the existing fixture.
testdata/dump/issue_191_function_procedure_overload/pgschema.sql Updates expected pgschema dump output to include both overloads with correct LANGUAGE.
testdata/dump/issue_191_function_procedure_overload/pgdump.sql Updates pg_dump input fixture to include the new overloads.
testdata/dump/issue_191_function_procedure_overload/manifest.json Updates fixture metadata to reflect covering both #191 and #368.
cmd/dump/dump_integration_test.go Adds an integration test function to execute the updated overload fixture.

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

@tianzhou tianzhou merged commit 113704a into main Mar 24, 2026
6 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.

dump emits wrong LANGUAGE for overloaded functions (plpgsql dumped as sql)

2 participants