Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(repair): child-orphan predicate uses a correlated NOT EXISTS anti-join (#6084)

time-attack74352ec50003
diff --git a/src/core/repair/orphan-children.ts b/src/core/repair/orphan-children.ts
index 7e98360824..aaf4294766 100644
--- a/src/core/repair/orphan-children.ts
+++ b/src/core/repair/orphan-children.ts
@@ -29,8 +29,14 @@ export const PAGE_CHILD_FK_TARGETS: ReadonlyArray<{ table: string; col: string;
{ table: 'files', col: 'page_id', allowNull: true },
];
-export function orphanPredicate(target: { col: string; allowNull: boolean }): string {
- return `${target.allowNull ? `${target.col} IS NOT NULL AND ` : ''}${target.col} NOT IN (SELECT id FROM pages)`;
+// NOT EXISTS (not `NOT IN (SELECT id FROM pages)`) so the planner can use a
+// hash anti-join: once the page-id list outgrows work_mem, NOT IN degrades to a
+// per-row rescan of pages. pages.id is the primary key (never NULL), so the
+// semantics match NOT IN exactly. The column is table-qualified because the
+// predicate also runs inside the repair's UPDATE and DELETE statements.
+export function orphanPredicate(target: { table: string; col: string; allowNull: boolean }): string {
+ const col = `${target.table}.${target.col}`;
+ return `${target.allowNull ? `${col} IS NOT NULL AND ` : ''}NOT EXISTS (SELECT 1 FROM pages p WHERE p.id = ${col})`;
}
const PROBE_BATCH = 500;
diff --git a/test/doctor-child-orphans.test.ts b/test/doctor-child-orphans.test.ts
index ecf4a2380d..7bd6d14c4b 100644
--- a/test/doctor-child-orphans.test.ts
+++ b/test/doctor-child-orphans.test.ts
@@ -76,10 +76,10 @@ describe('childTableOrphansCheck (#1063)', () => {
// (NULL is a valid SET NULL outcome, not an orphan).
const filesSql = capturedSql.find((s) => s.includes('FROM files WHERE'));
expect(filesSql).toBeDefined();
- expect(filesSql!).toContain('page_id IS NOT NULL AND page_id NOT IN');
+ expect(filesSql!).toContain('files.page_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM pages p WHERE p.id = files.page_id)');
const linksOrigSql = capturedSql.find((s) => s.includes('FROM links WHERE') && s.includes('origin_page_id'));
expect(linksOrigSql).toBeDefined();
- expect(linksOrigSql!).toContain('origin_page_id IS NOT NULL AND origin_page_id NOT IN');
+ expect(linksOrigSql!).toContain('links.origin_page_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM pages p WHERE p.id = links.origin_page_id)');
// NOT-NULL FK tables MUST NOT have the IS NOT NULL filter (it'd be redundant)
const ccSql = capturedSql.find((s) => s.includes('FROM content_chunks WHERE'));
expect(ccSql).toBeDefined();
diff --git a/test/e2e/repair-orphan-children-plan-postgres.test.ts b/test/e2e/repair-orphan-children-plan-postgres.test.ts
new file mode 100644
index 0000000000..8e04d4742b
--- /dev/null
+++ b/test/e2e/repair-orphan-children-plan-postgres.test.ts
@@ -0,0 +1,73 @@
+/**
+ * #6084 / #5328: doctor `child_table_orphans` and `gbrain repair
+ * orphan-children` share `orphanPredicate`. `col NOT IN (SELECT id FROM pages)`
+ * plans as a per-row SubPlan once the page ids outgrow work_mem, so the count
+ * never finishes on a large brain. The correlated NOT EXISTS plans as an
+ * anti-join. This pins the Postgres plan under a work_mem smaller than the
+ * page-id set, and the repair semantics (orphans removed, valid and allowed-NULL
+ * references kept) on real Postgres.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import type { OperationContext } from '../../src/core/ops/contract.ts';
+import { resolveRepairScope, runRepair } from '../../src/core/repair/core.ts';
+import { PAGE_CHILD_FK_TARGETS, orphanChildrenRepair, orphanPredicate } from '../../src/core/repair/orphan-children.ts';
+import { childTableOrphansCheck } from '../../src/commands/doctor/checks/core-health.ts';
+import { getEngine, hasDatabase, setupDB, teardownDB } from './helpers.ts';
+
+const describePg = hasDatabase() ? describe : describe.skip;
+
+describePg('orphan-children predicate on Postgres (#6084)', () => {
+ beforeAll(async () => {
+ const engine = await setupDB();
+ for (const slug of ['notes/kept-example', 'notes/removed-example']) {
+ await engine.putPage(slug, { type: 'note', title: slug, compiled_truth: `# ${slug}` }, { sourceId: 'default' });
+ }
+ await engine.executeRaw(`INSERT INTO pages (source_id, slug, type, title, compiled_truth)
+ SELECT 'default', 'notes/bulk-' || g, 'note', 'bulk', '' FROM generate_series(1, 6000) g`);
+ const ids = Object.fromEntries((await engine.executeRaw<{ slug: string; id: number }>(
+ "SELECT slug, id FROM pages WHERE slug IN ('notes/kept-example', 'notes/removed-example')")).map(r => [r.slug, Number(r.id)]));
+ await engine.executeRaw(`INSERT INTO tags (page_id, tag) VALUES ($1, 'valid-tag')`, [ids['notes/kept-example']]);
+ await engine.executeRaw(`INSERT INTO links (from_page_id, to_page_id, link_type, origin_page_id) VALUES ($1, $1, 'mention', NULL), ($1, $1, 'reference', $2)`,
+ [ids['notes/kept-example'], ids['notes/removed-example']]);
+ await engine.transaction(async tx => {
+ await tx.executeRaw('SET LOCAL session_replication_role = replica');
+ await tx.executeRaw('DELETE FROM pages WHERE id = $1', [ids['notes/removed-example']]);
+ await tx.executeRaw(`INSERT INTO tags (page_id, tag) VALUES ($1, 'orphan-tag')`, [ids['notes/removed-example']]);
+ });
+ await engine.executeRaw('ANALYZE pages');
+ await engine.executeRaw('ANALYZE tags');
+ }, 120_000);
+
+ afterAll(async () => { await teardownDB(); });
+
+ test('every target plans as an anti-join, never a per-row SubPlan, when page ids exceed work_mem', async () => {
+ const plans = await getEngine().transaction(async tx => {
+ await tx.executeRaw("SET LOCAL work_mem = '64kB'");
+ const out: Array<{ target: string; plan: string }> = [];
+ for (const target of PAGE_CHILD_FK_TARGETS) {
+ const rows = await tx.executeRaw<{ 'QUERY PLAN': string }>(
+ `EXPLAIN SELECT count(*)::int AS n FROM ${target.table} WHERE ${orphanPredicate(target)}`);
+ out.push({ target: `${target.table}.${target.col}`, plan: rows.map(r => r['QUERY PLAN']).join('\n') });
+ }
+ return out;
+ });
+ for (const { target, plan } of plans) {
+ expect({ target, subplan: /SubPlan/.test(plan) }).toEqual({ target, subplan: false });
+ expect({ target, antiJoin: /Anti Join/.test(plan) }).toEqual({ target, antiJoin: true });
+ }
+ });
+
+ test('doctor warns, the repair removes only orphans, valid and NULL references survive', async () => {
+ const engine = getEngine();
+ const before = await childTableOrphansCheck(engine);
+ expect(before.status).toBe('warn');
+ expect(before.details?.orphans).toEqual(['tags.page_id=1', 'links.origin_page_id=1']);
+ const ctx = { engine, config: { engine: 'postgres' }, remote: false, logger: console } as unknown as OperationContext;
+ const applied = await runRepair(ctx, orphanChildrenRepair, await resolveRepairScope(engine), { apply: true, explicit: true });
+ expect(applied.applied).toBe(2);
+ expect((await engine.executeRaw<{ tag: string }>('SELECT tag FROM tags ORDER BY tag')).map(r => r.tag)).toEqual(['valid-tag']);
+ const links = await engine.executeRaw<{ link_type: string; origin_page_id: number | null }>('SELECT link_type, origin_page_id FROM links ORDER BY link_type');
+ expect(links).toEqual([{ link_type: 'mention', origin_page_id: null }, { link_type: 'reference', origin_page_id: null }]);
+ expect((await childTableOrphansCheck(engine)).status).toBe('ok');
+ });
+});