Owner of the schema: WP-C after Phase 0 (db/migrations/). ADRs 0010, 0019, 0021, 0011, 0305. Schema contract: db/migrations/ (0001_init.sql, then additive files: 0002_tier_roles.sql per-tier roles, 0003_realtime_credential_reads.sql, 0004_rate_buckets_and_session_ids.sql, 0005_tail_deletes.sql).
| Table | Written by | Purpose |
|—|—|—|
| users, sessions, api_tokens | API | accounts, hashed session tokens + CSRF secret (+ public id, ADR 0307), hashed PATs with scopes |
| api_rate_buckets (UNLOGGED) | API | global rate-limit buckets, synced by every replica every 250 ms (ADR 0306) |
| projects, project_members | API | tenants and roles (owner/editor/viewer) |
| files | API; realtime (next_client_id blocks) | files; clientId allocation |
| pages | API; realtime (head_seq, tail_*, batched <= 1/s); compactor (snapshot_seq, node_count, tail_* decrements) | pages and compaction triggers (tail_ops, tail_bytes, tail_deletes = accepted DELETE ops, ADR 0312); sort_key is a fractional key (same algorithm as node keys) |
| page_ops | realtime (COPY) | op journal: one row per accepted batch, (page_id, seq) PK, (page_id, client_id, client_seq) index for dedupe |
| page_snapshots | compactor; API inserts only the seq-0 row of a new page (ADR 0305) | snapshot metadata (seq, sizes, rules version, content hash, blob key) |
| page_snapshot_chunks | compactor; API inserts only the seq-0 chunks of a new page (ADR 0305) | fallback storage of snapshot/structure gzip streams in 1 MiB chunks |
| compaction_leases | compactor | job claims |
| audit_log | API | membership, role, token, deletion and login-failure events (actions in security.md) |
API write pattern. Request paths write nothing per read: session sliding expiry at most once per hour per session, PAT last_used_at in one batched UPDATE every 30 s, login failures aggregated per minute. Every relational change that affects access or file metadata sends pg_notify inside its transaction: linora_auth (credential revoked, member added/removed/role changed, project/file deleted) and linora_file (pages changed, file renamed); payloads are defined once in packages/server-common/src/auth-events.ts.
Empty pages. Every page has a snapshot from birth: the API inserts the canonical empty page (root only) as seq 0 in page_snapshots + two page_snapshot_chunks rows (snapshot and structure, gzip, ~40 bytes each) in the transaction that creates the page, so readers (realtime index load, doc workers, compactor) never special-case “no snapshot”.
Roles (SR-07, ADR 0310). Migration 0002 creates one group role per tier (linora_api, linora_realtime, linora_docworker, linora_compactor). Table and column grants enforce the write ownership above: doc workers read only; the compactor reads only token_hash, expires_at of sessions. Each process connects as its tier’s LOGIN user (its own DATABASE_URL), and migrations run as the table owner. Every migration that adds a table grants it to the tiers that use it, in the same file.
Canonical columnar encoding from packages/doc (document-core.md), gzip-compressed:
pages/<pageId>/<seq>.snap.gz: full page (served to browsers as-is with Content-Encoding: gzip; ETag = pageId:seq. The file is immutable, but GET .../snapshot serves the newest one, so browsers revalidate: no-cache, ADR 0311).pages/<pageId>/<seq>.struct.gz: structure section for the realtime structural index (13 B/node raw: sorted ids, parent slots, kinds).Sizes: ~15-20 B/node for snapshots gzipped (682K nodes ~12 MB), ~7 B/node for structure sections.
busy when commit latency > 200 ms or an owner’s backlog > 64 MiB.One three-call interface for every tier (@linorahq/server-common blob/): read (stream; BlobNotFound), write (atomic), remove.
LINORA_BLOB_STORE=local: LINORA_BLOB_DIR, temp file + fsync + rename. Dev and single host.LINORA_BLOB_STORE=s3: any S3-compatible bucket (LINORA_S3_*), SigV4 on node:crypto, no SDK. Single PUT up to 16 MiB, multipart above. Retries with jittered backoff, and mid-stream GET resume with Range + If-Match.Snapshots from the blob store (immutable files; the download URL is revalidated by ETag, ADR 0311); Postgres chunks only when no blob store is configured. The API streams chunks in 128 KiB substring slices (STORAGE EXTERNAL makes these partial TOAST reads), one slice in flight per download. Journal tails: owner ring (<= 256 KB/room), else one Postgres read per room shared by all waiting subscribers. The API service and doc workers read through their own small pools (optionally a read replica).
Realtime: 1 per owner thread + 1 LISTEN. API: 10-20 per instance (PgBouncer in transaction mode when replicated). Doc workers: 2-4. Compactors: 1 each. Never one per user.