Skip to content

symfonic.memory.backends.postgres_claim_record_index

postgres_claim_record_index

Operator-installed index bounding claimed discard's claim-generation delete.

Discard removes every claim generation of a discarded row, keyed by exact owner (tenant_id, record_id, scope_path). The ledger keeps published and discarded history, so without a record-keyed index that delete either scans the tenant's ledger (custom plans) or filters a whole hot scope per row (generic plans). With this index each input row costs one descent plus its own generations, which the delete must remove anyway. Building it on a large ledger is an online operator step, so startup never creates it and discard refuses until a valid index of exactly this shape exists.

require_claim_record_index async

require_claim_record_index(conn: Any) -> None

Refuse a missing, invalid, partial, differently keyed or collated index.

Source code in src/symfonic/memory/backends/postgres_claim_record_index.py
async def require_claim_record_index(conn: Any) -> None:
    """Refuse a missing, invalid, partial, differently keyed or collated index."""
    valid = await conn.fetchval(
        "SELECT i.indisvalid AND i.indisready AND i.indislive "
        "AND i.indrelid='memory_pending_claims'::regclass AND NOT i.indisunique "
        "AND i.indnkeyatts=3 AND i.indnatts=3 "
        "AND i.indexprs IS NULL AND i.indpred IS NULL AND am.amname='btree' "
        "AND (SELECT array_agg(a.attname::text ORDER BY k.n) "
        "     FROM unnest(i.indkey::int2[]) WITH ORDINALITY AS k(attnum, n) "
        "     JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=k.attnum)=$2 "
        "AND (SELECT bool_and(c='pg_catalog.\"C\"'::regcollation) "
        "     FROM unnest(i.indcollation::oid[]) AS c) "
        "AND (SELECT bool_and(o=0) FROM unnest(i.indoption::int2[]) AS o) "
        "AND (SELECT bool_and(oc.opcname='text_ops' "
        "     AND oc.opcnamespace='pg_catalog'::regnamespace) "
        "     FROM unnest(i.indclass::oid[]) AS k(oid) JOIN pg_opclass oc ON oc.oid=k.oid) "
        "FROM pg_index i JOIN pg_class c ON c.oid=i.indexrelid "
        "JOIN pg_am am ON am.oid=c.relam "
        "WHERE i.indexrelid=to_regclass($1)",
        CLAIM_RECORD_INDEX, list(_KEYS),
    )
    if valid is not True:
        raise RuntimeError("CORE-199 claim record index missing or invalid")