Skip to content

feat: Npgsql 11's native temporal keys accept every range and multirange column - #335

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
test/150-native-temporal-keys
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
test/150-native-temporal-keys

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes CaffeinatedCoder/EFCore.ComplexIndexes#150

Problem

On EF Core 11, Npgsql declares temporal keys itself: HasKey(…).WithoutOverlaps() and WithPeriod() foreign keys. The issue assumed Npgsql checks the period column's store type. It checks the type mapping, so every column this package maps failed model validation:

WITHOUT OVERLAPS on primary key in entity type 'Slot' requires the last column to be a PostgreSQL range type (e.g. daterange, tsrange, tstzrange), but property 'Period' has type 'DateRange'.

Measured on PostgreSQL 18 (full table in the issue comment): the refusal holds on rc.1.1 and on rc.2 alike, for DateRange, TimeRange (custom timerange), RangeSet (datemultirange) and the PERIOD foreign key. PostgreSQL 18 itself accepts all of them.

The validator's check is typeMapping is not NpgsqlRangeTypeMapping on rc.1.1. On rc.2 it is typeMapping is not INpgsqlRangeTypeMapping (npgsql/efcore.pg#3975, added for NodaTime's ranges).

Change

  • Floors: Npgsql.EntityFrameworkCore.PostgreSQL and its NodaTime plugin move to 11.0.0-rc.2, and Microsoft.EntityFrameworkCore.Design in the two EF test projects to 11.0.0-rc.2.26504.105. The interface doesn't exist on rc.1.1. This takes the floor bullet of Publish 11.0.0-preview.1 on the .NET 11 GA SDK and Npgsql's provider rc.3 #332.
  • Mappings: ValueRangeTypeMapping, ValueRangeSetTypeMapping, YearMonthRangeTypeMapping and YearMonthRangeSetTypeMapping implement INpgsqlRangeTypeMapping explicitly. CreateCurrentValueComparer returns null, because every range type and RangeSet is already IComparable<T> in PostgreSQL's order, so EF's default comparer applies.
    • On rc.2, Npgsql reads the interface in three places only: the validator, and the two conventions that set that comparer. No query translation changes.
    • The interface lives in Npgsql's Storage.Internal.Mapping namespace, and the comment says so. No EF1001 warning fires.
  • Tests: NativeTemporalKeyTests (integration, on a PostgreSQL 18 container of its own):
    • a DateRange, RangeSet<DateRange, DateOnly>, TimeRange and YearMonthRange key: create script, pg_get_constraintdef, btree_gist, adjacent and other-room rows saved, overlap → DbUpdateException / 23P01 on PK_Slots
    • a PERIOD foreign key: covered across two adjacent principals saved, uncovered → 23503
    • without SetPostgresVersion(18, 0), the model throws before any SQL
    • the scaffolded migration carries Npgsql:WithoutOverlaps
    • a discovered sweep: every range type and its RangeSet resolve to an INpgsqlRangeTypeMapping and pass Npgsql's validation (census-checked, so a new type needs no edit)
  • Docs: a new docs/efcore.md section, "Reject overlapping rows with Npgsql's temporal keys (PostgreSQL 18)". The ComplexIndexes section below it now opens by saying it is the 10.x (EF Core 10) route.
  • Oracles: claims E64–E69, the sweep registered in oracles.json, seed multirange-mapping-not-an-npgsql-range-mapping.patch, and the two postgres-surface.json waivers now point to the new section with the real API names.
  • Changelogs: ### Added lines in the root, EF Core and EF Core NodaTime changelogs.

Tests

  • Reverted the fix: with the interface stripped from all four mappings, 7 of the 8 tests fail with Npgsql's "requires the last column to be a PostgreSQL range type". The eighth, the SetPostgresVersion test, is independent of the change. With only the multirange half stripped, the sweep names all 12 RangeSet types. That revert is the committed seed.
  • Full suite on rc.2: see the CI run. Locally, on .NET 11 RC2 with Npgsql rc.2, every project passes: core 1397, NodaTime 238, EF Core 291, EF Core NodaTime 83, ASP.NET Core 92, conventions 404, integration 284 on the default PostgreSQL 16 container (the new class on its own 18), infinity-disabled 4.
  • dotnet run tools/oracles.cs seeds multirange-mapping: CAUGHT … → NativeTemporalKeyTests: 2 of 8 tests fail, e.g. MultirangeKey_RejectsAnOverlap_AndAcceptsAdjacentAndOtherKeys.

Not in this PR

🤖 Generated with Claude Code

CaffeinatedCoder and others added 2 commits October 19, 2026 18:19
…nge column

Npgsql 11 validates WITHOUT OVERLAPS keys and PERIOD foreign keys by the
type mapping, not the store type name: from rc.2 on it accepts any mapping
that implements INpgsqlRangeTypeMapping. The range, multirange and
YearMonth mappings now implement it, returning no comparer because every
range type and RangeSet is already IComparable in PostgreSQL's order.

Npgsql EF moves to 11.0.0-rc.2 (the interface does not exist on rc.1.1),
EF Core Design to rc.2 in the test projects. NativeTemporalKeyTests holds
it on PostgreSQL 18, docs/efcore.md documents it, claims E64-E69.

Closes #150

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/range-set RangeSet and multirange algebra area/ranges Range unions, variants and the shape engines enhancement New feature or request package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UseComplexIndexes() on PostgreSQL or SQL Server still drops exclusion and temporal constraints and the IS NOT NULL filter without a word

1 participant