fix(pg-meta): pair composite FK columns positionally to avoid cartesi…#317
Open
anp0429 wants to merge 1 commit into
Open
fix(pg-meta): pair composite FK columns positionally to avoid cartesi…#317anp0429 wants to merge 1 commit into
anp0429 wants to merge 1 commit into
Conversation
…an product The foreign-key subquery in tables.sql joined source and target columns independently (attnum = any(conkey) with attnum = any(confkey)), producing the cartesian product of a composite key's columns. An N-column foreign key yielded N^2 relationship rows, reporting column pairings that do not exist in the schema. This surfaces in list_tables (verbose) foreign_key_constraints. Pair the columns positionally with unnest(conkey, confkey) with ordinality so column i maps only to column i. Adds a regression test.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What kind of change does this PR introduce?
Bug fix : data-integrity issue in
list_tables(verbose) foreign-key output.What is the current behavior?
For any composite (multi-column) foreign key,
list_tables(verbose) returns the cartesian product of the source and target columns. An N-column FK produces N²foreign_key_constraintsentries instead of N - reporting column pairings that do not exist in the schema.Repro:
list_tableswithverbose: truereturns 4 constraints forchild_parent_fk:Only
a=>aandb=>bare real. Because this tool exists so an AI agent can reason about database structure, the fabricated relationships are silently trusted - an agent could infer key relationships that don't exist.Root cause is in
pg-meta/tables.sql. The FK subquery joins source and target columns independently:any(conkey)againstany(confkey)cross-joins every source column with every target column, instead of pairing them positionally.What is the new behavior?
The two arrays are walked in lockstep with
unnest(conkey, confkey) with ordinality, so column i pairs only with column i. The example above now returns exactly the two real pairs. Adds a regression test asserting the exact set of column pairs for a composite FK.Additional context
The same
pg-metaquery backs the schema-docs work in #278, so this fix helps that path as well.