Skip to content

list_tables reports a foreign key once per partition when a partitioned table is involved #507

Closed

Description

@sarpaydemir
  • 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: true) reports the FK copies Postgres creates for partitions as real foreign keys. One declared FK is listed once per partition, and every partition gets an FK it never declared.

First noted by @Rjabov at the end of #381; it hasn't been filed since, so I'm picking it up.

To Reproduce

create table orders (id int primary key) partition by hash (id);
create table orders_p0 partition of orders for values with (modulus 2, remainder 0);
create table orders_p1 partition of orders for values with (modulus 2, remainder 1);
create table order_items (order_id int references orders (id));

create table users (id int primary key);
create table events (user_id int references users (id), created_at date) partition by range (created_at);
create table events_2025 partition of events for values from ('2025-01-01') to ('2026-01-01');
create table events_2026 partition of events for values from ('2026-01-01') to ('2027-01-01');

Two FKs are declared. foreign_key_constraints per table:

table reported expected
order_items 3: order_items_order_id_fkey, …_fkey1, …_fkey2 1
users 3: events_user_id_fkey ×3 1
orders_p0, orders_p1, events_2025, events_2026 1 each none

The count grows with partitions: with 12 monthly partitions, users lists the same FK 13 times.

Root cause

The relationships subquery in pg-meta/tables.sql reads every contype = 'f' row of pg_constraint, including the clones Postgres creates for partitions (conparentid <> 0). pg_dump skips them with the same predicate (getConstraints: contype = 'f' AND conparentid = 0).

Expected behavior

Each FK is reported once, on the tables it connects. FKs declared directly on a partition are still reported.

System information

  • @supabase/mcp-server-supabase at main (ed5084d)
  • PostgreSQL 15.19 and 17.11; PGlite (PostgreSQL 16.4) in the test harness

Additional context

Fix and regression test are ready; PR to follow.

Separate question, not part of the fix: partitions are listed as standalone tables next to their parent. Is that intended, or should list_tables mark or group them?

Activity

  1. mattrossman commented on Nov 2, 2026

    @mattrossman
    Collaborator

    partitions are listed as standalone tables next to their parent. Is that intended, or should list_tables mark or group them?

    I think if we're removing the duplicate foreign keys, it'd be helpful to have a marker so the relationship is still communicated.

    Something like:

    {
      "name": "public.events_2025",
      "partition_of": "public.events",
      "columns": [...],
      "primary_keys": [...]
    }
  2. sarpaydemir commented on Nov 2, 2026

    @sarpaydemir
    ContributorAuthor

    Thanks, added in #508. partition_of is in both compact and verbose output and points to the immediate parent, so a sub-partition names its own parent rather than the root.

  3. added a commit that references this issue on Nov 11, 2026
    2c7fa82
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions