Open History to see the last commit
1CREATE TABLE __SCHEMA__.installation (
2 singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
3 id uuid NOT NULL UNIQUE,
4 storage_version integer NOT NULL CHECK (storage_version = 1)
5);
6
7CREATE TABLE __SCHEMA__.paths (
8 tenant_id text COLLATE "C" NOT NULL CHECK (octet_length(tenant_id) BETWEEN 1 AND 512),
9 path text COLLATE "C" NOT NULL CHECK (octet_length(path) BETWEEN 1 AND 2048),
10 active_stream_id uuid,
11 PRIMARY KEY (tenant_id, path)
12);
13
14CREATE TABLE __SCHEMA__.streams (
15 id uuid PRIMARY KEY,
16 tenant_id text COLLATE "C" NOT NULL,
17 path text COLLATE "C" NOT NULL,
18 content_type text NOT NULL,
19 tail_offset text COLLATE "C" NOT NULL DEFAULT '0000000000000000_0000000000000000'
20 CHECK (tail_offset ~ '^[0-9]{16}_[0-9]{16}$'),
21 change_version bigint NOT NULL DEFAULT 0 CHECK (change_version >= 0),
22 last_seq text COLLATE "C",
23 closed boolean NOT NULL DEFAULT false,
24 closed_by jsonb,
25 deleted_at timestamptz,
26 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
27 accessed_at timestamptz NOT NULL DEFAULT clock_timestamp(),
28 ttl_seconds integer CHECK (ttl_seconds >= 0),
29 expires_at timestamptz,
30 CHECK (ttl_seconds IS NULL OR expires_at IS NULL),
31 UNIQUE (tenant_id, path, id),
32 FOREIGN KEY (tenant_id, path) REFERENCES __SCHEMA__.paths (tenant_id, path)
33);
34
35ALTER TABLE __SCHEMA__.paths ADD CONSTRAINT active_stream_fk
36 FOREIGN KEY (tenant_id, path, active_stream_id) REFERENCES __SCHEMA__.streams (tenant_id, path, id);
37
38CREATE TABLE __SCHEMA__.appends (
39 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
40 start_offset text COLLATE "C" NOT NULL,
41 end_offset text COLLATE "C" NOT NULL,
42 payload bytea NOT NULL CHECK (octet_length(payload) > 0),
43 message_ends integer[],
44 PRIMARY KEY (stream_id, end_offset),
45 CHECK (start_offset < end_offset),
46 CHECK (start_offset ~ '^[0-9]{16}_[0-9]{16}$' AND end_offset ~ '^[0-9]{16}_[0-9]{16}$'),
47 CHECK (message_ends IS NULL OR (cardinality(message_ends) > 0 AND message_ends[cardinality(message_ends)] = octet_length(payload)))
48);
49
50CREATE TABLE __SCHEMA__.producers (
51 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
52 producer_id text COLLATE "C" NOT NULL,
53 epoch bigint NOT NULL CHECK (epoch BETWEEN 0 AND 9007199254740991),
54 last_seq bigint NOT NULL CHECK (last_seq BETWEEN 0 AND 9007199254740991),
55 PRIMARY KEY (stream_id, producer_id)
56);
57
58CREATE TABLE __SCHEMA__.stream_changes (
59 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
60 version bigint NOT NULL,
61 before_offset text COLLATE "C" NOT NULL,
62 after_offset text COLLATE "C" NOT NULL,
63 kind text NOT NULL CHECK (kind IN ('created', 'appended', 'closed', 'deleted', 'expired')),
64 processed boolean NOT NULL DEFAULT false,
65 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
66 PRIMARY KEY (stream_id, version)
67);
68CREATE INDEX stream_changes_pending ON __SCHEMA__.stream_changes (created_at)
69 WHERE NOT processed;