Skip to content

[drizzle-kit] fix: pg push drops and recreates index when .with() params are numeric - #6124

Open
SnowingFox wants to merge 1 commit into
drizzle-team:mainfrom
SnowingFox:fix/pg-index-numeric-with-reloptions
Open

[drizzle-kit] fix: pg push drops and recreates index when .with() params are numeric#6124
SnowingFox wants to merge 1 commit into
drizzle-team:mainfrom
SnowingFox:fix/pg-index-numeric-with-reloptions

Conversation

@SnowingFox

Copy link
Copy Markdown

Fixes #6079.

Problem

Declaring Postgres index storage parameters (.with()) with numeric values makes drizzle-kit push DROP and re-CREATE the index on every push, even when nothing changed:

index('chunk_embedding_hnsw_idx')
  .using('hnsw', t.embedding.op('vector_cosine_ops'))
  .with({ m: 24, ef_construction: 128 }),

Postgres stores reloptions as text in pg_class.reloptions, so the introspected snapshot always holds string values ({ "m": "24" }), while the schema-side serializer passed the raw numeric values ({ m: 24 }). The squashed index strings never compared equal, so the differ classified the index as altered and emitted DROP INDEX + CREATE INDEX on every run. For HNSW/pgvector indexes a rebuild takes many minutes and interrupts leave the DB without the index (seq-scan fallback).

Fix

Normalize the schema-side .with() values to strings in drizzle-kit/src/serializer/pgSerializer.ts when building the index snapshot, so the schema side matches what Postgres introspection returns. Numeric params now compare equal to the introspected text reloptions and the diff stays quiet:

with: value.config.with
  ? Object.fromEntries(
    Object.entries(value.config.with).map(([key, v]) => [key, String(v)]),
  )
  : {},

This also makes the emitted create_index/create_index_pg statement data consistent with the snapshot schema (indexV4/V5/V6 already declare with: record(string(), string())). The generated SQL is unchanged (WITH (m=24,ef_construction=128)). String params written by users keep working as before.

Test plan

Adds a regression test (drizzle-kit/tests/push/pg-index-with-reloptions.test.ts) that pushes the same schema containing an index with numeric .with() params and asserts that no DROP INDEX/CREATE INDEX statements are produced. On the old code the test fails with two spurious statements; with the fix it passes. Existing index tests that asserted numeric with values in the statement data were updated to the now-correct string representation.

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.

[BUG]: drizzle-kit push (pg): numeric index .with() params never match text reloptions — index dropped and rebuilt on every push

1 participant