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
|
|
|
|
|
|
| 1 |
+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
| 2 |
+
CREATE EXTENSION IF NOT EXISTS "pg_trgm";
|
| 3 |
+
|
| 4 |
+
CREATE TABLE IF NOT EXISTS sessions (
|
| 5 |
+
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
| 6 |
+
tenant_id VARCHAR(64) DEFAULT 'default',
|
| 7 |
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 8 |
+
last_active_at TIMESTAMPTZ DEFAULT NOW(),
|
| 9 |
+
expires_at TIMESTAMPTZ DEFAULT NOW() + INTERVAL '7 days',
|
| 10 |
+
budget_usd NUMERIC(10, 6) DEFAULT 10.0,
|
| 11 |
+
spent_usd NUMERIC(10, 6) DEFAULT 0.0,
|
| 12 |
+
status VARCHAR(32) DEFAULT 'active',
|
| 13 |
+
metadata JSONB DEFAULT '{}',
|
| 14 |
+
context_json JSONB DEFAULT '[]',
|
| 15 |
+
model_name VARCHAR(128),
|
| 16 |
+
reasoning_effort VARCHAR(32),
|
| 17 |
+
max_iterations INTEGER DEFAULT 300,
|
| 18 |
+
yolo_mode BOOLEAN DEFAULT FALSE,
|
| 19 |
+
CONSTRAINT positive_budget CHECK (budget_usd >= 0),
|
| 20 |
+
CONSTRAINT positive_spent CHECK (spent_usd >= 0)
|
| 21 |
+
);
|
| 22 |
+
|
| 23 |
+
CREATE TABLE IF NOT EXISTS requests (
|
| 24 |
+
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
| 25 |
+
session_id UUID REFERENCES sessions(id) ON DELETE CASCADE,
|
| 26 |
+
tenant_id VARCHAR(64) DEFAULT 'default',
|
| 27 |
+
trace_id VARCHAR(128),
|
| 28 |
+
model VARCHAR(128) NOT NULL,
|
| 29 |
+
provider VARCHAR(64) NOT NULL,
|
| 30 |
+
operation_type VARCHAR(32) DEFAULT 'chat',
|
| 31 |
+
input_tokens INTEGER DEFAULT 0,
|
| 32 |
+
output_tokens INTEGER DEFAULT 0,
|
| 33 |
+
total_tokens INTEGER GENERATED ALWAYS AS (input_tokens + output_tokens) STORED,
|
| 34 |
+
cost_usd NUMERIC(10, 6) DEFAULT 0.0,
|
| 35 |
+
cached BOOLEAN DEFAULT FALSE,
|
| 36 |
+
latency_ms INTEGER,
|
| 37 |
+
queue_time_ms INTEGER DEFAULT 0,
|
| 38 |
+
request_payload JSONB,
|
| 39 |
+
response_payload JSONB,
|
| 40 |
+
tool_calls JSONB DEFAULT '[]',
|
| 41 |
+
finish_reason VARCHAR(32),
|
| 42 |
+
rate_limited BOOLEAN DEFAULT FALSE,
|
| 43 |
+
circuit_open BOOLEAN DEFAULT FALSE,
|
| 44 |
+
retry_count INTEGER DEFAULT 0,
|
| 45 |
+
created_at TIMESTAMPTZ DEFAULT NOW(),
|
| 46 |
+
CONSTRAINT non_negative_tokens CHECK (input_tokens >= 0 AND output_tokens >= 0)
|
| 47 |
+
);
|
| 48 |
+
|
| 49 |
+
CREATE TABLE IF NOT EXISTS circuit_events (
|
| 50 |
+
id SERIAL PRIMARY KEY,
|
| 51 |
+
provider VARCHAR(64) NOT NULL,
|
| 52 |
+
event_type VARCHAR(32) NOT NULL,
|
| 53 |
+
failure_count INTEGER DEFAULT 0,
|
| 54 |
+
details JSONB DEFAULT '{}',
|
| 55 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 56 |
+
);
|
| 57 |
+
|
| 58 |
+
CREATE TABLE IF NOT EXISTS budget_events (
|
| 59 |
+
id SERIAL PRIMARY KEY,
|
| 60 |
+
session_id UUID REFERENCES sessions(id) ON DELETE CASCADE,
|
| 61 |
+
event_type VARCHAR(32) NOT NULL,
|
| 62 |
+
threshold_percent INTEGER,
|
| 63 |
+
spent_before NUMERIC(10, 6),
|
| 64 |
+
spent_after NUMERIC(10, 6),
|
| 65 |
+
budget NUMERIC(10, 6),
|
| 66 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 67 |
+
);
|
| 68 |
+
|
| 69 |
+
CREATE TABLE IF NOT EXISTS tool_executions (
|
| 70 |
+
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
|
| 71 |
+
request_id UUID REFERENCES requests(id) ON DELETE CASCADE,
|
| 72 |
+
tool_name VARCHAR(128) NOT NULL,
|
| 73 |
+
tool_args JSONB,
|
| 74 |
+
result_json JSONB,
|
| 75 |
+
success BOOLEAN DEFAULT TRUE,
|
| 76 |
+
error_message TEXT,
|
| 77 |
+
latency_ms INTEGER,
|
| 78 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 79 |
+
);
|
| 80 |
+
|
| 81 |
+
CREATE TABLE IF NOT EXISTS cache_events (
|
| 82 |
+
id SERIAL PRIMARY KEY,
|
| 83 |
+
cache_key VARCHAR(256) NOT NULL,
|
| 84 |
+
cache_type VARCHAR(32) NOT NULL,
|
| 85 |
+
hit BOOLEAN NOT NULL,
|
| 86 |
+
ttl_seconds INTEGER,
|
| 87 |
+
size_bytes INTEGER,
|
| 88 |
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
| 89 |
+
);
|
| 90 |
+
|
| 91 |
+
CREATE INDEX IF NOT EXISTS idx_sessions_tenant ON sessions(tenant_id);
|
| 92 |
+
CREATE INDEX IF NOT EXISTS idx_sessions_status ON sessions(status);
|
| 93 |
+
CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions(expires_at);
|
| 94 |
+
CREATE INDEX IF NOT EXISTS idx_sessions_created ON sessions(created_at DESC);
|
| 95 |
+
CREATE INDEX IF NOT EXISTS idx_sessions_active ON sessions(tenant_id, status) WHERE status = 'active';
|
| 96 |
+
CREATE INDEX IF NOT EXISTS idx_sessions_metadata ON sessions USING GIN (metadata);
|
| 97 |
+
|
| 98 |
+
CREATE INDEX IF NOT EXISTS idx_requests_session ON requests(session_id);
|
| 99 |
+
CREATE INDEX IF NOT EXISTS idx_requests_tenant ON requests(tenant_id);
|
| 100 |
+
CREATE INDEX IF NOT EXISTS idx_requests_trace ON requests(trace_id);
|
| 101 |
+
CREATE INDEX IF NOT EXISTS idx_requests_provider ON requests(provider);
|
| 102 |
+
CREATE INDEX IF NOT EXISTS idx_requests_created ON requests(created_at DESC);
|
| 103 |
+
CREATE INDEX IF NOT EXISTS idx_requests_model ON requests(model);
|
| 104 |
+
CREATE INDEX IF NOT EXISTS idx_requests_payload ON requests USING GIN (request_payload);
|
| 105 |
+
|
| 106 |
+
CREATE INDEX IF NOT EXISTS idx_circuit_provider ON circuit_events(provider);
|
| 107 |
+
CREATE INDEX IF NOT EXISTS idx_circuit_created ON circuit_events(created_at DESC);
|
| 108 |
+
CREATE INDEX IF NOT EXISTS idx_budget_session ON budget_events(session_id);
|
| 109 |
+
CREATE INDEX IF NOT EXISTS idx_tool_request ON tool_executions(request_id);
|
| 110 |
+
CREATE INDEX IF NOT EXISTS idx_tool_name ON tool_executions(tool_name);
|
| 111 |
+
CREATE INDEX IF NOT EXISTS idx_cache_key ON cache_events(cache_key);
|
| 112 |
+
CREATE INDEX IF NOT EXISTS idx_cache_created ON cache_events(created_at DESC);
|
| 113 |
+
|
| 114 |
+
CREATE OR REPLACE FUNCTION update_last_active()
|
| 115 |
+
RETURNS TRIGGER AS $$
|
| 116 |
+
BEGIN
|
| 117 |
+
NEW.last_active_at = NOW();
|
| 118 |
+
RETURN NEW;
|
| 119 |
+
END;
|
| 120 |
+
$$ LANGUAGE plpgsql;
|
| 121 |
+
|
| 122 |
+
DROP TRIGGER IF EXISTS trigger_update_last_active ON sessions;
|
| 123 |
+
CREATE TRIGGER trigger_update_last_active
|
| 124 |
+
BEFORE UPDATE ON sessions
|
| 125 |
+
FOR EACH ROW
|
| 126 |
+
EXECUTE FUNCTION update_last_active();
|
| 127 |
+
|
| 128 |
+
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_daily_costs AS
|
| 129 |
+
SELECT
|
| 130 |
+
tenant_id,
|
| 131 |
+
DATE(created_at) as date,
|
| 132 |
+
provider,
|
| 133 |
+
model,
|
| 134 |
+
COUNT(*) as request_count,
|
| 135 |
+
SUM(input_tokens) as total_input_tokens,
|
| 136 |
+
SUM(output_tokens) as total_output_tokens,
|
| 137 |
+
SUM(cost_usd) as total_cost_usd,
|
| 138 |
+
AVG(latency_ms)::NUMERIC(10,2) as avg_latency_ms,
|
| 139 |
+
SUM(CASE WHEN cached THEN 1 ELSE 0 END) as cache_hits,
|
| 140 |
+
SUM(CASE WHEN NOT cached THEN 1 ELSE 0 END) as cache_misses
|
| 141 |
+
FROM requests
|
| 142 |
+
GROUP BY tenant_id, DATE(created_at), provider, model;
|
| 143 |
+
|
| 144 |
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_mv_daily_costs ON mv_daily_costs(tenant_id, date, provider, model);
|
| 145 |
+
|
| 146 |
+
CREATE OR REPLACE FUNCTION refresh_daily_costs()
|
| 147 |
+
RETURNS void AS $$
|
| 148 |
+
BEGIN
|
| 149 |
+
REFRESH MATERIALIZED VIEW CONCURRENTLY mv_daily_costs;
|
| 150 |
+
END;
|
| 151 |
+
$$ LANGUAGE plpgsql;
|
| 152 |
+
|
| 153 |
+
ALTER TABLE sessions ENABLE ROW LEVEL SECURITY;
|
| 154 |
+
ALTER TABLE requests ENABLE ROW LEVEL SECURITY;
|
| 155 |
+
|
| 156 |
+
CREATE POLICY tenant_isolation_sessions ON sessions
|
| 157 |
+
USING (tenant_id = current_setting('app.current_tenant', true));
|
| 158 |
+
CREATE POLICY tenant_isolation_requests ON requests
|
| 159 |
+
USING (tenant_id = current_setting('app.current_tenant', true));
|
| 160 |
+
|
| 161 |
+
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ml_intern;
|
| 162 |
+
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ml_intern;
|