Skip to content

list_tables returns columns out of order after ALTER COLUMN #496

Closed

Description

@janakmandavgade

Bug report

  • [✓] I confirm this is a bug with Supabase, not with my own application.
  • [✓] I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

list_tables (verbose) can return a table's columns in a different order from the table definition. The columns CTE is aggregated with array_agg(row_to_json(columns)) with no ORDER BY (packages/mcp-server-supabase/src/pg-meta/index.ts, coalesceRowsToArray), so the order follows pg_attribute scan order. ordinal_position is then removed from the output, so the agent has no way to recover the real order.

Routine migration statements such as ALTER COLUMN ... SET NOT NULL, SET DEFAULT or TYPE update the column's pg_attribute row. Postgres writes the new row version at the end of the heap, so after these statements a sequential scan returns that column last.

To Reproduce

create table people (id integer, email text, name text, age integer);
alter table people alter column email set not null;
alter table people alter column id set default 0;

Call list_tables with schemas: ["public"], verbose: true.

Expected columns: id, email, name, age
Actual columns: name, age, email, id

(Reproduced against PGlite with the exact listTablesSql query used by the server.)

Expected behavior

Columns are listed in ordinal_position (attnum) order, matching \d people and information_schema.columns.

Why it matters

Agents use this output to write INSERT ... VALUES (...) without column lists, to generate types and docs, and to reason about the schema. A shuffled order silently produces wrong SQL.

Proposed fix

Add ORDER BY columns.ordinal_position inside the array_agg. This is a one-line SQL change plus a regression test, with no schema or output-shape change. I have a PR ready.

System information

  • OS: Ubuntu 24.04 (WSL2 on Windows)
  • Browser: N/A (MCP server, no browser involved)
  • Version of supabase-js: N/A — affects @supabase/mcp-server-supabase 0.14.0 (repo main @ 910fde4)
  • Version of Node.js: v24.15.0
  • Database used for reproduction: PGlite 0.2.x (the test DB used by the repo's own test suite)

Additional context

Regression test added in src/server.database.test.ts, run against the repo's test DB (PGlite 0.2.17):

Without the fix:

× tools > list tables returns columns in ordinal position order after ALTER COLUMN
- Expected: ["id", "email", "name", "age"]
+ Received: ["name", "age", "email", "id"]

With ORDER BY columns.ordinal_position added:

✓ tools > list tables returns columns in ordinal position order after ALTER COLUMN
Tests  736 passed (736)

With ORDER BY columns.ordinal_position added to the column array_agg:

✓ tools > list tables returns columns in ordinal position order after ALTER COLUMN

Activity

  1. added
    bug
    on Oct 28, 2026
  2. added a commit that references this issue on Nov 11, 2026
    c242e63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions