Skip to content

Support indexes on JSON members - #3974

Merged
roji merged 1 commit into
mainfrom
roji-json-member-index-triage
Oct 16, 2026
Merged

roji merged 1 commit into
mainfrom
roji-json-member-index-triage

Conversation

@roji

@roji roji commented Oct 16, 2026

Copy link
Copy Markdown
Member

JSON member selectors currently produce indexes on the containing document rather than the selected value, so unique indexes do not enforce member uniqueness. This adds PostgreSQL expression-index support through the existing HasIndex API.

  • Preserve EF's selected-member metadata and reuse query SQL rendering for matching JSON extraction, casts, and path escaping. B-tree remains the default; explicit GIN supports appropriate jsonb array/subdocument workloads, with existing whole-document indexes unchanged.
  • Support nested paths, fixed collection positions, composite keys, and existing index options. Recreate indexes when their expression type changes, and reject unsupported wildcard traversal and nested tsvector combinations.
  • Cover generated SQL, actual PostgreSQL uniqueness and queries, migration up/down behavior, snapshot compilation, model differences, and legacy whole-document scaffolding.

Temporary EF RC2 limitations: dependencies remain unchanged. The pinned build cannot scaffold migration C# that creates member indexes or construct mixed-direction composite member indexes. Active tests assert the exact failures and link to the merged fix in dotnet/efcore#39089; consuming it will require converting those assertions to positive coverage. Uniform-direction composites work.

Previously applied incorrect indexes require explicit drop/recreate migrations; upgrading alone cannot repair them. User documentation changes are prepared separately in npgsql/doc for the JSON mapping page and 11.0 release notes.

Closes #2568
Fixes #3918

Preserve JSON index metadata and render member expressions using the query SQL generator, while retaining whole-document index behavior. Detect expression type changes and reject unsupported collection traversal and tsvector combinations.

Cover SQL generation, PostgreSQL enforcement, migration model differences, snapshot round-trips, and exact failures for the pinned EF RC2 limitations.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 2918b62e-4cca-481c-876b-722b5ac7cb13
Copilot AI balanced review requested due to automatic review settings October 16, 2026 09:53
@roji
roji enabled auto-merge (squash) October 16, 2026 09:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Temporal casts can prevent index creation, and nested keys named NULL can silently bypass uniqueness enforcement.

2 open findings
What changed in this PR

Adds PostgreSQL expression indexes for JSON members through HasIndex, addressing indexes that previously targeted the entire document.

Changes:

  • Preserves JSON paths and renders member extraction expressions with existing index options.
  • Validates unsupported traversal and tracks expression-type changes for migrations.
  • Adds SQL, uniqueness, migration, snapshot, and compatibility coverage, including documented RC2 limitations.
File Description
test/​EFCore.PG.Tests/​Migrations/​JsonIndexTest.cs Tests scaffolding, snapshots, model differences, and validation.
test/​EFCore.PG.FunctionalTests/​Migrations/​NpgsqlMigrationsSqlGeneratorTest.cs Tests generated SQL and PostgreSQL behavior.
src/​EFCore.PG/​Properties/​NpgsqlStrings.resx Adds JSON-index diagnostics.
src/​EFCore.PG/​Properties/​NpgsqlStrings.Designer.cs Exposes diagnostic resource accessors.
src/​EFCore.PG/​Migrations/​NpgsqlMigrationsSqlGenerator.cs Renders JSON member index expressions.
src/​EFCore.PG/​Metadata/​Internal/​NpgsqlAnnotationProvider.cs Preserves JSON-index metadata and expression types.
src/​EFCore.PG/​Metadata/​Internal/​NpgsqlAnnotationNames.cs Defines the expression-type annotation.
src/​EFCore.PG/​Infrastructure/​Internal/​NpgsqlModelValidator.cs Rejects unsupported JSON-index configurations.
src/​EFCore.PG/​Extensions/​BuilderExtensions/​NpgsqlIndexBuilderExtensions.cs Documents index-method behavior for JSON members.
Files not reviewed (1)
  • src/EFCore.PG/Properties/NpgsqlStrings.Designer.cs: Generated file

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +2491 to +2494
var typeMapping = element.StoreTypeMapping ?? element.ContainingColumn.StoreTypeMapping!;
var expression = new JsonScalarExpression(
new SqlFragmentExpression(DelimitIdentifier(element.ContainingColumn.Name)),
path, typeMapping.ClrType, typeMapping, element.IsNullable);
new SqlFragmentExpression(DelimitIdentifier(element.ContainingColumn.Name)),
path, typeMapping.ClrType, typeMapping, element.IsNullable);

return _querySqlGeneratorFactory.Create().GetCommand(expression).CommandText;
@roji
roji merged commit 7844382 into main Oct 16, 2026
16 checks passed
@roji
roji deleted the roji-json-member-index-triage branch October 16, 2026 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET 11 RC1: Index on a member of a JSON-mapped complex type indexes the whole jsonb column Make HasIndex work on JSON properties

2 participants