Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(db): partial index on (identityId, scope) WHERE scope IS NOT NULL

Claude68bf911969ab
diff --git a/backend/src/db/migrations/20260513003124_add-scope-index-to-identity-access-token-revocations.ts b/backend/src/db/migrations/20260513003124_add-scope-index-to-identity-access-token-revocations.ts
new file mode 100644
index 0000000000..e26b82e6f4
--- /dev/null
+++ b/backend/src/db/migrations/20260513003124_add-scope-index-to-identity-access-token-revocations.ts
@@ -0,0 +1,34 @@
+import { Knex } from "knex";
+
+import { TableName } from "../schemas";
+
+const SCOPE_INDEX = "idx_identity_access_token_revocations_identityid_scope";
+const MIGRATION_TIMEOUT = 5 * 60 * 1000;
+
+export async function up(knex: Knex): Promise<void> {
+ const result = await knex.raw("SHOW statement_timeout");
+ const originalTimeout = result.rows[0].statement_timeout;
+
+ try {
+ await knex.raw(`SET statement_timeout = ${MIGRATION_TIMEOUT}`);
+
+ // Partial index on the scoped-marker subset so the validator's
+ // `... AND (id IN (...) OR scope IN (...))` lookup gets a real index
+ // path on the scope side instead of relying on the planner's row-count
+ // estimate for the OR. CONCURRENTLY so the build doesn't block writes.
+ await knex.raw(`
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS ${SCOPE_INDEX}
+ ON ${TableName.IdentityAccessTokenRevocation} ("identityId", "scope")
+ WHERE "scope" IS NOT NULL
+ `);
+ } finally {
+ await knex.raw(`SET statement_timeout = '${originalTimeout}'`);
+ }
+}
+
+export async function down(knex: Knex): Promise<void> {
+ await knex.raw(`DROP INDEX IF EXISTS ${SCOPE_INDEX}`);
+}
+
+const config = { transaction: false };
+export { config };