Skip to content

Conversation

@tianzhou
Copy link
Contributor

Fix #246

tianzhou and others added 8 commits January 15, 2026 09:39
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>
Copilot AI review requested due to automatic review settings January 16, 2026 06:16
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 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_depend system 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>
@tianzhou tianzhou merged commit 9adebbf into main Jan 16, 2026
2 checks passed
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>
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 which reference other functions are sometimes not dumped in the correct order

1 participant