Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:24kchengYe MemoMind Batch Database Storage

From Leeroopedia
Knowledge Sources
Domains Database, Data_Storage
Last Updated 2026-10-28 00:00 GMT

Overview

Technique that inserts multiple facts into a PostgreSQL database in a single batch operation using `unnest` for column arrays, minimizing round-trips and enabling transactional atomicity.

Description

Inserting facts one by one requires N database round-trips for N facts. Batch storage uses PostgreSQL's `unnest` to pass all column values as arrays in a single query, then `INSERT ... SELECT FROM unnest(...)` to insert all rows atomically.

This provides:

  • Performance: one query instead of N
  • Atomicity: all facts in a batch succeed or fail together
  • Search vector population: the query also computes the BM25 search vector (via `tokenize` for VectorChord or `GENERATED ALWAYS` for native PostgreSQL)

The stored facts include text, embedding vector, temporal fields (event_date, occurred_start, occurred_end, mentioned_at), context, fact_type, confidence_score, metadata, chunk_id, document_id, tags, observation_scopes, and text_signals (entity names + date tokens for enriched BM25 indexing).

Usage

Use this principle when storing multiple facts per retain operation. It is the persistence layer of the retain pipeline, called after fact extraction, embedding, and entity resolution.

Theoretical Basis

-- Abstract batch insert (simplified SQL)
WITH input_data AS (
    SELECT * FROM unnest(
        $2::text[], $3::vector[], $4::timestamptz[], ...
    ) AS t(text, embedding, event_date, ...)
)
INSERT INTO memory_units (bank_id, text, embedding, ...)
SELECT $1, text, embedding, ...
FROM input_data
RETURNING id

The `text_signals` column enriches BM25 indexing by concatenating entity names and date tokens with the fact text, so searches for entity names or dates match even when the fact text doesn't contain them literally.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment