Skip to content

A column rename drops and re-creates every complex index and exclusion constraint on the column #182

Closed

Description

@CaffeinatedCoder

Found while reviewing the SchemaKit design (#61); reproduced on main @ 8dfbf08 in a scratch MigrationHarness.NpgsqlDiff test.

Problem

When a column is renamed, every complex index and exclusion constraint on that column is dropped and created again. The database would have kept them: PostgreSQL and SQLite carry an index or constraint across a column rename, and SQL Server does unless the index's filter reads the column (sp_rename rejects that: "The index … is dependent on column …"). EF Core emits only the RenameColumnOperation for its own unfiltered indexes.

The differs compare resolved descriptors. The source side resolves to the old column name and the target side to the new one, so the pair never matches. Table renames are normalized (CustomMigrationsModelDiffer.cs:201, NpgsqlComplexIndexMigrationsModelDiffer.cs:1073). Column renames are not.

Model: members with HasColumnName("email") → "email_address" and "code" → "member_code". It has a unique HasComplexIndex(x => x.Email.Value), a composite index over both columns, and an exclusion constraint on Code.

CREATE EXTENSION IF NOT EXISTS btree_gist;
ALTER TABLE "members" DROP CONSTRAINT IF EXISTS "ex_members_code";
DROP INDEX ux_members_email;
DROP INDEX ix_members_code_email;
ALTER TABLE members RENAME COLUMN email TO email_address;
ALTER TABLE members RENAME COLUMN code TO member_code;
CREATE UNIQUE INDEX ux_members_email ON members (email_address);
CREATE INDEX ix_members_code_email ON members (member_code, email_address);
ALTER TABLE "members" DROP CONSTRAINT IF EXISTS "ex_members_code";
ALTER TABLE "members" ADD CONSTRAINT "ex_members_code" EXCLUDE USING gist ("member_code" WITH =, (int4range(1,2)) WITH &&);

The result is correct, but it costs a full rebuild. DROP INDEX and ADD CONSTRAINT … EXCLUDE take ACCESS EXCLUSIVE and hold it until commit. On a large table, a rename that should take milliseconds blocks reads and writes for as long as every index and constraint on the column takes to build.

Change

Resolve the source side under the target's column names for columns that a RenameColumnOperation in the base operations renames, as table renames already do. That covers column parts, JSON extractions (the container column), template and filter placeholders, INCLUDE lists, and exclusion and temporal elements. A pair that matches after normalization produces no operation.

This changes what an existing model scaffolds, so it ships in a minor with a ### Changed answers row and a docs/upgrading.md entry.

SchemaKit needs the same rule: design/schemakit-api.md §9.4 names table renames only.

Tests

  • A MigrationHarness.NpgsqlDiff test for the model above: only the two RenameColumnOperations, no index or constraint operations.
  • The same on SQL Server and SQLite through CoreDiff/SqlServerDiff.
  • A filter whose placeholder names the renamed column, and an INCLUDE entry naming it.
  • A PostgresIntegrationTests case that applies the rename and reads the index definition back from pg_indexes.

Activity

  1. added this to the 5.6.0 milestone on Oct 23, 2026
  2. added
    bug
    severity/medium
    package/core
    package/postgresql
    area/indexes
    area/constraints
    area/migrations
    package/sqlserver
    on Oct 23, 2026
  3. CaffeinatedCoder commented on Oct 23, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed on release/5.x by #183 (merge d28c938), shipping in 5.6.0. Closed by hand: Closes acts only on merges into main.

    A column rename now keeps the complex indexes, exclusion constraints and temporal constraints on the column, and a default name that follows the column becomes a rename. SQL Server is the exception for filtered indexes: sp_rename rejects a column a filtered index's predicate reads, so such an index is dropped before the rename and created after it, as EF Core does for its own.

    SchemaKit still needs the same rule: design/schemakit-api.md §9.4 covers table renames only.

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

    area/constraintsarea/indexesarea/migrationsbugpackage/corepackage/postgresqlpackage/sqlserverseverity/medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions