Skip to content

fix(pg-meta): order list_tables columns by ordinal position - #497

Merged
mattrossman merged 2 commits into
supabase:mainfrom
janakmandavgade:fix/list-tables-column-order
Oct 28, 2026
Merged

mattrossman merged 2 commits into
supabase:mainfrom
janakmandavgade:fix/list-tables-column-order

Conversation

@janakmandavgade

@janakmandavgade janakmandavgade commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

array_agg in coalesceRowsToArray followed heap scan order, which is not guaranteed. ALTER COLUMN rewrites a column's pg_attribute row and moves it to the end of the heap, so list_tables could return columns out of order. Add an optional orderBy argument to coalesceRowsToArray and use columns.ordinal_position when building the table columns.

Adds a test that alters columns and checks the order stays the same.

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

Fixes #496

list_tables (verbose) builds each table's columns with array_agg(row_to_json(columns)) and no ORDER BY, so the order follows pg_attribute scan order. ALTER COLUMN ... SET NOT NULL / SET DEFAULT / TYPE write a new pg_attribute row version at the end of the heap, so that column is returned last. ordinal_position is stripped from the output, so the agent can't recover the real order.

What is the new behavior?

Columns are returned in ordinal_position order. coalesceRowsToArray takes an optional orderBy, and listTablesSql passes columns.ordinal_position. When orderBy is omitted, the generated SQL is unchanged.

No change to tool names, descriptions, input/output schemas or output shape. This is a server-only fix, so the published tools/list contract is untouched.

How to Review

  1. Column aggregation

    • packages/mcp-server-supabase/src/pg-meta/index.ts
    • coalesceRowsToArray gains an optional orderBy placed inside array_agg(...) before FILTER. listTablesSql is its only caller.
  2. Tests

    • packages/mcp-server-supabase/src/server.database.test.ts
    • list tables returns columns in ordinal position order after ALTER COLUMN checks the observable column order from the list_tables tool after the ALTERs that reorder the heap.

Review questions

  • Is the public contract minimal and intentional?
  • Do the main execution paths preserve existing behavior?
  • Do failures stop safely?
  • Do tests protect observable behavior rather than implementation details?

Verification

  • Regression test without the fix fails:
    × tools > list tables returns columns in ordinal position order after ALTER COLUMN
    - Expected
    + Received
      Array [
    -   "id",
    -   "email",
        "name",
        "age",
    +   "email",
    +   "id",
      ]
    
  • With the fix it passes.
  • pnpm --filter @supabase/mcp-server-supabase test:unit --run: 34 files, 736 tests passed.
  • pnpm --filter @supabase/mcp-server-supabase test:integration --run (stdio, after a fresh build): 7/7 passed.
  • biome check, tsc --noEmit and the package build are clean.

I reviewed the diff and ran the tests above locally.

Additional context

Same class of issue as #381 / #382 (primary key column order).

BEGIN_COMMIT_OVERRIDE
fix(pg-meta): order list_tables columns by ordinal position
END_COMMIT_OVERRIDE

array_agg in coalesceRowsToArray followed heap scan order, which is not
guaranteed. ALTER COLUMN rewrites a column's pg_attribute row and moves
it to the end of the heap, so list_tables could return columns out of
order. Add an optional orderBy argument to coalesceRowsToArray and use
columns.ordinal_position when building the table columns.

Adds a test that alters columns and checks the order stays the same.
@janakmandavgade
janakmandavgade requested a review from a team as a code owner October 28, 2026 16:15
@mattrossman

Copy link
Copy Markdown
Collaborator

Thanks for the fix! I pushed a small comment cleanup and confirmed the regression test passes (the tests step is expected to fail in CI)

@mattrossman
mattrossman merged commit c242e63 into supabase:main Oct 28, 2026
4 of 5 checks passed
barryroodt pushed a commit that referenced this pull request Nov 4, 2026
…tables (#508)

Closes #507

## What kind of change does this PR introduce?

Bug fix, plus an optional `partition_of` output field.

## What is the current behavior?

`list_tables` reports the FK clones Postgres creates for partitions as
separate foreign keys, so one FK is listed once per partition. Nothing
in the output marks a partition's parent.

## What is the new behavior?

The relationships subquery filters `conparentid = 0`, the predicate
`pg_dump` uses, so each FK is reported once. FKs declared directly on a
partition are still reported.

Partitions carry `partition_of`, their immediate parent in the same
`schema.name` format as `name`, in both compact and verbose output:

```jsonc
{ "name": "public.events_2025", "rls_enabled": false, "rows": 0, "partition_of": "public.events" }
```

A sub-partition points to its own parent, not the root. Other tables
omit the field, including `INHERITS` children. A partition still names
its parent when the parent's schema isn't in `schemas`, as
`target_table` already does.

No change to tool names, descriptions or input schemas. `partition_of`
is optional in the output schema. Databases without partitioned tables
get identical output.

## How to Review

1. **Query**
   - `packages/mcp-server-supabase/src/pg-meta/tables.sql`
- One predicate in the relationships subquery. `conparentid` rather than
`relispartition`, which would also hide FKs declared on or pointing at a
partition.
- One scalar subquery for `partition_of`. It filters the parent on
`relkind = 'p'`, because `INHERITS` children also have `pg_inherits`
rows.

2. **Output**
- `packages/mcp-server-supabase/src/pg-meta/types.ts`,
`packages/mcp-server-supabase/src/tools/database-operation-tools.ts`
   - `partition_of` is omitted when null, like `comment`.

3. **Tests**
- `packages/mcp-server-supabase/src/server.database.test.ts`, next to
the composite-FK tests:
- Clones on both sides of an FK are dropped, while an FK declared on a
partition is kept.
- `partition_of` in both modes for a root, a partition, a sub-partition,
a partition in another schema, an `INHERITS` parent and its child.

**Review questions**

- [ ] Is the immediate parent, rather than the root, what you want in
`partition_of`?
- [ ] Is the public contract minimal and intentional?
- [ ] Do the main execution paths preserve existing behavior?
- [ ] Do tests protect observable behavior rather than implementation
details?

## Verification

Without the fix, the FK test fails (trimmed):

```
× tools > partition clones of an FK are not reported as separate constraints
AssertionError: expected [ { …(5) }, { …(5) }, { …(5) } ] to deeply equal [ { …(5) } ]
+     "name": "order_items_order_id_fkey1",
+     "target_table": "public.orders_p0",
+     "name": "order_items_order_id_fkey2",
+     "target_table": "public.orders_p1",
```

Without `partition_of`, the `partition_of` test fails with `expected
undefined to be 'public.events'`. It also fails if `INHERITS` children
are marked, if `partition_of` points to the root, or if it uses the
partition's schema instead of the parent's.

With the fix, the CI steps pass locally:

- `biome ci .`: clean
- Management API types regenerated: no diff
- `pnpm run build` and `pnpm run test:packed-platform-consumer`: 3/3
- `vitest --coverage --project unit --project integration`: 752 passed
- `typecheck`: clean
- `tables.sql` on PostgreSQL 15.19 and 17.11 with the schema from #507,
plus sub-, default and cross-schema partitions, a detached partition and
`INHERITS` children: each FK reported once, `partition_of` set only on
partitions

The `test` job's 5 e2e tests need `ANTHROPIC_API_KEY`, which fork PRs
don't receive (same as #382 and #497).

I reviewed the diff and ran the checks above locally. I used AI tooling
while developing it.

## Additional context

Same class of fix as #382 and #497. `partition_of` follows the
suggestion in #507.
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.

list_tables returns columns out of order after ALTER COLUMN

2 participants