homelab-codex-ws/services/kb-postgres
oskar e5fcc2d4d9 feat(kb-postgres): migration 002_chunks.sql — document_chunk table
Krok 1 planu docs/kb/modules/05-faza2-plan.md §3/§6 (chunk-level embeddings,
1:N do envelope). Addytywna — 001_envelope.sql nietknięta (zweryfikowane
\d envelope po migracji: identyczny schemat + FK jako "Referenced by").

Schemat wg rekomendacji recon (§2 decyzja 1+2): osobna tabela (nie kolumna
w envelope, bo N-wartościowy chunking jest obowiązkowy przy dokumentach
>8k tokenów), embedding VECTOR(1024) pod bge-m3 (dense), HNSW cosine index,
kolumna `model` do trywialnego re-indexu przy zmianie modelu (kb-00 zasada
#1: indeks odtwarzalny). Idempotentna (CREATE TABLE/INDEX IF NOT EXISTS,
zweryfikowane podwójnym uruchomieniem na kb-postgres@PIHA — drugi run same
NOTICE "already exists, skipping").

Zastosowana na żywej bazie: ssh piha docker exec kb-postgres psql, po
potwierdzeniu SQL przez Oskara. \dt + \d document_chunk + \d envelope
zweryfikowane po migracji.

Testy: dopisane sanity-testy 002 do packages/kb-mail/tests/test_migration.py
(wzorzec 001 — statyczne assercje na treści SQL, bez DB), 13/13 zielone.

Co NIE jest częścią tego kroku (§3 planu, odłożone): entity/entity_link
(graf encji) — szkic na przyszłość, nie blokuje domknięcia modułu 5.

Co dalej (plan §6, poza zakresem tego kroku): ollama pull bge-m3 na SOLARII,
token API Paperless, jobs/gmail-header-backfill/, adapter Paperless→koperta,
chunking+embed job.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-13 21:08:41 +02:00
..
init feat(kb-postgres): migration 002_chunks.sql — document_chunk table 2026-07-13 21:08:41 +02:00
docker-compose.yml feat(kb-mail): fundament — pgvector spine, koperta, archiwum, pakiet domeny 2026-06-19 20:02:25 +02:00
env.example feat(kb-mail): fundament — pgvector spine, koperta, archiwum, pakiet domeny 2026-06-19 20:02:25 +02:00
healthcheck.sh feat(kb-mail): fundament — pgvector spine, koperta, archiwum, pakiet domeny 2026-06-19 20:02:25 +02:00
README.md refactor(kb-postgres): relokacja SOLARIA→PIHA — arm64, mem_limit 1g, tuning pod małą maszynę 2026-06-22 19:59:58 +02:00
service.yaml refactor(kb-postgres): relokacja SOLARIA→PIHA — arm64, mem_limit 1g, tuning pod małą maszynę 2026-06-22 19:59:58 +02:00

kb-postgres

Postgres 16 + pgvector — KB spine on PIHA (Raspberry Pi 5, always-on). Stores the frozen envelope schema shared by all KB pillars (mails, documents, photos, transactions).

Runs here because the KB store must answer queries 24/7; SOLARIA (GPU/compute) is powered down intermittently. Embeddings/models still run on SOLARIA's GPU — only the Postgres+pgvector store lives on PIHA. The pgvector/pgvector:pg16 image is multi-arch and runs natively on arm64 (the Pi 5).

Port: 5433 on PIHA (Tailscale-accessible to other nodes).

Standard deploy (from SATURN)

# On SATURN — pushes to master, then deploy.sh SSHes to PIHA and runs deploy-node.sh
git push origin master
scripts/deploy/deploy.sh piha

deploy-node.sh on PIHA automatically picks up the per-host override:

docker compose \
  -f services/kb-postgres/docker-compose.yml \
  -f hosts/piha/runtime/kb-postgres/docker-compose.override.yml \
  up -d --remove-orphans

The PIHA override caps memory (mem_limit: 1g) and tunes Postgres for a tight, HA-shared RAM budget. Data lives in the kb_postgres_data named volume, which must land on the NVMe (Docker data-root on /home), never the SD card — verify before first deploy (see the override file's DATA PLACEMENT note):

# On PIHA
docker info -f '{{.DockerRootDir}}'                       # expect an NVMe path
df -h "$(docker info -f '{{.DockerRootDir}}')"            # confirm it's the NVMe

First-time setup on PIHA (before first deploy)

The .env file must exist at services/kb-postgres/.env in the PIHA repo checkout (alongside the compose file — that's where env_file: .env resolves to):

# On PIHA
cd ~/homelab-codex-ws
cp services/kb-postgres/env.example services/kb-postgres/.env
# Edit .env: set POSTGRES_PASSWORD to something strong

.env is gitignored (*.env rule in root .gitignore) — it will never be committed.

Manual one-off (debugging / first boot)

# On PIHA, from repo root
docker compose \
  -f services/kb-postgres/docker-compose.yml \
  -f hosts/piha/runtime/kb-postgres/docker-compose.override.yml \
  up -d

Verify after first boot

# Host-side healthcheck
./services/kb-postgres/healthcheck.sh

# Inside the container
docker exec kb-postgres psql -U kb -d kb -c '\d envelope'
docker exec kb-postgres psql -U kb -d kb \
  -c "SELECT extname FROM pg_extension WHERE extname = 'vector';"

Expected \d envelope output:

                    Table "public.envelope"
  Column  |           Type           | Nullable |  Default
----------+--------------------------+----------+-----------
 id       | text                     | not null |
 source   | text                     | not null |
 ts       | timestamp with time zone | not null |
 geo      | jsonb                    |          |
 raw_ref  | text                     | not null |
 entities | jsonb                    | not null | '[]'::jsonb
Indexes:
    "envelope_pkey" PRIMARY KEY, btree (id)
    "envelope_source_idx" btree (source)
    "envelope_ts_idx" btree (ts)

Schema contract

The envelope table is the frozen cross-source envelope (see docs/kb/kb-00-overview.md §Zasady przekrojowe). Adding columns is OK; removing or renaming existing ones is NOT.

Future migrations go in init/ as 002_*.sql, 003_*.sql, …. Postgres runs initdb scripts only on a fresh volume — for existing instances apply migrations with psql directly.

Connection string

postgresql://kb:<POSTGRES_PASSWORD>@piha:5433/kb

Set KB_TEST_DSN to this value when running integration tests from packages/kb-mail/ (host = piha over Tailscale).