db: Add pgsql schema
This commit is contained in:
parent
9c63b6c593
commit
d121f2a76d
|
|
@ -0,0 +1,5 @@
|
||||||
|
INSERT INTO komp.space(name, model, dim) VALUES
|
||||||
|
('dev_knowledge','mxbai-embed-large',1024),
|
||||||
|
('pattern_exchange','mxbai-embed-large',1024),
|
||||||
|
('runtime_memory','mxbai-embed-large',1024)
|
||||||
|
ON CONFLICT (name) DO NOTHING;
|
||||||
130
db/schema.sql
130
db/schema.sql
|
|
@ -1,43 +1,105 @@
|
||||||
-- Kompanion knowledge store (sqlite)
|
-- Requires: CREATE EXTENSION IF NOT EXISTS vector; CREATE EXTENSION IF NOT EXISTS ltree;
|
||||||
PRAGMA journal_mode=WAL;
|
|
||||||
PRAGMA synchronous=NORMAL;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS entries (
|
CREATE SCHEMA IF NOT EXISTS komp;
|
||||||
id INTEGER PRIMARY KEY,
|
CREATE EXTENSION IF NOT EXISTS vector;
|
||||||
ts TEXT NOT NULL,
|
CREATE EXTENSION IF NOT EXISTS ltree;
|
||||||
aspect TEXT,
|
|
||||||
tags TEXT,
|
CREATE TABLE IF NOT EXISTS komp.source (
|
||||||
text TEXT NOT NULL
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
kind TEXT NOT NULL, -- filesystem|repo|url|note
|
||||||
|
uri TEXT NOT NULL, -- path or URL
|
||||||
|
repo TEXT,
|
||||||
|
ref TEXT,
|
||||||
|
meta JSONB DEFAULT '{}'::jsonb,
|
||||||
|
created_at TIMESTAMPTZ DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE VIRTUAL TABLE IF NOT EXISTS entries_fts USING fts5(text, content="entries", content_rowid="id");
|
CREATE TABLE IF NOT EXISTS komp.chunk (
|
||||||
CREATE TRIGGER IF NOT EXISTS entries_ai AFTER INSERT ON entries BEGIN
|
id BIGSERIAL PRIMARY KEY,
|
||||||
INSERT INTO entries_fts(rowid, text) VALUES (new.id, new.text);
|
source_id BIGINT REFERENCES komp.source(id) ON DELETE CASCADE,
|
||||||
END;
|
lineno INT,
|
||||||
CREATE TRIGGER IF NOT EXISTS entries_ad AFTER DELETE ON entries BEGIN
|
text TEXT NOT NULL,
|
||||||
INSERT INTO entries_fts(entries_fts, rowid, text) VALUES(delete, old.id, old.text);
|
sha256 TEXT NOT NULL,
|
||||||
END;
|
tokens INT,
|
||||||
CREATE TRIGGER IF NOT EXISTS entries_au AFTER UPDATE ON entries BEGIN
|
created_at TIMESTAMPTZ DEFAULT now()
|
||||||
INSERT INTO entries_fts(entries_fts, rowid, text) VALUES(delete, old.id, old.text);
|
);
|
||||||
INSERT INTO entries_fts(rowid, text) VALUES (new.id, new.text);
|
CREATE INDEX IF NOT EXISTS idx_chunk_source ON komp.chunk(source_id);
|
||||||
END;
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS sources (
|
-- A space is a distinct memory with its own model+dim & policy
|
||||||
id INTEGER PRIMARY KEY,
|
CREATE TABLE IF NOT EXISTS komp.space (
|
||||||
file TEXT NOT NULL,
|
id SERIAL PRIMARY KEY,
|
||||||
sha TEXT,
|
name TEXT UNIQUE, -- dev_knowledge | pattern_exchange | runtime_memory
|
||||||
lineno INTEGER
|
model TEXT NOT NULL,
|
||||||
|
dim INT NOT NULL,
|
||||||
|
metric TEXT NOT NULL DEFAULT 'cosine'
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS vectors (
|
-- Embedding tables per common dimension (add more as needed)
|
||||||
id INTEGER PRIMARY KEY,
|
CREATE TABLE IF NOT EXISTS komp.embedding_768 (
|
||||||
entry_id INTEGER REFERENCES entries(id) ON DELETE CASCADE,
|
id BIGSERIAL PRIMARY KEY,
|
||||||
model TEXT NOT NULL,
|
chunk_id BIGINT REFERENCES komp.chunk(id) ON DELETE CASCADE,
|
||||||
dim INTEGER NOT NULL,
|
space_id INT REFERENCES komp.space(id) ON DELETE CASCADE,
|
||||||
vec BLOB NOT NULL
|
embedding VECTOR(768) NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ DEFAULT now(),
|
||||||
|
UNIQUE(chunk_id, space_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_embed768_space ON komp.embedding_768(space_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS ivf_embed768 ON komp.embedding_768 USING ivfflat (embedding vector_cosine_ops) WITH (lists=100);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS komp.embedding_1024 (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
chunk_id BIGINT REFERENCES komp.chunk(id) ON DELETE CASCADE,
|
||||||
|
space_id INT REFERENCES komp.space(id) ON DELETE CASCADE,
|
||||||
|
embedding VECTOR(1024) NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ DEFAULT now(),
|
||||||
|
UNIQUE(chunk_id, space_id)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_embed1024_space ON komp.embedding_1024(space_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS ivf_embed1024 ON komp.embedding_1024 USING ivfflat (embedding vector_cosine_ops) WITH (lists=100);
|
||||||
|
|
||||||
|
-- Branch hierarchy (Branch Embeddings): path encodes the cluster tree (e.g., physics.quantum.tunneling)
|
||||||
|
CREATE TABLE IF NOT EXISTS komp.branch (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
space_id INT REFERENCES komp.space(id) ON DELETE CASCADE,
|
||||||
|
path LTREE NOT NULL,
|
||||||
|
label TEXT,
|
||||||
|
meta JSONB DEFAULT '{}'::jsonb,
|
||||||
|
UNIQUE(space_id, path)
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS gist_branch_path ON komp.branch USING GIST (path);
|
||||||
|
|
||||||
|
-- Centroids per dimension (store only the dim matching the space)
|
||||||
|
CREATE TABLE IF NOT EXISTS komp.branch_centroid_768 (
|
||||||
|
branch_id BIGINT PRIMARY KEY REFERENCES komp.branch(id) ON DELETE CASCADE,
|
||||||
|
embedding VECTOR(768) NOT NULL,
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT now()
|
||||||
|
);
|
||||||
|
CREATE TABLE IF NOT EXISTS komp.branch_centroid_1024 (
|
||||||
|
branch_id BIGINT PRIMARY KEY REFERENCES komp.branch(id) ON DELETE CASCADE,
|
||||||
|
embedding VECTOR(1024) NOT NULL,
|
||||||
|
updated_at TIMESTAMPTZ DEFAULT now()
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS ledger_head (
|
-- Soft membership of chunks to branches
|
||||||
id INTEGER PRIMARY KEY CHECK (id=1),
|
CREATE TABLE IF NOT EXISTS komp.chunk_branch (
|
||||||
head_sha TEXT
|
chunk_id BIGINT REFERENCES komp.chunk(id) ON DELETE CASCADE,
|
||||||
|
branch_id BIGINT REFERENCES komp.branch(id) ON DELETE CASCADE,
|
||||||
|
weight REAL NOT NULL CHECK (weight >= 0 AND weight <= 1),
|
||||||
|
PRIMARY KEY(chunk_id, branch_id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- Relations between chunks (similarity / cites / derives / contradicts / …)
|
||||||
|
CREATE TABLE IF NOT EXISTS komp.chunk_edge (
|
||||||
|
src_chunk_id BIGINT REFERENCES komp.chunk(id) ON DELETE CASCADE,
|
||||||
|
dst_chunk_id BIGINT REFERENCES komp.chunk(id) ON DELETE CASCADE,
|
||||||
|
relation TEXT NOT NULL,
|
||||||
|
weight REAL,
|
||||||
|
meta JSONB DEFAULT '{}'::jsonb,
|
||||||
|
PRIMARY KEY(src_chunk_id, dst_chunk_id, relation)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE OR REPLACE VIEW komp.latest_sources AS
|
||||||
|
SELECT s.*, max(c.created_at) AS last_chunk_at
|
||||||
|
FROM komp.source s LEFT JOIN komp.chunk c ON c.source_id = s.id
|
||||||
|
GROUP BY s.id;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue