-
Notifications
You must be signed in to change notification settings - Fork 5
Refuse foreign tables, views, and partitioned tables with foreign partitions with a clear message #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mason-sharp
wants to merge
7
commits into
main
Choose a base branch
from
fix/ACE-207/relkind-precheck
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refuse foreign tables, views, and partitioned tables with foreign partitions with a clear message #159
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f6c414
feat(queries): read a table's inheritance tree in one recursive query
mason-sharp d4a079a
feat(mtree): refuse foreign tables and parents with foreign children
mason-sharp 8d9df2e
feat(schema-diff): report skipped foreign tables; skip them in repset…
mason-sharp 5246b64
feat: refuse foreign tables, views, and partitioned tables with forei…
mason-sharp bdc1950
ci: run query, diff, and repair unit tests
mason-sharp 565de7b
fix: name every non-table relation kind, and say why a foreign table …
mason-sharp 606f08a
fix(repset-diff): leave out relations ACE cannot compare instead of f…
mason-sharp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| // /////////////////////////////////////////////////////////////////////////// | ||
| // | ||
| // # ACE - Active Consistency Engine | ||
| // | ||
| // Copyright (C) 2023 - 2026, pgEdge (https://www.pgedge.com/) | ||
| // | ||
| // This software is released under the PostgreSQL License: | ||
| // https://opensource.org/license/postgresql | ||
| // | ||
| // /////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| package queries | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // RelationInfo is one relation in an inheritance tree. | ||
| type RelationInfo struct { | ||
| Schema string | ||
| Name string | ||
| RelKind string // r = heap, p = partitioned, f = foreign | ||
| Depth int // 0 for the table the tree was built from | ||
| Parent string // qualified parent name, "" for the root | ||
| } | ||
|
|
||
| // Qualified returns schema.name without quoting. | ||
| func (r RelationInfo) Qualified() string { | ||
| return r.Schema + "." + r.Name | ||
| } | ||
|
|
||
| // RelationTree is a table together with every relation that inherits from it, | ||
| // directly or through intermediate parents. | ||
| type RelationTree struct { | ||
| Root RelationInfo | ||
| Descendants []RelationInfo // ordered by depth, then name | ||
| } | ||
|
|
||
| // HasForeign reports whether the root or any descendant is a foreign table. | ||
| func (t *RelationTree) HasForeign() bool { | ||
| if t.Root.RelKind == "f" { | ||
| return true | ||
| } | ||
| for _, d := range t.Descendants { | ||
| if d.RelKind == "f" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // ForeignRelations lists the qualified names of every foreign relation in | ||
| // the tree, root first, then in tree order. | ||
| func (t *RelationTree) ForeignRelations() []string { | ||
| var out []string | ||
| if t.Root.RelKind == "f" { | ||
| out = append(out, t.Root.Qualified()) | ||
| } | ||
| for _, d := range t.Descendants { | ||
| if d.RelKind == "f" { | ||
| out = append(out, d.Qualified()) | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // HeapLeaves lists the relations that actually store rows: the root if it is | ||
| // a heap table, then every heap descendant. Partitioned relations hold no | ||
| // rows and are skipped; foreign relations are skipped. | ||
| func (t *RelationTree) HeapLeaves() []RelationInfo { | ||
| var out []RelationInfo | ||
| if t.Root.RelKind == "r" { | ||
| out = append(out, t.Root) | ||
| } | ||
| for _, d := range t.Descendants { | ||
| if d.RelKind == "r" { | ||
| out = append(out, d) | ||
| } | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // IsInherited reports whether anything inherits from the root. | ||
| func (t *RelationTree) IsInherited() bool { | ||
| return len(t.Descendants) > 0 | ||
| } | ||
|
|
||
| // GetRelationTree runs the recursive pg_inherits query for schema.table. | ||
| // It returns nil, nil when the table does not exist. A relation reachable | ||
| // through more than one parent (multiple inheritance) is listed once. | ||
| func GetRelationTree(ctx context.Context, db DBQuerier, schema, table string) (*RelationTree, error) { | ||
| sql, err := RenderSQL(SQLTemplates.GetRelationTree, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to render GetRelationTree SQL: %w", err) | ||
| } | ||
| // The query text is a fixed template; schema and table travel as bind | ||
| // parameters, so nothing from the caller is spliced into the SQL. | ||
| rows, err := db.Query(ctx, sql, schema, table) // nosemgrep | ||
| if err != nil { | ||
| return nil, fmt.Errorf("query to get relation tree for %s.%s failed: %w", schema, table, err) | ||
| } | ||
| defer rows.Close() | ||
|
|
||
| var relations []RelationInfo | ||
| for rows.Next() { | ||
| var r RelationInfo | ||
| if err := rows.Scan(&r.Schema, &r.Name, &r.RelKind, &r.Depth, &r.Parent); err != nil { | ||
| return nil, fmt.Errorf("failed to scan relation tree row: %w", err) | ||
| } | ||
| relations = append(relations, r) | ||
| } | ||
| if err := rows.Err(); err != nil { | ||
| return nil, fmt.Errorf("error iterating relation tree rows: %w", err) | ||
| } | ||
| tree, err := buildRelationTree(relations) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("relation tree for %s.%s: %w", schema, table, err) | ||
| } | ||
| return tree, nil | ||
| } | ||
|
|
||
| // relationKey identifies a relation by its separate schema and name so that | ||
| // "a"."b.c" and "a.b"."c" never collide the way their dotted rendering does. | ||
| type relationKey struct { | ||
| schema string | ||
| name string | ||
| } | ||
|
|
||
| // buildRelationTree folds the query's rows, ordered by depth with the root | ||
| // first, into a tree. A relation that appears more than once (reachable | ||
| // through several parents) is kept once. It returns nil, nil for no rows. | ||
| func buildRelationTree(relations []RelationInfo) (*RelationTree, error) { | ||
| var tree *RelationTree | ||
| seen := make(map[relationKey]bool) | ||
| for _, r := range relations { | ||
| key := relationKey{schema: r.Schema, name: r.Name} | ||
| if r.Depth == 0 { | ||
| tree = &RelationTree{Root: r} | ||
| seen[key] = true | ||
| continue | ||
| } | ||
| if tree == nil { | ||
| return nil, fmt.Errorf("has a descendant before its root") | ||
| } | ||
| if seen[key] { | ||
| continue | ||
| } | ||
| seen[key] = true | ||
| tree.Descendants = append(tree.Descendants, r) | ||
| } | ||
| return tree, nil | ||
| } | ||
|
|
||
| // UnsupportedReason says why ACE cannot compare the tree's root relation, or | ||
| // returns "" for a heap or partitioned table whose tree holds no foreign | ||
| // relations. Only relkinds r and p are comparable; every other kind gets a | ||
| // message naming what it is. The text reads as a predicate on the table | ||
| // name, e.g. "'s.t' is a foreign table; ...". | ||
| func (t *RelationTree) UnsupportedReason() string { | ||
| switch t.Root.RelKind { | ||
| case "r", "p": | ||
| // comparable; fall through to the foreign-descendant check below | ||
| case "f": | ||
| return "is a foreign table; its rows live outside PostgreSQL and it cannot have a primary key, so ACE has nothing to compare" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| case "v", "m": | ||
| return "is a view; ACE compares tables" | ||
| case "S": | ||
| return "is a sequence; ACE compares tables" | ||
| case "c": | ||
| return "is a composite type; ACE compares tables" | ||
| case "i", "I": | ||
| return "is an index; ACE compares tables" | ||
| case "t": | ||
| return "is a TOAST table; ACE compares tables" | ||
| default: | ||
| return fmt.Sprintf("is not a table (relkind %q); ACE compares tables", t.Root.RelKind) | ||
| } | ||
| if t.HasForeign() { | ||
| return fmt.Sprintf("has foreign relations in its inheritance tree (%s); ACE does not yet compare tables with foreign children or partitions", | ||
| strings.Join(t.ForeignRelations(), ", ")) | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // HotTableHint looks for a table named "_<table>" next to a view. Some | ||
| // tiering extensions, coldfront among them, rename the real table that way | ||
| // and put a view in its place, so the underscore table may be the data the | ||
| // user meant to compare. When found, and ACE could compare it, it returns a | ||
| // sentence mentioning that table; otherwise "". | ||
| func HotTableHint(ctx context.Context, db DBQuerier, schema, table string) (string, error) { | ||
| hot, err := GetRelationTree(ctx, db, schema, "_"+table) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return hotTableHintText(schema, table, hot), nil | ||
| } | ||
|
|
||
| // hotTableHintText renders the hint for a candidate hot table, or "" when | ||
| // there is none or ACE would refuse it anyway. | ||
| func hotTableHintText(schema, table string, hot *RelationTree) string { | ||
| if hot == nil || (hot.Root.RelKind != "r" && hot.Root.RelKind != "p") || hot.UnsupportedReason() != "" { | ||
| return "" | ||
| } | ||
| return fmt.Sprintf(" A table named '%s' also exists. It may be the table behind this view (tiering extensions such as coldfront use this layout); if so, compare '%s' instead.", | ||
| hot.Root.Qualified(), hot.Root.Qualified()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Keep qualified identifier components separate.
String()makes dotted identifiers ambiguous. For example,Schema="ops.eu", Name="events"andSchema="ops", Name="eu.events"both produceops.eu.events.repset-diffuses this value as a relation key and passes it totable-diff. It can merge distinct relations or select the wrong relation. KeepQualifiedNamethrough the downstream boundary, or use a canonical quoted representation that every consumer can parse correctly.🤖 Prompt for AI Agents