Implement bounded OTLP observability across API and asynchronous image pipeline

This commit is contained in:
2026-11-15 22:30:03 +03:00
parent 470d9d6f1e
commit 2701db5106
60 changed files with 6118 additions and 49 deletions
+35
View File
@@ -0,0 +1,35 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
password_hash text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
created_at timestamptz NOT NULL
);
CREATE TABLE members (
chat_id uuid NOT NULL REFERENCES chats(id),
user_id uuid NOT NULL REFERENCES users(id),
position integer NOT NULL CHECK (position >= 0),
PRIMARY KEY(chat_id,user_id),
UNIQUE(chat_id,position)
);
CREATE INDEX members_user_idx ON members(user_id,chat_id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL,
sender_id uuid NOT NULL,
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
created_at timestamptz NOT NULL,
FOREIGN KEY(chat_id,sender_id) REFERENCES members(chat_id,user_id)
);
CREATE INDEX messages_history_idx ON messages(chat_id,created_at,id);
CREATE TABLE sessions (
token_digest text PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id),
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
+10
View File
@@ -0,0 +1,10 @@
CREATE TABLE message_requests (
actor_id uuid NOT NULL REFERENCES users(id),
chat_id uuid NOT NULL REFERENCES chats(id),
key text NOT NULL,
body_digest text NOT NULL,
response jsonb NOT NULL,
expires_at timestamptz NOT NULL,
PRIMARY KEY(actor_id,chat_id,key)
);
CREATE INDEX message_requests_expiry_idx ON message_requests(expires_at);
+13
View File
@@ -0,0 +1,13 @@
-- PostgreSQL text rejects U+0000, while the API accepts Unicode code points.
-- Preserve UTF-8 losslessly. API validation enforces code point limits; the DB
-- additionally bounds byte lengths, without stripping or replacing characters.
ALTER TABLE users DROP CONSTRAINT users_display_name_check;
ALTER TABLE users ALTER COLUMN display_name TYPE bytea USING convert_to(display_name,'UTF8');
ALTER TABLE users ADD CHECK (octet_length(display_name) BETWEEN 1 AND 400);
ALTER TABLE chats DROP CONSTRAINT chats_title_check;
ALTER TABLE chats ALTER COLUMN title TYPE bytea USING convert_to(title,'UTF8');
ALTER TABLE chats ADD CHECK (octet_length(title) BETWEEN 1 AND 400);
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ALTER COLUMN text TYPE bytea USING convert_to(text,'UTF8');
ALTER TABLE messages ADD CHECK (octet_length(text) BETWEEN 1 AND 16000);
ALTER TABLE message_requests ALTER COLUMN response TYPE bytea USING convert_to(response::text,'UTF8');
+35
View File
@@ -0,0 +1,35 @@
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL REFERENCES users(id),
filename text NOT NULL,
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 text NOT NULL,
status text NOT NULL CONSTRAINT attachments_status_check CHECK (status IN ('queued','processing','ready','failed')),
error_code text,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
object_key text NOT NULL UNIQUE,
UNIQUE(id,chat_id)
);
CREATE TABLE message_attachments (
message_id uuid NOT NULL REFERENCES messages(id),
attachment_id uuid NOT NULL REFERENCES attachments(id),
position integer NOT NULL CHECK (position BETWEEN 0 AND 9),
PRIMARY KEY(message_id,attachment_id),
UNIQUE(message_id,position)
);
CREATE INDEX message_attachments_attachment_idx ON message_attachments(attachment_id);
CREATE TABLE upload_intents (
id uuid PRIMARY KEY,
object_key text NOT NULL UNIQUE,
created_at timestamptz NOT NULL
);
CREATE INDEX upload_intents_created_idx ON upload_intents(created_at,id);
CREATE TABLE attachment_reconcile_audit (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
event text NOT NULL,
object_key text NOT NULL,
happened_at timestamptz NOT NULL DEFAULT now()
);
@@ -0,0 +1,3 @@
-- Empty text is valid only when the API supplies at least one attachment.
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (octet_length(text) <= 16000);
+49
View File
@@ -0,0 +1,49 @@
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_check;
ALTER TABLE attachments ADD CONSTRAINT attachments_status_check
CHECK (status IN ('queued','processing','ready','failed'));
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1 CHECK (pipeline_version > 0);
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]';
ALTER TABLE attachments ADD COLUMN stage_key text;
CREATE TABLE attachment_jobs (
id uuid PRIMARY KEY,
attachment_id uuid NOT NULL REFERENCES attachments(id),
pipeline_version integer NOT NULL CHECK (pipeline_version > 0),
source_key text,
state text NOT NULL CHECK (state IN ('queued','processing','ready','failed','superseded')),
attempt integer NOT NULL DEFAULT 0 CHECK (attempt >= 0),
attempt_limit integer NOT NULL CHECK (attempt_limit > 0),
lease_id uuid,
lease_until timestamptz,
available_at timestamptz NOT NULL DEFAULT now(),
error_code text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE(attachment_id,pipeline_version)
);
CREATE INDEX jobs_recovery_idx ON attachment_jobs(state,lease_until);
CREATE TABLE job_outbox (
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id),
published_at timestamptz
);
CREATE TABLE job_audit (
id bigserial PRIMARY KEY,
job_id uuid NOT NULL REFERENCES attachment_jobs(id),
event text NOT NULL,
attempt integer NOT NULL,
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE failed_jobs (
job_id uuid PRIMARY KEY REFERENCES attachment_jobs(id),
error_code text NOT NULL,
failed_at timestamptz NOT NULL DEFAULT now(),
replay_count integer NOT NULL DEFAULT 0
);
CREATE TABLE test_faults (
id bigserial PRIMARY KEY,
point text NOT NULL,
attachment_id uuid,
action text NOT NULL CHECK (action IN ('crash','pause','error')),
reached_at timestamptz,
released boolean NOT NULL DEFAULT false
);
+2
View File
@@ -0,0 +1,2 @@
ALTER TABLE attachment_jobs ADD COLUMN traceparent text;
ALTER TABLE attachment_jobs ADD COLUMN tracestate text;