Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

0003_storage.sql

91 lines4.3 KB

1-- One manifest per immutable object; directories contain one entry per verified block.
2CREATE TABLE __SCHEMA__.chunks (
3 id uuid PRIMARY KEY,
4 stream_id uuid NOT NULL REFERENCES __SCHEMA__.streams (id),
5 start_offset text COLLATE "C" NOT NULL,
6 end_offset text COLLATE "C" NOT NULL,
7 state text NOT NULL CHECK (state IN ('frozen', 'sealed')),
8 frame_count integer NOT NULL CHECK (frame_count BETWEEN 1 AND 131072),
9 payload_bytes bigint NOT NULL CHECK (payload_bytes > 0),
10 block_directory bytea,
11 claim_token uuid,
12 claim_epoch bigint NOT NULL DEFAULT 0,
13 claim_until timestamptz,
14 attempts bigint NOT NULL DEFAULT 0,
15 object_key text,
16 sha256 text,
17 encoded_bytes integer,
18 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
19 sealed_at timestamptz,
20 UNIQUE (stream_id, id),
21 UNIQUE (stream_id, end_offset),
22 CHECK (start_offset < end_offset),
23 CHECK (__SCHEMA__.valid_offset(start_offset) AND __SCHEMA__.valid_offset(end_offset)),
24 CHECK ((state = 'frozen' AND object_key IS NULL AND sha256 IS NULL AND encoded_bytes IS NULL AND sealed_at IS NULL)
25 OR (state = 'sealed' AND object_key IS NOT NULL AND sha256 ~ '^[0-9a-f]{64}$' AND encoded_bytes > 0 AND sealed_at IS NOT NULL)),
26 retired_at timestamptz,
27 CONSTRAINT chunk_claim CHECK ((claim_token IS NULL) = (claim_until IS NULL)),
28 CONSTRAINT chunk_dense_ordinals CHECK
29 (substring(end_offset,1,16)::bigint-substring(start_offset,1,16)::bigint=frame_count),
30 CONSTRAINT sealed_directory_required CHECK
31 ((state='sealed' AND block_directory IS NOT NULL AND octet_length(block_directory)>0)
32 OR (state='frozen' AND block_directory IS NULL))
33);
34CREATE INDEX chunks_pending ON __SCHEMA__.chunks (created_at) WHERE state = 'frozen';
35CREATE INDEX chunks_retired ON __SCHEMA__.chunks (retired_at, id) WHERE retired_at IS NOT NULL;
36​
37-- Only the first append/rotation changes this queue, leaving stream counters
38-- out of every index predicate so ordinary counter updates can be HOT.
39CREATE TABLE __SCHEMA__.hot_chunks (
40 stream_id uuid PRIMARY KEY REFERENCES __SCHEMA__.streams(id),
41 due_at timestamptz NOT NULL
42);
43CREATE INDEX hot_chunks_due ON __SCHEMA__.hot_chunks(due_at,stream_id);
44​
45CREATE TABLE __SCHEMA__.replay_pins (
46 id uuid NOT NULL,
47 chunk_id uuid NOT NULL REFERENCES __SCHEMA__.chunks (id) ON DELETE CASCADE,
48 serving_epoch uuid NOT NULL,
49 expires_at timestamptz NOT NULL,
50 PRIMARY KEY (id, chunk_id)
51);
52CREATE INDEX replay_pins_chunk_expiry ON __SCHEMA__.replay_pins (chunk_id, expires_at);
53CREATE INDEX replay_pins_expiry ON __SCHEMA__.replay_pins (expires_at, id);
54​
55-- This ledger deliberately has no FK to a generation/chunk. It retains upload
56-- intent and deletion retry identity after metadata cleanup or orphan discovery.
57CREATE TABLE __SCHEMA__.chunk_objects (
58 object_key text PRIMARY KEY,
59 stream_id uuid NOT NULL,
60 chunk_id uuid NOT NULL,
61 sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
62 encoded_bytes integer NOT NULL CHECK (encoded_bytes BETWEEN 1 AND 134217728),
63 created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
64 retired_at timestamptz,
65 collected_at timestamptz,
66 orphan_candidate boolean NOT NULL DEFAULT false,
67 quarantine_reason text,
68 claim_token uuid,
69 claim_epoch bigint NOT NULL DEFAULT 0,
70 claim_until timestamptz,
71 next_attempt_at timestamptz NOT NULL DEFAULT clock_timestamp(),
72 CHECK (collected_at IS NULL OR retired_at IS NOT NULL),
73 CONSTRAINT object_claim CHECK ((claim_token IS NULL) = (claim_until IS NULL))
74);
75CREATE INDEX chunk_objects_chunk ON __SCHEMA__.chunk_objects (chunk_id);
76CREATE INDEX chunk_objects_pending ON __SCHEMA__.chunk_objects (retired_at, object_key)
77 WHERE retired_at IS NOT NULL AND collected_at IS NULL;
78CREATE INDEX chunk_objects_orphans ON __SCHEMA__.chunk_objects (next_attempt_at, created_at, object_key)
79 WHERE orphan_candidate AND retired_at IS NULL AND quarantine_reason IS NULL;
80​
81CREATE TABLE __SCHEMA__.reclamation_policy (
82 singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
83 enabled boolean NOT NULL DEFAULT false,
84 inventory_cursor text,
85 inventory_completed_at timestamptz,
86 work_phase smallint NOT NULL DEFAULT 0 CHECK (work_phase BETWEEN 0 AND 5),
87 metadata_phase smallint NOT NULL DEFAULT 0 CHECK (metadata_phase BETWEEN 0 AND 7),
88 configured_recovery_token uuid
89);
90INSERT INTO __SCHEMA__.reclamation_policy (singleton) VALUES (true);
91​