Skip to content

feat: btree_gist is injected where an exclusion constraint needs it, once per migration and first - #79

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
feat/btree-gist-injection
Nov 12, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
feat/btree-gist-injection

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Closes #78

Was stacked on #77; rebased onto main after that merged.

Problem

An exclusion constraint with = on a scalar column needs btree_gist. After #77 a model had to declare the extension itself; without it the migration scaffolds and fails at apply: data type integer has no default operator class for access method "gist".

Change

Kernel

  • SchemaResolver.StoreType(path): the column's type as the model states it, asked of the declared property in the table, like its column. A snapshot records it, so both sides answer alike.
  • The planner acts on Dependency.Extension: for what it creates, one call per extension to ISchemaDialect.EnsureExtension(extension, targetModel, sql), ahead of every other statement. The kernel's own dialect refuses.

PostgreSQL

  • The descriptor lists btree_gist by 5.x's rule, unchanged: GiST only; a column by its store type; an expression by its operator. It sits in an Operational<> field, so it is never compared.
  • NpgsqlSchemaDialect.EnsureExtension writes CREATE EXTENSION IF NOT EXISTS btree_gist, and nothing when the model declares the extension with HasPostgresExtension or carries the suppress annotation.
  • modelBuilder.SuppressBtreeGistInjection() sets 5.x's annotation, CustomTemporal:SuppressAutoExtension.
  • The test models no longer declare the extension, so every live test that creates a constraint measures the injection.

Decisions to confirm

  1. ISchemaDialect.EnsureExtension is a new member of the contract, and the dialect, not the contributor, decides whether a statement is needed. The alternative was the contributor writing the statement in Create, which gives one per constraint and no access to the model.
  2. SchemaResolver.StoreType returns the store type as text, not the design's StoreFacts record. Enough for this rule; StoreFacts can come with the first rule that needs a mapping.
  3. A rebuild writes the statement again (IF NOT EXISTS, once per migration), as 5.x did for every add.
  4. The suppress annotation applies to every extension a dialect is asked for, not to btree_gist alone. Only btree_gist exists today.
  5. No CASCADE on the statement, unlike Npgsql's own; btree_gist depends on nothing.

Found on the way

  • 5.x's check for an array ([]) before the range suffix was dead: no array type ends in range or is in the list. Removed; the rows for integer[] and daterange[] hold the answer.
  • Npgsql declares ltree by itself for a column of that type, so a diff to it carries an AlterDatabaseOperation.

Tests

420 kernel, 61 PostgreSQL offline, 24 live on PostgreSQL 16.15 and 18.6, 125 probes.

  • Kernel: the store type on a model and its compiled snapshot, on three providers; an extension two created objects share is ensured once and first, and not at all when nothing is created; the kernel's dialect refuses.
  • PostgreSQL offline: the statement once and first for two constraints; present for a scalar column and for an expression compared by =, absent for a range, for SP-GiST, for a model that declares the extension, and for one that suppresses; a type change that flips the need leaves the constraint alone; the snapshot leads to the same statements as the model.
  • Live: a database without the extension gets it from a first and from a later migration; suppressed, the migration fails with 42704 on a database that lacks it and applies on one that has it.

Each guard shown to fail, 15 mutations in a scratch copy. Two survived the first run and are closed: SP-GiST counted like GiST (no test had a scalar under SP-GiST; TextSpGist added) and the dead array check (removed).

Mutation Fails
Planner ignores extension dependencies 13 offline, 30 live
Planner asks once per object Two_constraints_…_once_and_first, An_extension_…_ensured_once_and_first
Extensions ensured after the drops Another_deferral_is_a_rebuild, A_dropped_condition_column_…, the kernel's
Kernel dialect accepts an extension silently The_kernels_dialect_refuses_a_dependency_on_an_extension ×3
Store type not the column's A_paths_store_type_… ×3, two PostgreSQL tests
The need is compared A_type_change_that_ends_the_need_…
Columns never need it; expressions never need it 8 offline and 26 live; 1 offline and 2 live
SP-GiST counts like GiST The_extension_is_added_for_what_needs_it_and_for_nothing_else
Ranges need it A_store_type_has_a_gist_operator_class_… and 5 more
Suppression ignored; the model's own declaration ignored A_model_that_suppresses_…, live Suppressed_…; A_model_that_declares_the_extension_itself_…
The switch sets another key; 5.x's key changed The_suppress_switch_is_the_annotation_5x_wrote

🤖 Generated with Claude Code

@CaffeinatedCoder CaffeinatedCoder added this to the 11.0.0 milestone Nov 12, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request stacked Base is another open PR; merge in order package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) package/postgresql Affects CodoMetis.EFCore.SchemaKit.PostgreSQL area/constraints Exclusion constraints, row guards and other constraint-like objects labels Nov 12, 2026
Base automatically changed from feat/pg-exclusion-package to main November 12, 2026 11:15
…once per migration and first

Closes #78.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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 enhancement New feature or request package/core Affects CodoMetis.EFCore.SchemaKit (the kernel) package/postgresql Affects CodoMetis.EFCore.SchemaKit.PostgreSQL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PgExclusion, second slice: btree_gist is injected where an element needs it

1 participant