Skip to content

A column an expression element reads cannot be dropped: ImpactOf answers Lost, and PostgreSQL refuses the drop (2BP01) #102

Closed

Description

@CaffeinatedCoder

Found by the pre-preview sweep of main @ 948496a, while checking a claim of GPT's review; reproduced on PostgreSQL 16 and 18.

What happens

An exclusion constraint has an expression element, pg.Equal(x => x.Note!.ToLower()). The column Note is dropped, with its element taken out of the declaration. The migration scaffolds and fails at apply:

CREATE EXTENSION IF NOT EXISTS btree_gist;
DropColumnOperation
ALTER TABLE memos ADD CONSTRAINT ex_memo EXCLUDE USING gist ("OwnerId" WITH =, "Period" WITH &&);
-- 2BP01: cannot drop column Note of table memos because other objects depend on it

The same when the declaration stays and Note becomes a stored computed column, which Npgsql writes as drop and add (#80): AlterColumnOperation, then the create, and 2BP01 at the drop.

Why

PgExclusionContributor.ImpactOf answers Lost for a dropped column that any element reads (PgExclusionContributor.cs:158, through ElementReads). PostgreSQL drops the constraint with the column only when the column itself is an element. A column read through an element's expression is a dependency like one in the condition: the drop is refused. Spike E measured the plain element and the condition; the expression element was not a cell of the table (plan, section 3.7).

Scenario

public sealed class MemoExpr : ITestModel
{
    public static void Build(ModelBuilder b) => b.Entity<Memo>().ToTable("memos").DeclarePostgres(pg =>
        [pg.Exclusion(pg.Equal(x => x.Note!.ToLower()).Overlaps(x => x.Period), name: "ex_memo")]);
}

// Note is gone, and so is its element.
public sealed class MemoWithoutNote : ITestModel
{
    public static void Build(ModelBuilder b) => b.Entity<MemoSlim>().ToTable("memos").DeclarePostgres(pg =>
        [pg.Exclusion(pg.Equal(x => x.OwnerId).Overlaps(x => x.Period), name: "ex_memo")]);
}

// database.Create<MemoExpr>(); database.Migrate<MemoExpr, MemoWithoutNote>();   → 2BP01

Change

Lost only for a column that is an element by itself (KeyTarget.Column); RebuildAround for one read through an expression, as for the condition. A column that is both gets RebuildAround, which is right in every case. The impact table of the plan gains the row, and spike E the cell.

Test

Both pairs in PgExclusionLiveTests through AssertMigratesTo, and the statement order in PgExclusionMigrationTests: the constraint dropped before DropColumnOperation.

Activity

  1. added this to the 11.0.0 milestone on Nov 14, 2026
  2. added
    bug
    severity/high
    on Nov 14, 2026
  3. added
    package/postgresql
    area/constraints
    on Nov 14, 2026
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/constraintsbugpackage/postgresqlseverity/high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions