Support for Partial Indexes (Filtered Indexes) #5749
+2,376
−66
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements support for partial indexes (also known as filtered indexes) in Prisma Schema Language. Partial indexes allow creating indexes that only include rows matching a specific condition, reducing index size and improving query performance.
There was an issue related to this PR that was 5 years old. 😱
New Syntax
Raw SQL Syntax (all supported databases)
Object Literal Syntax (type-safe alternative)
Preview Feature
This feature is gated behind the
partialIndexespreview feature:Supported Databases
CREATE INDEXCockroachDB Limitation
CockroachDB supports creating partial indexes but cannot introspect predicate text. This means:
add/modify/removepredicate migrations don't workThe differ skips predicate comparison for CockroachDB to prevent false-positive migrations.
Changes
PSL Parser (
psl/)object_expressionandobject_memberrulesExpression::Objectvariant andObjectMemberstructWhereClause,WhereCondition,WhereFieldCondition,WhereValueenumspartial_index_is_supportedvalidation requiring preview featurePartialIndexconnector capability for PostgreSQL, SQLite, CockroachDB, SQL ServerSchema Engine (
schema-engine/)CREATE INDEX ... WHEREfor all supported databasespredicates_match()trait method toSqlSchemaDifferFlavourpartialIndexespreview feature when partial indexes detectedSQL Server Specifics
CREATE INDEX(not as table constraints)render_create_table_asto exclude filtered unique indexes from constraintsfilter_definitioncolumn to index introspection querySQL DDL (
libs/sql-ddl/)WhereClausesupport toCreateIndexfor PostgreSQLCreatePartialIndexfor SQLiteTest Coverage
PSL Unit Tests (
psl/psl/tests/attributes/partial_index.rs)@@uniqueand@@index{ not: true/false/null }syntaxMigration Tests (
schema-engine/sql-migration-tests/)Total: 21 migration tests
Introspection Tests (
schema-engine/sql-introspection-tests/)@uniquewithwhereclauseGenerated SQL Examples
PostgreSQL
SQLite
SQL Server
CockroachDB
Key Files
psl/schema-ast/src/parser/datamodel.pestpsl/schema-ast/src/ast/expression.rsExpression::ObjectAST typepsl/parser-database/src/attributes.rswhereclause parsing logicpsl/parser-database/src/walkers/index.rspsl/psl-core/src/common/preview_features.rsPartialIndexesfeature flagpsl/psl-core/src/builtin_connectors/mssql_datamodel_connector.rsPartialIndexcapabilityschema-engine/.../sql_schema_calculator.rsschema-engine/.../sql_schema_differ/table.rsschema-engine/.../sql_schema_differ_flavour.rspredicates_match()trait methodschema-engine/.../flavour/postgres/schema_differ.rsschema-engine/.../flavour/mssql/renderer.rsschema-engine/.../flavour/mssql/schema_differ.rsschema-engine/sql-schema-describer/src/mssql.rsschema-engine/.../introspection/Related
Checklist