Skip to content

fix(pg-meta): pair composite FK columns positionally to avoid cartesi… - #317

Merged
gregnr merged 4 commits into
supabase:mainfrom
anp0429:fix/composite-fk-cartesian-product
Jul 17, 2026
Merged

gregnr merged 4 commits into
supabase:mainfrom
anp0429:fix/composite-fk-cartesian-product

Conversation

@anp0429

@anp0429 anp0429 commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

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_constraints entries instead of N : reporting column pairings that do not exist in the schema.

Repro:

create table public.parent (a int not null, b int not null, primary key (a, b));
create table public.child (
  a int not null, b int not null,
  constraint child_parent_fk foreign key (a, b) references public.parent (a, b)
);

list_tables with verbose: true returns 4 constraints for child_parent_fk:

public.child.a => public.parent.a
public.child.a => public.parent.b   <- does not exist
public.child.b => public.parent.a   <- does not exist
public.child.b => public.parent.b

Only a=>a and b=>b are 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:

... on sa.attrelid = c.conrelid  and sa.attnum = any (c.conkey)
... on ta.attrelid = c.confrelid and ta.attnum = any (c.confkey)

any(conkey) against any(confkey) cross-joins every source column with every target column, instead of pairing them positionally.

What is the new behavior?

Each foreign key is now returned as one constraint object with the grouped shape discussed in the review below:

{
  "name": "child_parent_fk",
  "source_table": "public.child",
  "source_columns": ["a", "b"],
  "target_table": "public.parent",
  "target_columns": ["a", "b"]
}
  • Columns are walked in lockstep with unnest(conkey, confkey) with ordinality, so wrong pairings are impossible by construction, and both arrays aggregate ordered by constraint ordinality - source_columns[i] pairs with target_columns[i], in constraint-definition order (not attnum, not alphabetical).
  • source_table is kept in the output because relationships includes constraints where the listed table is the referenced target.
  • Single-column FKs are emitted as one-element arrays for a uniform shape.
  • Adds a regression test using deliberately non-alphabetical column order (foreign key (b, a) references parent (y, x)) so any future regression to attnum or name ordering fails immediately.

Additional context

The same pg-meta query backs the schema-docs work in #278, so this fix helps that path as well.

@anp0429

anp0429 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

Hi @gregnr : no rush on this, but I wanted to check whether you see this as a real issue before it goes further. The short version: for composite foreign keys, list_tables (verbose) returns the cartesian product of columns (N² pairings instead of N), so it reports column relationships that don't exist in the schema , which an AI agent could silently trust. There's a minimal repro in the PR. I might be missing context on why it works this way, so I'd genuinely value your read: is this a bug worth fixing, or intended behavior? Happy to adjust or close if I've misunderstood something.

@gregnr gregnr left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anp0429 thanks for the PR and the ping! I just tried this and repro'd the bug on my end - nice find. The fix looks good, though one thing I noticed - for a composite FK, foreign_key_constraints is outputting one row per column pair vs a single row that represents captures both columns. If we keep it as is, nothing really signals that 2 rows belong to one atomic constraint vs 2 independent FKs.

I think we'll likely want to group within a single constraint instead - something like:

{
  "name": "child_parent_fk",
  "source_columns": ["a", "b"],
  "target_table": "public.parent",
  "target_columns": ["a", "b"]
}

so that the composite is explicit. What do you think?

@anp0429

anp0429 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

@gregnr Yeah, agreed : grouping is the right call. The per-pair rows kind of have the same problem as the original bug, just one level up: nothing tells you those two rows are one atomic constraint.
One thing I want to be careful with in the SQL: the aggregated arrays need to be ordered by the constraint's column position, not attnum or name, so the source/target pairing stays positional. I'll add a test with non-alphabetical column order to lock that in.
Quick question : do you want single-column FKs moved to the array shape too for uniformity, or should only composites change? Fine either way, will update the PR once I know.

anp8729 added 2 commits July 16, 2026 15:09
…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.
@anp0429
anp0429 force-pushed the fix/composite-fk-cartesian-product branch from c47537d to d17cdfb Compare July 16, 2026 19:54
@anp0429
anp0429 requested a review from a team as a code owner July 16, 2026 19:54
@anp0429

anp0429 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

@gregnr Went ahead and pushed the grouped shape so there's something concrete to review - went with unnest(conkey, confkey) with ordinality so the positional pairing is structural, plus the non-alphabetical column-order test mentioned above. For the single-column question I defaulted to one-element arrays for a uniform shape, but it's a two-line change either way if you'd rather keep those as-is.

@anp0429

anp0429 commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

To sanity-check the grouping change beyond the committed tests, I ran an automated review over this branch : an LLM proposes edge-case tests, then a deterministic gate actually executes each one against the code (no model in the pass/fail path). 9 additional cases, all passing:

  • constraint-definition order preserved even when it differs from the declared/physical column order
  • two independent composite FKs between the same table pair stay separate (no over-merging)
  • self-referential composite FKs reported once per side
  • cross-schema FK visibility from either table's schema
  • single-column FKs correctly emitted as one-element arrays
  • repeated verbose calls return identical FK output

0 gaps found. Happy to commit any of these as tests if you want the coverage in the suite.
(Still open from above: keep single-column FKs as one-element arrays for uniformity, or preserve their old shape? Defaulted to arrays, two-line change either way.)

@gregnr

gregnr commented Jul 17, 2026

Copy link
Copy Markdown
Member

@anp0429 this is great work, nice job. All of those additional test scenarios look valuable - please commit them if you can! I'll merge this once those are in. We'll keep it as you have it where the FK columns are always arrays, even for non-composite FKs.

…-PK unique ref, multi-FK, 3-column, single-column arrays)
@anp0429

anp0429 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Done, added all of those: self-referential, cross-schema, non-PK unique reference, multiple FKs between the same tables, three-column ordering, and single-column arrays. Full suite green (110/110). Thanks for the review, glad this is landing.

@anp0429

anp0429 commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the Biome formatting. The remaining failures look unrelated to this PR, the e2e tests are failing on x-api-key header is required (missing API key in CI), and the Management API types check is flagging pre-existing upstream type drift. My unit tests (including the new FK ones) pass. Let me know if you'd like me to do anything else.

@gregnr
gregnr merged commit 10af00b into supabase:main Jul 17, 2026
3 of 6 checks passed
@gregnr

gregnr commented Jul 17, 2026

Copy link
Copy Markdown
Member

Merged. Thanks again @anp0429!

@supabase-releaser supabase-releaser Bot mentioned this pull request Jul 17, 2026
@anp0429

anp0429 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @gregnr, glad it was helpful. Enjoyed working through this one.

Rodriguespn pushed a commit that referenced this pull request Aug 10, 2026
🤖 I have created a release *beep* *boop*
---


<details><summary>mcp-utils: 0.6.0</summary>

##
[0.6.0](mcp-utils-v0.5.1...mcp-utils-v0.6.0)
(2026-08-10)


### Features

* hide tools from tools/list
([#334](#334))
([d80471a](d80471a))
</details>

<details><summary>mcp-server-supabase: 0.10.0</summary>

##
[0.10.0](mcp-server-supabase-v0.9.0...mcp-server-supabase-v0.10.0)
(2026-08-10)


### ⚠ BREAKING CHANGES

* add query_logs tool for custom log queries
([#333](#333))

### Features

* add --content-api-url flag and SUPABASE_CONTENT_API_URL env var
([#343](#343))
([6fcaaa3](6fcaaa3))
* add query_logs tool for custom log queries
([#333](#333))
([798806b](798806b))
* hide tools from tools/list
([#334](#334))
([d80471a](d80471a))


### Bug Fixes

* hide read-only mode
([#349](#349))
([5cda067](5cda067))
* **pg-meta:** pair composite FK columns positionally to avoid cartesi…
([#317](#317))
([10af00b](10af00b))
* select query_logs dialect via logsDialect
([#357](#357))
([80ff453](80ff453))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
mattrossman added a commit that referenced this pull request Oct 26, 2026
…NCLUDE columns (#382)

Closes #381

### What kind of change does this PR introduce?

Bug fix: data-integrity issue in `list_tables` (verbose) primary key
output.

### What is the current behavior?

`primary_keys` is sorted by each column's physical position in the table
rather than its position in the key, and it includes the non-key
`INCLUDE` payload columns of the backing index.

```sql
create table membership (
  org_id  int not null,          -- physically column 1
  user_id int not null,          -- physically column 2
  role    text,
  primary key (user_id, org_id)
);

create table inc (a int not null, b int not null, c int not null);
create unique index inc_pk_idx on inc (a) include (b);
alter table inc add constraint inc_pk primary key using index inc_pk_idx;
```

| table | declared | reported |
|---|---|---|
| `membership` | `(user_id, org_id)` | `["org_id","user_id"]` |
| `inc` | `(a) INCLUDE (b)` | `["a","b"]` |

Root cause in `pg-meta/tables.sql`: `a.attnum = any (i.indkey)` is a
set-membership test, so each column's position within the key is
discarded and the enclosing `jsonb_agg` has no `order by`. `indkey` also
spans `indnatts`, which includes the `INCLUDE` payload, rather than
`indnkeyatts`.

This is the same defect #317 fixed in the foreign key subquery
immediately below, which walks columns with `unnest(...) with
ordinality` and aggregates `order by cols.ord`. The primary key subquery
was not updated at the time.

### What is the new behavior?

- `indkey` is unnested `with ordinality` and the aggregate is ordered by
that ordinal, so columns come back in constraint-definition order - not
attnum, not alphabetical.
- The unnest is bounded by `indnkeyatts`, so `INCLUDE` payload columns
are excluded.
- The implicit comma joins become explicit joins, which is what makes
the lateral unnest readable. Output shape is unchanged: `to_jsonb(_pk) -
'ord'` emits exactly the same keys as before.
- A comment records why `= any (indkey)` is wrong, so it doesn't get
"simplified" back.

Two regression tests, both using a key order that is deliberately the
reverse of the physical column order so any regression to attnum
ordering fails immediately - the same shape as the FK ordering test
added in #317.

### Verification

Windows 11, Node 22 LTS, pnpm 10:

```
pnpm run build                            -> success
CI=true pnpm vitest run --project unit    -> 212 passed | 5 failed (217)
pnpm typecheck                            -> clean
npx @biomejs/[email protected] ci .             -> Checked 89 files. No fixes applied.
```

Both new tests pass:

```
✓ tools > composite primary key preserves constraint-definition column order
✓ tools > primary key excludes non-key INCLUDE columns
```

**The 5 failures are pre-existing and unrelated to this change.** They
are all `normalizeFilename`: `edge-function.ts` imports `resolve` from
`node:path`, which is platform-dispatched, but strips a hardcoded POSIX
prefix (`/tmp/user_fn_.../`), so on Windows the strip never matches and
the full `C:\tmp\...` path is returned. 3 direct failures in
`edge-function.test.ts` plus 2 downstream in `server.test.ts` (`list
edge functions`, `get edge function`). I confirmed the identical 5
failures on clean `upstream/main` with nothing applied, so they are the
baseline, not a regression. Happy to file that separately -
`node:path/posix` looks like the one-line fix, and every caller already
forces POSIX semantics with `fileURLToPath(..., { windows: false })`.

End to end against a real MCP client and server
(`createSupabaseMcpServer` over `StreamTransport`, PostgreSQL 16.4 via
PGlite), calling `list_tables` with `verbose: true`:

```
before   membership  declared (user_id, org_id)   ->  ["org_id","user_id"]
         triple      declared (r, q, p)           ->  ["p","q","r"]
         inc         declared (a) INCLUDE (b)     ->  ["a","b"]

after    membership                               ->  ["user_id","org_id"]
         triple                                   ->  ["r","q","p"]
         inc                                      ->  ["a"]
```

Single-column primary keys and the composite foreign key output from
#317 are unchanged on both builds.

### On whether this actually misleads a model

I tested it rather than asserting it, and the result is mostly negative,
so it's worth stating plainly: across 18 runs (3 models × 3 questions ×
before/after, one replicate per cell) **17 answered correctly on both
builds**. Models generally did not trust `primary_keys` - 17 of 18 runs
called `execute_sql` and read `pg_index`/`pg_constraint` directly. Two
runs spontaneously flagged the output as wrong, e.g. *"list_tables
reports the primary key as ["a","b"], which is misleading: it lumps the
INCLUDE column in with the real key column."*

The one failure was a weaker model on "what's the primary key on
membership?" against the unfixed build, which answered `org_id,
user_id`. Notably its own verification query used
`information_schema.key_column_usage` without `order by
ordinal_position`, so it confirmed the wrong answer.

So the case for this fix is not "agents give wrong answers". It's that
`primary_keys` contradicts the database, that models are paying for a
catalog round-trip `list_tables` exists to save, and that a non-agent
consumer of the published package has no fallback at all.

### What I did not test

- Only against PGlite (PostgreSQL 16.4) and the repo's test harness, not
a live Supabase project. `indnkeyatts` requires PG 11+.
- One replicate per cell in the model matrix, and those runners were
agents with shell access, which lean toward verifying - a plain chat
client may lean on `list_tables` more than these did.

---------

Co-authored-by: Matt Rossman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants