Skip to content

[Pg-kit] Ignore leaf partitions when introspecting Postgres - #6196

Open
EndiButler wants to merge 1 commit into
drizzle-team:betafrom
EndiButler:fix/pg-kit-leaf-partitions
Open

[Pg-kit] Ignore leaf partitions when introspecting Postgres#6196
EndiButler wants to merge 1 commit into
drizzle-team:betafrom
EndiButler:fix/pg-kit-leaf-partitions

Conversation

@EndiButler

Copy link
Copy Markdown

What this fixes

A leaf partition has relkind = 'r', so Postgres introspection returns it as an ordinary table. Nothing downstream knows it is a partition.

Reproduced on this branch's parent (beta, drizzle-kit@1.0.0-rc.4) with one partitioned parent and three monthly partitions, against a schema file that does not declare them:

tables seen by introspection: [ 'logs', 'logs_2026_01', 'logs_2026_02', 'logs_2026_03' ]

push would run:
  DROP TABLE "logs";
  DROP TABLE "logs_2026_01";
  DROP TABLE "logs_2026_02";
  DROP TABLE "logs_2026_03";

Two separate problems:

  1. push proposes dropping partitions the user never declared — for the common time-range retention setup, those are created by pg_partman or a cron job and hold the data the partitioning exists to protect.
  2. It does not even complete: dropping the parent already dropped its partitions, so the second statement aborts with table "logs_2026_01" does not exist, leaving the database partially migrated.

pull is affected the same way — it emits a CREATE TABLE per partition, duplicating the parent's columns and its automatically propagated indexes.

This is what was reported in #2854 ("drizzle-kit push seems to be blowing up").

The fix

Filter on relispartition in both Postgres introspection paths (introspect.ts and aws-introspect.ts). The partitioned parent stays; its partitions are hidden. Because the child OIDs then drop out of every downstream constraint and index query, their auto-created indexes disappear from the pulled schema too.

Scope

Deliberately narrow: this is only the bug. It does not add any way to declare partitioning — that is the feature request in #2854, which I have written up separately as a design proposal with a working prototype branch. This fix stands on its own and does not depend on it.

Tests

  • tests/postgres/pull.test.ts — the existing introspect partitioned tables test now creates a real leaf partition and asserts only the parent is returned.
  • tests/postgres/pg-tables.test.ts — new push does not drop leaf partitions it does not know about: creates partitions outside drizzle, then asserts push produces no statements and both partitions survive.

Regression check on this machine: tests/postgres reports the same failure set as the untouched beta branch (those failures need a live Postgres/PostGIS container and a built drizzle-orm/dist), with one additional passing test. tsc --noEmit clean for both changed files.

A leaf partition has relkind = 'r', so introspection returned it as an ordinary
table. pull then duplicated the parent's columns and indexes for every
partition, and push proposed DROP TABLE for each partition the schema file did
not declare - then aborted mid-migration, because dropping the parent had
already dropped them:

  DROP TABLE "logs";
  DROP TABLE "logs_2026_01";   -- ERROR: table "logs_2026_01" does not exist

Filtering on relispartition keeps the partitioned parent and hides its
partitions, which also removes their auto-created indexes from every downstream
introspection query.

Refs drizzle-team#2854
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.

1 participant