Views that call functions are not ordered after those functions

Open
#284 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
go, postgresql
Domain
databases

Research direction

Start with GetViews and view_sql_generator.go, then inspect the View struct and the existing TableDependencies flow. Use the supplied function-and-view reproducer to verify dependency discovery and ordering. Done means the function is created before the view in the generated plan, without the current PostgreSQL error.

Written by the indexing model from the issue text.

Description

Views that call functions are not ordered after those functions

pg-schema-diff version

v1.0.5

Problem

A view definition can call a function:

CREATE FUNCTION fn_compute(id integer) RETURNS TABLE(result text) ...;
CREATE VIEW v_computed AS SELECT * FROM fn_compute(42);

pg-schema-diff does not track this dependency. The view may be created before the function, causing:

ERROR: function fn_compute(integer) does not exist (SQLSTATE 42883)

Root cause

The view vertex generator (view_sql_generator.go) only wires dependencies on tables (via TableDependencies). It has no awareness of function dependencies.

PostgreSQL does track view→function dependencies in pg_depend (via the view's rewrite rule), so the information is available:

SELECT pg_proc.proname, proc_ns.nspname,
       pg_catalog.pg_get_function_identity_arguments(pg_proc.oid)
FROM pg_catalog.pg_depend AS d
JOIN pg_catalog.pg_rewrite AS r ON d.objid = r.oid AND r.ev_class = <view_oid>
JOIN pg_catalog.pg_depend AS d2 ON r.oid = d2.objid
JOIN pg_catalog.pg_proc ON d2.refobjid = pg_proc.oid AND d2.refclassid = 'pg_proc'::REGCLASS
JOIN pg_catalog.pg_namespace AS proc_ns ON pg_proc.pronamespace = proc_ns.oid
WHERE d.refobjid = <view_oid> AND d2.deptype = 'n'
  AND proc_ns.nspname NOT IN ('pg_catalog', 'information_schema');

Suggested fix

  1. Add a function_dependencies column to GetViews (similar query to above)
  2. Add DependsOnFunctions []SchemaQualifiedName to the View struct
  3. In view_sql_generator.go Add(), wire: mustRun(addVertexId).after(buildFunctionVertexId(f, diffTypeAddAlter))

Reproducer

CREATE FUNCTION public.fn_helper() RETURNS TABLE(x int) LANGUAGE sql AS $$ SELECT 1; $$;
CREATE VIEW public.v_uses_fn AS SELECT * FROM public.fn_helper();

Plan from empty → target: view is ordered before function → apply fails.

Related

  • #248 (function dependency ordering)
Dominant language
Go
Stars
884
Forks
82
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stripe/pg-schema-diff

All issues in stripe/pg-schema-diff

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.