Skip to content

fix: a column an element's expression reads holds the drop, it is not lost with it - #123

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
fix/expression-element-column-dropped
Nov 14, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
fix/expression-element-column-dropped

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #102

Problem

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

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

The same when the declaration stays and Note becomes a stored computed column, which Npgsql writes as a drop and an add (#80).

PgExclusionContributor.ImpactOf answered Lost for a dropped column that any element reads. PostgreSQL drops the constraint with the column only when the column is an element by itself. Spike E measured that case and the condition's; a column inside an element's expression was not a cell of the table.

Change

  • Lost for a column that is an element by itself, RebuildAround for every other column the constraint reads. The descriptor keeps the columns that are elements by themselves (KeyColumns), in an Operational<> field outside its comparison: the elements say it already, so no model differs from its snapshot for it.
  • The rule is the server's, not the one A column an expression element reads cannot be dropped: ImpactOf answers Lost, and PostgreSQL refuses the drop (2BP01) #102 proposed. The issue said a column read in more than one way should be rebuilt around, as the answer that is right in every case. Measured, a column that is an element by itself takes the constraint with it whatever else reads it too, so that case needs no drop and gets none.
  • The plan's impact table (section 3.7) gains the row, with the cells that were not measured marked as such, and CLAUDE.md says where a new kind of element gets its row.

Tests

Locally: 517 kernel tests, 192 of the PostgreSQL package with the live ones on PostgreSQL 16.x and 18.x, 125 probes. Twelve tests of the PostgreSQL package are new, eight of them on a live server.

The server's word, as one test (The_server_drops_the_constraint_with_an_elements_column_and_holds_every_other_column_it_reads): ALTER TABLE … DROP COLUMN by hand with the constraint in place, alike on 16 and 18.

The column is The drop The constraint
an element by itself applies gone with it
in an element's expression only refused, 2BP01 stays
in the condition only refused, 2BP01 stays
an element, and in another element's expression applies gone
an element, and in the condition applies gone
an element, beside SQL that names no column through a placeholder applies gone
read by SQL that names no column through a placeholder refused, 2BP01 stays

The statements of each diff (PgExclusionMigrationTests): the constraint dropped before DropColumnOperation and before the AlterColumnOperation of a computed column for an expression's column; no drop for an element's column that something else reads too; beside SQL that names no column, an element's column goes and any other is rebuilt around; a rename and a type change of an expression's column leave the constraint alone.

Each of those pairs applied (PgExclusionLiveTests, through AssertMigratesTo): the catalog after the migration is the catalog EnsureCreated() builds for the target.

Guards shown to fail, four defects seeded one at a time in a scratch copy on PostgreSQL 18:

Seed Tests that fail
The old rule: every column an element reads is lost with it 4, two of them at apply with 2BP01
Expressions taken for the columns that are elements 8
No column takes the constraint with it 13
Every element counts as a column by itself 4

Decisions in this PR that are not confirmed

  • The measured rule over the issue's conservative one, as above. It costs a statement less in three cases and rests on one more fact about the server, which the test holds.
  • KeyColumns on the descriptor, outside its comparison, rather than a flag per element or reading the element's text.
  • The row is held by PgExclusionLiveTests, not by spike E.

Not in this PR

🤖 Generated with Claude Code

…mn, it holds the drop

PgExclusionContributor.ImpactOf answered Lost for a dropped column that any
element reads. PostgreSQL drops an exclusion constraint with a column only
when the column is an element by itself. A column that an element's
expression reads, (lower("Note")) WITH =, is held like one in the condition:
the drop is refused while the constraint is there (2BP01). The migration
scaffolded and failed at apply, for a plain drop and for an alteration that
Npgsql writes as drop and add.

Measured on PostgreSQL 16 and 18, one row for each way a constraint reads a
column: an element by itself takes the constraint with it whatever else reads
the column too; a column read only through an expression, only by the
condition, or by SQL that names no column is held. The descriptor keeps the
columns that are elements by themselves, outside its comparison, and answers
Lost for those and RebuildAround for every other.

Closes #102

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 14, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working severity/high Wrong results, crash or failed writes in a realistic scenario package/postgresql Affects CodoMetis.EFCore.SchemaKit.PostgreSQL area/constraints Exclusion constraints, row guards and other constraint-like objects labels Nov 14, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 88fb08b into main Nov 14, 2026
6 of 7 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the fix/expression-element-column-dropped branch November 14, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/constraints Exclusion constraints, row guards and other constraint-like objects bug Something isn't working package/postgresql Affects CodoMetis.EFCore.SchemaKit.PostgreSQL severity/high Wrong results, crash or failed writes in a realistic scenario

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant