Conversation
Add a new sqlc query to retrieve function-to-function dependencies from PostgreSQL's pg_depend system catalog. This query identifies functions that depend on other functions (deptype='n' for normal dependencies), which is essential for topologically sorting functions so that dependencies are created before dependents. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ames Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Replace alphabetical sorting with topological sorting in both generateCreateFunctionsSQL and generateDropFunctionsSQL. This ensures functions are ordered so dependencies come before dependents. Also fixes function dependency normalization in plan.go - the Dependencies field on ir.Function now gets normalized when replacing temporary schema names with target schema names, enabling proper dependency lookup during topological sorting. Updates test case to use SQL-standard function with DEFAULT parameter that references another function, which PostgreSQL tracks via pg_depend and is idempotent. Fixes #246 - functions that reference other functions are now ordered correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Updates expected output files for drop_function test case to reflect the new reverse topological ordering (dependents dropped before dependencies). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes issue #246 where functions that reference other functions through dependencies (e.g., default parameter values) were not being dumped in the correct order, causing errors when the schema was applied.
Changes:
- Added function-to-function dependency tracking via PostgreSQL's
pg_dependsystem catalog - Implemented topological sorting for function creation and drop operations to respect dependencies
- Added test coverage for the function dependency ordering scenario
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
ir/queries/queries.sql |
Added GetFunctionDependencies query to fetch function-to-function dependencies from pg_depend |
ir/queries/queries.sql.go |
Generated code from the new SQL query |
ir/ir.go |
Added Dependencies field to Function struct to store function dependency keys |
ir/inspector.go |
Added buildFunctionDependencies function to populate function dependencies from the database |
internal/diff/topological.go |
Implemented topologicallySortFunctions using Kahn's algorithm for dependency ordering |
internal/diff/function.go |
Modified create/drop function generation to use topological sort instead of alphabetical sort |
cmd/plan/plan.go |
Added normalization of function dependencies during schema name replacement |
testdata/diff/dependency/function_to_function/* |
New test case demonstrating the fix for the reported issue |
testdata/diff/create_function/drop_function/* |
Updated test expectations reflecting new drop order (reverse topological) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Addresses PR review feedback - adds documentation consistent with topologicallySortTables and topologicallySortTypes explaining why cycle breaking is safe for mutually recursive functions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
alecthomas
pushed a commit
to alecthomas/pgschema
that referenced
this pull request
Jan 26, 2026
* chore: test case * feat(ir): add GetFunctionDependencies query for pg_depend lookup Add a new sqlc query to retrieve function-to-function dependencies from PostgreSQL's pg_depend system catalog. This query identifies functions that depend on other functions (deptype='n' for normal dependencies), which is essential for topologically sorting functions so that dependencies are created before dependents. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(ir): add Dependencies field to Function struct * feat(ir): populate function dependencies from pg_depend Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(ir): use pg_get_function_identity_arguments for normalized type names Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat(diff): add topologicallySortFunctions for dependency ordering Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(diff): use topological sort for function ordering Replace alphabetical sorting with topological sorting in both generateCreateFunctionsSQL and generateDropFunctionsSQL. This ensures functions are ordered so dependencies come before dependents. Also fixes function dependency normalization in plan.go - the Dependencies field on ir.Function now gets normalized when replacing temporary schema names with target schema names, enabling proper dependency lookup during topological sorting. Updates test case to use SQL-standard function with DEFAULT parameter that references another function, which PostgreSQL tracks via pg_depend and is idempotent. Fixes pgplex#246 - functions that reference other functions are now ordered correctly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test: update expected output for topological function ordering Updates expected output files for drop_function test case to reflect the new reverse topological ordering (dependents dropped before dependencies). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs: add detailed cycle-breaking comment to topologicallySortFunctions Addresses PR review feedback - adds documentation consistent with topologicallySortTables and topologicallySortTypes explaining why cycle breaking is safe for mutually recursive functions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #246