Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

3.0 KB
Raw Blame
import { performance } from 'node:perf_hooks'
import type { RelayDatabase, SqlRow } from './database.js'

export type HeapWindowReapBudget = {
  // Pages one DELETE may visit, so its row locks and WAL stay a few hundred rows.
  pagesPerStatement: number
  // Pages one tick may visit while it finds nothing to delete.
  maxPagesPerTick: number
  // Rows one tick deletes before it stops; this is what paces a backlog over days.
  maxRowsPerTick: number
  budgetMs: number
}

// Unindexed retention LIMIT scans restart at page 0 and worsen as the head empties (production EXPLAIN 2026-10-04); TID windows bound each statement’s pages.
export class HeapWindowReaper {
  private nextPage: number | undefined

  constructor(
    private readonly table: string,
    private readonly predicate: string,
    private readonly budget: HeapWindowReapBudget,
    private readonly random: () => number = Math.random,
    private readonly clock: () => number = () => performance.now()
  ) {}

  async reap(database: RelayDatabase, params: unknown[]): Promise<number> {
    if (database.dialect !== 'postgres') {
      return changes(
        await database.query(
          `DELETE FROM ${this.table} WHERE rowid IN (
             SELECT rowid FROM ${this.table} WHERE ${this.predicate}
             LIMIT ${this.budget.maxRowsPerTick}
           )`,
          params
        )
      )
    }
    const pages = await heapPages(database, this.table)
    if (pages === 0) return 0
    // A random first page: every director runs this sweep, and walks that all start at page 0
    // after a rollout would read the same pages in lockstep.
    let page = this.nextPage ?? Math.floor(this.random() * pages)
    const startedAt = this.clock()
    let scanned = 0
    let deleted = 0
    while (
      scanned < this.budget.maxPagesPerTick &&
      deleted < this.budget.maxRowsPerTick &&
      this.clock() - startedAt < this.budget.budgetMs
    ) {
      if (page >= pages) page = 0
      const end = Math.min(page + this.budget.pagesPerStatement, pages)
      // SKIP LOCKED defers busy rows; ANY(TID array) guarantees TID scan while IN may hash-join a full sequential scan.
      deleted += changes(
        await database.query(
          `DELETE FROM ${this.table} WHERE ctid = ANY(ARRAY(
             SELECT ctid FROM ${this.table}
             WHERE ctid >= CAST(? AS tid) AND ctid < CAST(? AS tid) AND ${this.predicate}
             FOR UPDATE SKIP LOCKED
           ))`,
          [`(${page},0)`, `(${end},0)`, ...params]
        )
      )
      scanned += end - page
      page = end
    }
    this.nextPage = page
    return deleted
  }
}

async function heapPages(database: RelayDatabase, table: string): Promise<number> {
  const row = (
    await database.query(
      `SELECT pg_relation_size(CAST(? AS regclass)) / current_setting('block_size')::bigint
         AS pages`,
      [table]
    )
  )[0]
  return Number(row?.pages ?? 0)
}

export function changes(rows: SqlRow[]): number {
  return Number(rows[0]?.changes ?? 0)
}