Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(facts): partial index on facts(superseded_by) so fact deletes stop scanning facts
garrytan95ed4c3d3eed
diff --git a/src/core/schema-migrations/latest.generated.ts b/src/core/schema-migrations/latest.generated.tsindex 38bbe95f83..c36bc50268 100644--- a/src/core/schema-migrations/latest.generated.ts+++ b/src/core/schema-migrations/latest.generated.ts@@ -4,4 +4,4 @@// The highest schema migration version (migrate.ts LATEST_VERSION), importable without// loading the migrations themselves.-export const LATEST_SCHEMA_VERSION = 270;+export const LATEST_SCHEMA_VERSION = 271;diff --git a/src/core/schema-migrations/registry.generated.ts b/src/core/schema-migrations/registry.generated.tsindex 416ba8eac7..2c553806d9 100644--- a/src/core/schema-migrations/registry.generated.ts+++ b/src/core/schema-migrations/registry.generated.ts@@ -271,6 +271,7 @@ import { v267 } from './v267-pages-title-search-index.ts';import { v268 } from './v268-effect-claim-owner.ts';import { v269 } from './v269-links-supersedes-index.ts';import { v270 } from './v270-pages-source-created-index.ts';+import { v271 } from './v271-facts-superseded-by-index.ts';// Array order is historical (see HISTORICAL_ARRAY_ORDER in the generator); the// runner sorts by version before applying.@@ -540,4 +541,5 @@ export const MIGRATIONS: Migration[] = [v268,v269,v270,+ v271,];diff --git a/src/core/schema-migrations/v271-facts-superseded-by-index.ts b/src/core/schema-migrations/v271-facts-superseded-by-index.tsnew file mode 100644index 0000000000..698d541c7e--- /dev/null+++ b/src/core/schema-migrations/v271-facts-superseded-by-index.ts@@ -0,0 +1,33 @@+import type { Migration } from './types.ts';+import { buildIndexOnline, migrationNotice } from './helpers.ts';++// Applied by src/core/migrate.ts on the next initSchema(); see docs/ENGINES.md+// ("Canonical schema sources") for when schema.sql or a TS fragment also changes.+//+// facts.superseded_by is a self-referencing FK (ON DELETE SET NULL, v167) with+// no index. Every DELETE on facts (clearConversationFacts, page purge, rename,+// ontology rekey) makes Postgres look up referencing rows with+// `superseded_by = <deleted id>`, and the conversation clear's+// `EXISTS (SELECT 1 FROM facts g WHERE g.superseded_by = f.id)` guard asks the+// same question: with no index each lookup is a Seq Scan of facts, so a+// delete's cost grows with the whole brain. Only superseded rows carry a value,+// so the partial index stays small. Built online (CONCURRENTLY on Postgres);+// an interrupted build leaves an INVALID index that the next initSchema drops+// and rebuilds. Not in schema.sql: the facts table is created by migrations+// (v045), not by the schema blob, so a fresh install gets the index here too.+export const FACTS_SUPERSEDED_BY_INDEX = {+ name: 'idx_facts_superseded_by',+ table: 'facts',+ sql: 'CREATE INDEX IF NOT EXISTS idx_facts_superseded_by ON facts (superseded_by) WHERE superseded_by IS NOT NULL',+};++export const v271: Migration = {+ version: 271,+ name: 'facts_superseded_by_index',+ idempotent: true,+ transaction: false,+ sql: '',+ handler: async engine => {+ await buildIndexOnline(engine, 271, FACTS_SUPERSEDED_BY_INDEX, { notice: migrationNotice });+ },+};diff --git a/test/e2e/facts-superseded-by-index-postgres.test.ts b/test/e2e/facts-superseded-by-index-postgres.test.tsnew file mode 100644index 0000000000..19d1b11437--- /dev/null+++ b/test/e2e/facts-superseded-by-index-postgres.test.ts@@ -0,0 +1,3 @@+import { registerPostgresTests } from '../helpers/test-backends.ts';++await registerPostgresTests(() => import('../facts-superseded-by-index.test.ts'));diff --git a/test/facts-superseded-by-index.test.ts b/test/facts-superseded-by-index.test.tsnew file mode 100644index 0000000000..d319a99d59--- /dev/null+++ b/test/facts-superseded-by-index.test.ts@@ -0,0 +1,94 @@+/**+ * facts.superseded_by is a self-referencing FK (ON DELETE SET NULL). Deleting a fact makes the+ * database find the rows that point at it (the RI action `UPDATE ONLY facts SET superseded_by = NULL+ * WHERE superseded_by = $1`), and clearConversationFacts' `EXISTS (... g.superseded_by = f.id)` guard+ * asks the same question. With no index on superseded_by each lookup was a Seq Scan of the whole facts+ * table, so every fact DELETE cost grew with the brain. Protects: initSchema leaves a valid partial index+ * idx_facts_superseded_by, and both lookup shapes plan an index scan on it over a seeded, analyzed facts+ * table. Control: without the index the same statements read facts with a Seq Scan, and rerunning the+ * migration from the version before it rebuilds the index. PGLite always, Postgres when DATABASE_URL is+ * set (testBackends; test/e2e/facts-superseded-by-index-postgres.test.ts runs the Postgres arm).+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import type { BrainEngine } from '../src/core/engine.ts';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { MIGRATIONS, runMigrations } from '../src/core/migrate.ts';+import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';+import { testBackends } from './helpers/test-backends.ts';++const backends = testBackends();+const engines: Array<{ backend: string; engine: BrainEngine }> = [];+let closePostgres: (() => Promise<void>) | undefined;++beforeAll(async () => {+ if (backends.includes('pglite')) {+ const engine = new PGLiteEngine();+ await engine.connect({}); await engine.initSchema();+ engines.push({ backend: 'pglite', engine });+ }+ if (backends.includes('postgres')) {+ const pg = await isolatedPersistencePostgres(process.env.DATABASE_URL!);+ engines.push({ backend: 'postgres', engine: pg.engine }); closePostgres = pg.close;+ }+}, 120_000);++afterAll(async () => {+ for (const { engine } of engines) await engine.disconnect();+ await closePostgres?.();+});++async function plans(engine: BrainEngine): Promise<string[]> {+ const [target] = await engine.executeRaw<{ id: number }>('SELECT max(superseded_by)::int AS id FROM facts');+ const id = Number(target!.id);+ const explain = async (statement: string) => (await engine.executeRaw<{ 'QUERY PLAN': string }>(`EXPLAIN ${statement}`))+ .map(r => r['QUERY PLAN']).join('\n');+ return [+ await explain(`SELECT 1 FROM facts WHERE superseded_by = ${id}`),+ await explain(`UPDATE ONLY facts SET superseded_by = NULL WHERE superseded_by = ${id}`),+ ];+}++describe('facts (superseded_by) partial index for fact deletes', () => {+ for (const backend of backends) {+ const engineFor = () => engines.find(e => e.backend === backend)!.engine;++ test(`[${backend}] initSchema builds a valid partial index on facts (superseded_by)`, async () => {+ const rows = await engineFor().executeRaw<{ indexdef: string; valid: boolean }>(+ `SELECT i.indexdef, x.indisvalid AS valid FROM pg_indexes i JOIN pg_index x ON x.indexrelid = to_regclass(i.indexname)+ WHERE i.tablename = 'facts' AND i.indexname = 'idx_facts_superseded_by'`);+ expect(rows).toHaveLength(1);+ expect(rows[0]!.valid).toBe(true);+ expect(rows[0]!.indexdef).toContain('(superseded_by)');+ expect(rows[0]!.indexdef).toContain('WHERE (superseded_by IS NOT NULL)');+ });++ test(`[${backend}] the superseded_by lookup and the FK set-null action read facts through that index`, async () => {+ const engine = engineFor();+ await engine.executeRaw(+ `INSERT INTO facts (source_id, fact, source, context)+ SELECT 'default', 'Probe fact ' || g, 'superseded-index-probe', repeat('x', 200) FROM generate_series(1, 20000) g`);+ await engine.executeRaw(+ `UPDATE facts f SET superseded_by = n.id, expired_at = now() FROM facts n+ WHERE f.source = 'superseded-index-probe' AND f.id % 20 = 0 AND n.id = f.id + 1`);+ await engine.executeRaw('ANALYZE facts');+ for (const plan of await plans(engine)) {+ expect(plan).toMatch(/Index (Only )?Scan using idx_facts_superseded_by on facts/);+ expect(plan).not.toContain('Seq Scan on facts');+ }+ });++ test(`[${backend}] control: without the index both lookups Seq Scan facts, and the migration rebuilds it`, async () => {+ const engine = engineFor();+ const latest = await engine.getConfig('version');+ await engine.executeRaw('DROP INDEX idx_facts_superseded_by');+ for (const plan of await plans(engine)) expect(plan).toContain('Seq Scan on facts');+ const version = MIGRATIONS.find(m => m.name === 'facts_superseded_by_index')!.version;+ await engine.setConfig('version', String(version - 1));+ await runMigrations(engine);+ const [row] = await engine.executeRaw<{ valid: boolean }>(`SELECT indisvalid AS valid FROM pg_index WHERE indexrelid = to_regclass('idx_facts_superseded_by')`);+ expect(row?.valid).toBe(true);+ expect(await engine.getConfig('version')).toBe(latest);+ for (const plan of await plans(engine)) expect(plan).toMatch(/Index (Only )?Scan using idx_facts_superseded_by on facts/);+ });+ }+});diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-default.json b/test/fixtures/goldens/catalog/pglite-engine-init-default.jsonindex 7b7e693366..cdaca08757 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-default.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-default.json@@ -1181,6 +1181,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json b/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.jsonindex 195f585823..204bb14b3f 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json@@ -1181,6 +1181,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json b/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.jsonindex 4c946e50b3..a408e9f28f 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json@@ -1179,6 +1179,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-default.json b/test/fixtures/goldens/catalog/postgres-engine-init-default.jsonindex 34ef89e510..703310949f 100644--- a/test/fixtures/goldens/catalog/postgres-engine-init-default.json+++ b/test/fixtures/goldens/catalog/postgres-engine-init-default.json@@ -1183,6 +1183,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json b/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.jsonindex 6daaa770ef..d79f491df8 100644--- a/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json@@ -1183,6 +1183,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json b/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.jsonindex 9ff262608d..a6c66a3a65 100644--- a/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json+++ b/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json@@ -1181,6 +1181,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",diff --git a/test/fixtures/goldens/migrations/records.json b/test/fixtures/goldens/migrations/records.jsonindex f1791ee8dd..69667b876e 100644--- a/test/fixtures/goldens/migrations/records.json+++ b/test/fixtures/goldens/migrations/records.json@@ -1,6 +1,6 @@{"golden": {- "count": 265,+ "count": 266,"gaps": [17,18,@@ -15,6 +15,7 @@"CONTENT_CHUNKS_GENERATION_INDEX": "c434b3a4acdad499cf3b9f170d9f9967b8e3e8218c829db99f3ecba3b53c8b5d","CONTENT_CHUNKS_OCCURRENCE_GENERATION_INDEX": "b4f73db97ad878ef7256128ddef83fd11e2e08fd215e389d9e921a0daf04853d","CONTENT_CHUNKS_OCCURRENCE_INDEX": "7b692ba777b30399ab5a3b100eb40b3b9d1b3fb2322dc4dc808ba8a4d667912c",+ "FACTS_SUPERSEDED_BY_INDEX": "555c2b267989359a078de0f82cea830584bc0a268be6796bf526c0a9bed45905","FACT_PURGES_ALL_SUBJECTS_INDEX": "99e2eb6c24134b539cb4cfd6e5ff66a724463525d09e9ba1fdde3c13f5787b6c","FACT_PURGES_SUBJECT_INDEX": "f4fba07082f2e26da3fa51b0b689f3fe78a3d09afd20c1924dbc2fbe6b1560f1","FACT_RELINK_TABLE_SQL": "1fdde4b99d0077365274096f294be64a992961c7b4544af794224de238b03630",@@ -75,7 +76,7 @@"tableRows": "906d2413b13d76a06d5910885e47ed5c544be4ff3a2266e83146715721b24270","verifyProjectionStatistics": "0d231b04ecfbc5a4109d6fca9567bf4e8c9e9db6c3fbbb35212e026579ed6f67"},- "latest": 270,+ "latest": 271,"records": [{"handler": {@@ -4330,6 +4331,35 @@"transaction": false,"verify": "absent","version": 270+ },+ {+ "handler": {+ "form": "arrow",+ "helpers": [+ "FACTS_SUPERSEDED_BY_INDEX",+ "PLANNER_REPAIR_TABLES",+ "analyzeAfterIndexBuild",+ "analyzeDeferred",+ "analyzeYieldingToVacuum",+ "buildIndexOnline",+ "dropInvalidConcurrentIndex",+ "lockTableForAnalyze",+ "migrationNotice",+ "quietMigrationNotices",+ "tableRows"+ ],+ "sha256": "da2e5f805da6de37f972aa014bc83ce70e66c6f2d7f0485497efef6ae16f656b"+ },+ "idempotent": true,+ "name": "facts_superseded_by_index",+ "sql": {+ "length": 0,+ "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"+ },+ "sqlFor": "absent",+ "transaction": false,+ "verify": "absent",+ "version": 271}],"versions": [@@ -4597,7 +4627,8 @@267,268,269,- 270+ 270,+ 271]},"normalizer": "migrations-record-v1"diff --git a/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.json b/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.jsonindex e419515fc1..57f3ddf46f 100644--- a/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.json+++ b/test/fixtures/goldens/pglite-upgrade-replay/catalog-after-boot.json@@ -1181,6 +1181,7 @@"idx_facts_session": "CREATE INDEX idx_facts_session ON public.facts USING btree (source_id, source_session, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since": "CREATE INDEX idx_facts_since ON public.facts USING btree (source_id, created_at DESC) WHERE (expired_at IS NULL)","idx_facts_since_event_time": "CREATE INDEX idx_facts_since_event_time ON public.facts USING btree (source_id, COALESCE(valid_from, created_at) DESC) WHERE (expired_at IS NULL)",+ "idx_facts_superseded_by": "CREATE INDEX idx_facts_superseded_by ON public.facts USING btree (superseded_by) WHERE (superseded_by IS NOT NULL)","idx_facts_unconsolidated": "CREATE INDEX idx_facts_unconsolidated ON public.facts USING btree (source_id, entity_slug) WHERE ((consolidated_at IS NULL) AND (expired_at IS NULL))","idx_facts_unembedded": "CREATE INDEX idx_facts_unembedded ON public.facts USING btree (source_id) WHERE ((expired_at IS NULL) AND ((embedding IS NULL) OR (embedded_text_hash IS DISTINCT FROM md5(fact))))","idx_facts_unlinked_active": "CREATE INDEX idx_facts_unlinked_active ON public.facts USING btree (source_id, id) WHERE ((entity_slug IS NULL) AND (expired_at IS NULL))",