[BUG] Functions with knowable dependencies are created before the tables they depend on
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start in function_sql_vertex_generator.go around lines 81-100, then compare the function dependency handling with GetViews and the View.TableDependencies field. Trace GetDependsOnFunctions and GetAddAlterDependencies to determine how table dependencies can be represented and used. Reproduce the issue with the provided schema.sql and verify that the generated plan creates JobRun before jobRunClaimNext.
Written by the indexing model from the issue text.
Description
Describe the bug
When pg-schema-diff generates a plan, functions defined with RETURNS SETOF "MyTable" are scheduled before the referenced table.
The dependency exists in PostgreSQL's pg_depend, so the information is available.
Expected behavior
pg-schema-diff should detect table dependencies referenced by functions and ensure that the functions are created after their dependencies.
To Reproduce
- Add the schema below to a migration file.
- Run pg-schema-diff (plan/apply) against an empty database.
- Observe
ERROR: relation "JobRun" does not existbecause the function runs before the table is created.
-- File: schema.sql
CREATE TABLE "JobRun" (
"runId" BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"type" TEXT NOT NULL,
"status" TEXT NOT NULL
);
CREATE OR REPLACE FUNCTION "jobRunClaimNext"(p_types TEXT[])
RETURNS SETOF "JobRun"
LANGUAGE sql
AS $$
SELECT r.*
FROM "JobRun" AS r
WHERE r."status" = 'PENDING'
AND r."type" = ANY (p_types)
LIMIT 1;
$$;
Context
pg-schema-diff version: v1.0.2
pg-schema-diff usage: CLI
Postgres version: 17
pg_dump of database: N/A
Possible solution plan
I've never touched a line of Go code, so I passed the above info to Claude, and here's a summary of what it says:
function_sql_vertex_generator.go: Lines 81-100:GetAddAlterDependenciesonly tracks function-to-function dependencies viaDependsOnFunctions- No mechanism to track table references in function bodies
- Query
pg_dependto find actual table dependencies (similar toGetViews) - Add a
TableDependenciesfield toschema.Function(likeView.TableDependencies) - Extend
GetDependsOnFunctionsquery or create a newGetDependsOnTablesquery - Use these dependencies in
GetAddAlterDependencies
Alternatively, if there were a way to impose order from the command line or via the naming of the .sql schemas, that would work too.
- Dominant language
- Go
- Stars
- 884
- Forks
- 82
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stripe/pg-schema-diff
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 76/100
stripe/pg-schema-diff#302 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
stripe/pg-schema-diff#283 · 1 reaction ·
-
Difficulty 4/5 3-5 days Newbie friendliness 50/100
stripe/pg-schema-diff#301 ·
-
Is the repo active? Open
Difficulty 1/5 Under an hour Newbie friendliness 15/100
stripe/pg-schema-diff#289 · 7 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 76/100
stripe/pg-schema-diff#284 ·
All issues in stripe/pg-schema-diff
Similar issues
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
blinklabs-io/bursa#904 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comments ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100