Skip to content

[13.x] Add support for partial indexes - #61098

Closed
ibrasho wants to merge 1 commit into
laravel:13.xfrom
ibrasho:feature/add-partial-index-support
Closed

[13.x] Add support for partial indexes#61098
ibrasho wants to merge 1 commit into
laravel:13.xfrom
ibrasho:feature/add-partial-index-support

Conversation

@ibrasho

@ibrasho ibrasho commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Adds a where modifier to index definitions so an index can be limited to the rows matching a predicate:

// Fluent on the index definition
$table->unique(['c1', 'c2'])->where('c1 is null');

// Fluent on the column
$table->string('c1')->nullable()->unique()->where(fn ($q) => $q->whereNotNull('c1'));

// Closure form — portable, compiled by the query grammar, bindings inlined
$table->index('c1')->where(fn ($q) => $q->where('c1', 'a')->whereNull('c2'));
// → create index "..." on "orders" ("c1") where "c1" = 'a' and "c2" is null

The predicate accepts a raw string, an expression, or a closure over a query builder, in which case the constraints are compiled by the query grammar and their bindings inlined, since DDL statements may not carry any bindings.

Today this requires dropping to DB::statement('create unique index ... where ...'), which works until the migration touches something SQLite cannot alter in place — adding a foreign key, changing a column — at which point the table is rebuilt, its indexes are regenerated from introspection, and the predicate is silently lost along with the uniqueness guarantee it was enforcing.

Driver support

Partial indexes are supported on PostgreSQL, SQLite and SQL Server. MySQL and MariaDB have no equivalent, so a predicate throws a RuntimeException there rather than being silently dropped.

A unique constraint may not be backed by a partial index on PostgreSQL, so a predicate compiles the index directly instead of through an add constraint statement. Constraint-only options have no equivalent on a bare index, so combining where with deferrable throws rather than dropping the deferrability.

SQLite exposes no pragma for the predicate of a partial index, so it is read back from sqlite_master and is now carried through the table rebuild that emulates the unsupported alter table operations, which would otherwise regenerate the index without its predicate.

Introspection

Schema::getIndexes() now returns a where key alongside the existing ones, holding the predicate as reported by the database (pg_get_expr(indpred, indrelid) on PostgreSQL, sys.indexes.filter_definition on SQL Server, parsed from the index definition on SQLite) or null. Anything implementing a custom schema processor will want to add the key.

Notes for review

  • On a ColumnDefinition the predicate is a single attribute, so a column declared with both ->unique() and ->index() applies the same predicate to both indexes. Splitting it per index method would mean tracking predicates per fluent index rather than per column — happy to change this if the shared predicate is the wrong default.
  • SQLite and PostgreSQL 17 are covered by integration tests run against live databases, including a regression test asserting the predicate survives a table rebuild and still enforces uniqueness. SQL Server is covered by grammar unit tests only.

Adds a "where" modifier to index definitions so indexes can be limited to
the rows matching a predicate:

    $table->unique(['workspace_id', 'sku'])->where('sku is not null');
    $table->string('sku')->unique()->where(fn ($q) => $q->whereNotNull('sku'));

The predicate accepts a raw string, an expression, or a closure over a query
builder, in which case the constraints are compiled by the query grammar and
their bindings inlined, since DDL statements may not carry any bindings.

Partial indexes are supported on PostgreSQL, SQLite and SQL Server. MySQL and
MariaDB have no equivalent, so a predicate raises an exception there rather
than being silently dropped.

A unique constraint may not be backed by a partial index on PostgreSQL, so a
predicate compiles the index directly instead of through an "add constraint"
statement. SQLite exposes no pragma for the predicate of a partial index, so
it is read back from "sqlite_master" and is now carried through the table
rebuild that emulates the unsupported "alter table" operations, which would
otherwise regenerate the index without its predicate.
@taylorotwell

Copy link
Copy Markdown
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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.

2 participants