View dependency query produces false positives and misses view-to-view dependencies
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
Research direction
Start in internal/queries/queries.sql at the GetViews query and its table_dependencies subquery. Run the issue's t1/v1/v2 reproducer through pg-schema-diff plan, then verify the generated order is t1 → v1 → v2 without a false cycle.
Written by the indexing model from the issue text.
Description
View dependency query produces false positives and misses view-to-view dependencies
pg-schema-diff version
v1.0.5
Problem
The GetViews query in internal/queries/queries.sql has two issues in its table_dependencies subquery:
1. Missing view-to-view dependencies
The join filter dep_c.relkind IN ('r', 'p') excludes views ('v'). Views that depend on other views (e.g. CREATE VIEW a AS SELECT * FROM other_view) have no dependency edge, so they can be ordered before the view they reference.
Fix: Change to dep_c.relkind IN ('r', 'p', 'v') and add dep_c.oid != c.oid to exclude self-references (every view's rewrite rule has a dependency on itself).
2. False-positive dependencies from other views' rewrite rules
The join path:
FROM pg_catalog.pg_depend AS d
INNER JOIN pg_catalog.pg_rewrite AS r ON d.objid = r.oid
INNER JOIN pg_catalog.pg_depend AS d2 ON r.oid = d2.objid
...
WHERE d.refobjid = c.oid
This finds rewrite rules that reference the current view (d.refobjid = c.oid), then finds what those rules depend on. But OTHER views' rewrite rules also reference this view (if they SELECT from it). This causes view A to appear to depend on view B's dependencies, creating false cycles.
Fix: Add AND r.ev_class = c.oid to the pg_rewrite join to restrict to the view's own rewrite rule only.
Reproducer
CREATE TABLE t1 (id int PRIMARY KEY, val text);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE VIEW v2 AS SELECT * FROM v1; -- v2 depends on v1
Running pg-schema-diff plan --from-dsn empty --to-dsn target:
- Without fix: v2 may be ordered before v1 (missing dep), or cycle detected (false positive from other rules)
- With fix: correct order t1 → v1 → v2
Related
- #248 (function composite type ordering — different issue but same dependency-tracking gap)
- 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 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 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
stripe/pg-schema-diff#282 ·
All issues in stripe/pg-schema-diff
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
enhancement needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
kind/cleanup
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
kubernetes-sigs/kueue#15947 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
sympozium-ai/sympozium#627 ·
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·