Repository navigation
fix(pg-meta): order list_tables columns by ordinal position - #497
Merged
mattrossman merged 2 commits intoOct 28, 2026
Merged
Conversation
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.
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
approved these changes
Oct 28, 2026
4 tasks
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.
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.
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'scolumnswitharray_agg(row_to_json(columns))and noORDER BY, so the order followspg_attributescan order.ALTER COLUMN ... SET NOT NULL/SET DEFAULT/TYPEwrite a newpg_attributerow version at the end of the heap, so that column is returned last.ordinal_positionis stripped from the output, so the agent can't recover the real order.What is the new behavior?
Columns are returned in
ordinal_positionorder.coalesceRowsToArraytakes an optionalorderBy, andlistTablesSqlpassescolumns.ordinal_position. WhenorderByis 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/listcontract is untouched.How to Review
Column aggregation
packages/mcp-server-supabase/src/pg-meta/index.tscoalesceRowsToArraygains an optionalorderByplaced insidearray_agg(...)beforeFILTER.listTablesSqlis its only caller.Tests
packages/mcp-server-supabase/src/server.database.test.tslist tables returns columns in ordinal position order after ALTER COLUMNchecks the observable column order from thelist_tablestool after the ALTERs that reorder the heap.Review questions
Verification
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 --noEmitand 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