Open History to see the last commit
1-- The protocol has a canonical zero token; nonzero positions carry history.
2CREATE FUNCTION __SCHEMA__.valid_offset(value text)
3RETURNS boolean LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
4 SELECT value='0000000000000000_0000000000000000'
5 OR (value ~ '^[0-9]{16}_[0-9a-f]{32}$' AND left(value,16)<>'0000000000000000')
6$$;
7
8-- Stream generations, fork references, hot/frozen appends, and producer sessions.
9CREATE TABLE __SCHEMA__.paths (
10 tenant_id text COLLATE "C" NOT NULL CHECK (octet_length(tenant_id) BETWEEN 1 AND 512),
11 path text COLLATE "C" NOT NULL CHECK (octet_length(path) BETWEEN 1 AND 2048),
12 active_stream_id uuid,
13 PRIMARY KEY (tenant_id, path)
14);
15
16CREATE TABLE __SCHEMA__.streams (
17 id uuid PRIMARY KEY,
18 tenant_id text COLLATE "C" NOT NULL,
19 path text COLLATE "C" NOT NULL,
20 content_type text NOT NULL,
21 tail_offset text COLLATE "C" NOT NULL DEFAULT '0000000000000000_0000000000000000'
22 CHECK (__SCHEMA__.valid_offset(tail_offset)),
23 write_epoch uuid NOT NULL,
24 write_history uuid NOT NULL,
25 hot_bytes bigint NOT NULL DEFAULT 0 CHECK (hot_bytes >= 0),
26 hot_frames integer NOT NULL DEFAULT 0 CHECK (hot_frames >= 0),
27 hot_start_offset text COLLATE "C" NOT NULL DEFAULT '0000000000000000_0000000000000000'
28 CHECK (__SCHEMA__.valid_offset(hot_start_offset)),
29 change_version bigint NOT NULL DEFAULT 0 CHECK (change_version >= 0),
30 last_seq text COLLATE "C",
31 closed boolean NOT NULL DEFAULT false,
32 closed_by jsonb,
33 deleted_at timestamptz,
34 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
35 accessed_at timestamptz NOT NULL DEFAULT clock_timestamp(),
36 ttl_seconds integer CHECK (ttl_seconds >= 0),
37 expires_at timestamptz,
38 source_id uuid,
39 fork_offset text COLLATE "C",
40 fork_sub_offset bigint NOT NULL DEFAULT 0 CHECK (fork_sub_offset BETWEEN 0 AND 9007199254740991),
41 fork_depth integer NOT NULL DEFAULT 0 CHECK (fork_depth BETWEEN 0 AND 128),
42 edge_active boolean NOT NULL DEFAULT false,
43 ref_count integer NOT NULL DEFAULT 0 CHECK (ref_count >= 0),
44 cleanup_pending boolean NOT NULL DEFAULT false,
45 CHECK (ttl_seconds IS NULL OR expires_at IS NULL),
46 UNIQUE (tenant_id, path, id),
47 FOREIGN KEY (tenant_id, path) REFERENCES __SCHEMA__.paths (tenant_id, path),
48 CONSTRAINT stream_tenant_id_unique UNIQUE (tenant_id, id),
49 CONSTRAINT fork_source_fk FOREIGN KEY (tenant_id, source_id) REFERENCES __SCHEMA__.streams (tenant_id, id),
50 CONSTRAINT fork_configuration CHECK (
51 (source_id IS NULL AND fork_offset IS NULL AND fork_sub_offset = 0 AND fork_depth = 0 AND NOT edge_active)
52 OR (source_id IS NOT NULL AND source_id <> id AND __SCHEMA__.valid_offset(fork_offset) AND fork_depth > 0)
53 )
54);
55
56ALTER TABLE __SCHEMA__.paths ADD CONSTRAINT active_stream_fk
57 FOREIGN KEY (tenant_id, path, active_stream_id) REFERENCES __SCHEMA__.streams (tenant_id, path, id);
58
59CREATE INDEX forks_by_source ON __SCHEMA__.streams (source_id) WHERE edge_active;
60CREATE INDEX retired_without_forks ON __SCHEMA__.streams (deleted_at, id) WHERE cleanup_pending AND ref_count = 0;
61
62CREATE INDEX streams_reclaimable ON __SCHEMA__.streams (deleted_at, id)
63 WHERE deleted_at IS NOT NULL AND ref_count = 0 AND NOT edge_active AND NOT cleanup_pending;
64CREATE INDEX forks_all_sources ON __SCHEMA__.streams (source_id) WHERE source_id IS NOT NULL;
65
66-- Sealed frames live in object storage; only hot/frozen payloads remain here.
67CREATE TABLE __SCHEMA__.appends (
68 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
69 start_offset text COLLATE "C" NOT NULL,
70 end_offset text COLLATE "C" NOT NULL,
71 payload bytea NOT NULL CHECK (octet_length(payload) > 0),
72 payload_bytes integer NOT NULL,
73 message_ends integer[],
74 PRIMARY KEY (stream_id, end_offset),
75 CHECK (start_offset < end_offset),
76 CHECK (__SCHEMA__.valid_offset(start_offset) AND __SCHEMA__.valid_offset(end_offset)),
77 CHECK (message_ends IS NULL OR (cardinality(message_ends) > 0 AND message_ends[cardinality(message_ends)] = octet_length(payload))),
78 CONSTRAINT append_payload_bytes CHECK (payload_bytes > 0 AND (payload IS NULL OR octet_length(payload) = payload_bytes))
79);
80
81CREATE TABLE __SCHEMA__.producers (
82 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
83 producer_id text COLLATE "C" NOT NULL,
84 epoch bigint NOT NULL CHECK (epoch BETWEEN 0 AND 9007199254740991),
85 last_seq bigint NOT NULL CHECK (last_seq BETWEEN 0 AND 9007199254740991),
86 PRIMARY KEY (stream_id, producer_id)
87);
88
89CREATE TABLE __SCHEMA__.stream_expirations (
90 stream_id uuid PRIMARY KEY REFERENCES __SCHEMA__.streams (id),
91 due_at timestamptz NOT NULL
92);
93CREATE INDEX stream_expirations_due ON __SCHEMA__.stream_expirations (due_at, stream_id);