-- +goose Up -- Stored embedding vectors of candidate cards and need snapshots. A row is -- one text of a subject (the whole view or one item) encoded by one model, -- revision, and prompt recipe; content_sha256 is the digest of the rendered -- text, so a stale row is never mistaken for the current one. The vector is -- little-endian float32: exact, platform independent, 4 KiB for 1024 -- dimensions. Derived data follows its subject: an account or a need that -- is deleted takes its vectors with it. CREATE TABLE semantic_vectors ( subject_kind text NOT NULL, subject_id uuid NOT NULL, candidate_id uuid, need_id uuid, model_id text NOT NULL, model_revision text NOT NULL, prompt_version text NOT NULL, item_key text NOT NULL, content_sha256 bytea NOT NULL, label_sha256 bytea, dimension integer NOT NULL, vector bytea NOT NULL, computed_at timestamptz NOT NULL, CONSTRAINT semantic_vectors_pkey PRIMARY KEY ( subject_kind, subject_id, model_id, model_revision, prompt_version, item_key, content_sha256 ), CONSTRAINT semantic_vectors_candidate_fkey FOREIGN KEY (candidate_id) REFERENCES users (id) ON DELETE CASCADE, CONSTRAINT semantic_vectors_need_fkey FOREIGN KEY (need_id) REFERENCES organization_needs (id) ON DELETE CASCADE, CONSTRAINT semantic_vectors_subject CHECK ( (subject_kind = 'candidate' AND candidate_id = subject_id AND need_id IS NULL) OR (subject_kind = 'need' AND need_id = subject_id AND candidate_id IS NULL) ), CONSTRAINT semantic_vectors_model_format CHECK ( model_id ~ '^[A-Za-z0-9._/-]{1,200}$' AND model_revision ~ '^[A-Za-z0-9._/-]{1,200}$' AND prompt_version ~ '^[A-Za-z0-9._/-]{1,200}$' ), CONSTRAINT semantic_vectors_item_key_format CHECK ( item_key ~ '^[a-z_]{1,32}(:[0-9]{1,3})?$' ), CONSTRAINT semantic_vectors_digests CHECK ( octet_length(content_sha256) = 32 AND (label_sha256 IS NULL OR octet_length(label_sha256) = 32) ), CONSTRAINT semantic_vectors_dimension CHECK ( dimension BETWEEN 1 AND 4096 AND octet_length(vector) = 4 * dimension ) ); -- The search reads the whole-view vector of many candidates for one model. CREATE INDEX semantic_vectors_candidate_whole_idx ON semantic_vectors (model_id, model_revision, prompt_version, candidate_id) WHERE subject_kind = 'candidate' AND item_key = 'whole'; -- Account and need deletion find the rows of their subject through these. CREATE INDEX semantic_vectors_candidate_idx ON semantic_vectors (candidate_id) WHERE candidate_id IS NOT NULL; CREATE INDEX semantic_vectors_need_idx ON semantic_vectors (need_id) WHERE need_id IS NOT NULL; -- +goose Down -- The vectors are derived data: the backfill command computes them again. DROP TABLE semantic_vectors;