|
| 1 | +-- Replaces kcpii_subject_idx (import_id, subject_token) with the same unique key led by |
| 2 | +-- subject_token. |
| 3 | +-- |
| 4 | +-- Search resolves a reader's permission grants by subject_token alone (activeConnectorPermissionGrants), |
| 5 | +-- and the old key led with import_id, so that lookup scanned the whole index. Led by subject_token, |
| 6 | +-- it reads only the reader's identities, one per import that lists them. The uniqueness is the |
| 7 | +-- same column set, so the identity insert's untargeted ON CONFLICT DO NOTHING and the per-import |
| 8 | +-- conflict check (import_id plus subject_token) behave as before. The old index is dropped once |
| 9 | +-- the new one is built, so the uniqueness never lapses and identity inserts maintain the same |
| 10 | +-- number of indexes. |
| 11 | +-- |
| 12 | +-- Replay-safety: as 0446. The build and the drop are CONCURRENTLY below an embedded COMMIT, and a |
| 13 | +-- replay moves only an INVALID index left by a failed build aside to be dropped, keeping a valid one. |
| 14 | +DO $$ |
| 15 | +BEGIN |
| 16 | + IF EXISTS ( |
| 17 | + SELECT 1 FROM pg_index WHERE indexrelid = to_regclass('kcpii_subject_import_idx_failed') |
| 18 | + AND (indisvalid OR indrelid <> to_regclass('knowledge_connector_permission_import_identity')) |
| 19 | + ) THEN |
| 20 | + RAISE EXCEPTION 'Refusing to drop unexpected recovery index kcpii_subject_import_idx_failed'; |
| 21 | + END IF; |
| 22 | + IF EXISTS ( |
| 23 | + SELECT 1 FROM pg_index WHERE indexrelid = to_regclass('kcpii_subject_import_idx') |
| 24 | + AND indrelid = to_regclass('knowledge_connector_permission_import_identity') AND NOT indisvalid |
| 25 | + ) THEN |
| 26 | + ALTER INDEX "kcpii_subject_import_idx" RENAME TO "kcpii_subject_import_idx_failed"; |
| 27 | + END IF; |
| 28 | +END $$;--> statement-breakpoint |
| 29 | +COMMIT;--> statement-breakpoint |
| 30 | +SET lock_timeout = 0;--> statement-breakpoint |
| 31 | +DROP INDEX CONCURRENTLY IF EXISTS "kcpii_subject_import_idx_failed";--> statement-breakpoint |
| 32 | +CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS "kcpii_subject_import_idx" ON "knowledge_connector_permission_import_identity" USING btree ("subject_token","import_id");--> statement-breakpoint |
| 33 | +DROP INDEX CONCURRENTLY IF EXISTS "kcpii_subject_idx";--> statement-breakpoint |
| 34 | +SET lock_timeout = '5s'; |
0 commit comments