Skip to content

Conversation

jkomyno
Copy link
Contributor

@jkomyno jkomyno commented Jul 29, 2025

DRAFT

This PR is a quick attempt at providing Partial Index support for PostgreSQL and CockroachDB.
This is not a planned feature, but I figured I'd give it a shot.

Index expressions like

CREATE TABLE "Order" (
    id SERIAL PRIMARY KEY,
    user_id INT NOT NULL,
    status VARCHAR(20) NOT NULL
);

-- Partial index
CREATE INDEX "Order_user_status_idx" ON "Order" (user_id, status) WHERE status IN ('pending', 'processing');

are stored in PSL like

model Order {
  id         Int    @id @default(autoincrement())
  user_id    Int
  status     String @db.VarChar(20)

  @@index([user_id, status], where: "status IN ('pending', 'processing')")
}

This feature is gated behind previewFeatures = ["partialIndexes"].


Limitations:

  • only provider = "postgresql" and provider = "cockroachdb" are supported
  • users have to write raw SQL in PSL, with no SQL autocompletion / compile-time syntax check
  • the SQL written by users may not correspond to the SQL stored in the database. For example:
    • user-defined:
      @@index([user_id, status], where: "status IN ('pending', 'processing')")
    • re-introspected:
      @@index([user_id, status], map: "Order_user_status_idx", where: "((status)::text = ANY ((ARRAY['pending'::character varying, 'processing'::character varying])::text[]))")

Copy link

codspeed-hq bot commented Jul 29, 2025

CodSpeed Performance Report

Merging #5567 will not alter performance

Comparing feat/partial-index (d5a0491) with main (361e86d)

Summary

✅ 11 untouched benchmarks

Copy link
Contributor

github-actions bot commented Jul 29, 2025

WASM Query Engine File Size

Engine This PR Base branch Diff
Postgres 2.203MiB 2.203MiB 355.000B
Postgres (gzip) 873.197KiB 873.061KiB 139.000B
Mysql 2.169MiB 2.169MiB 363.000B
Mysql (gzip) 858.697KiB 858.689KiB 8.000B
Sqlite 2.078MiB 2.078MiB 363.000B
Sqlite (gzip) 822.377KiB 822.276KiB 104.000B
SQL Server 2.135MiB 2.135MiB 363.000B
SQL Server (gzip) 846.997KiB 846.988KiB 9.000B
CockroachDB 2.225MiB 2.225MiB 355.000B
CockroachDB (gzip) 882.047KiB 882.025KiB 23.000B

WASM Query Compiler File Size

Engine This PR Base branch Diff
Postgres 1.832MiB 1.831MiB 369.000B
Postgres (gzip) 723.528KiB 723.581KiB -54.000B
Mysql 1.797MiB 1.796MiB 369.000B
Mysql (gzip) 713.159KiB 713.029KiB 133.000B
Sqlite 1.749MiB 1.749MiB 369.000B
Sqlite (gzip) 693.619KiB 693.602KiB 17.000B
SQL Server 1.835MiB 1.834MiB 370.000B
SQL Server (gzip) 728.916KiB 728.917KiB -2.000B
CockroachDB 1.855MiB 1.854MiB 367.000B
CockroachDB (gzip) 732.532KiB 732.501KiB 31.000B

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.

1 participant