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

0001_installation.sql

38 lines1.7 KB

1-- Installation identity, recovery fencing, and shared live-reader admission.
2CREATE TABLE __SCHEMA__.installation (
3 singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
4 id uuid NOT NULL UNIQUE,
5 storage_version integer NOT NULL CHECK (storage_version = 3),
6 storage_identity text,
7 serving_epoch uuid NOT NULL DEFAULT gen_random_uuid(),
8 serving_state text NOT NULL DEFAULT 'active' CHECK (serving_state IN ('active', 'recovering')),
9 environment_id text CHECK (octet_length(environment_id) BETWEEN 1 AND 2048),
10 recovery_token uuid,
11 recovery_source_epoch uuid,
12 completed_recovery_token uuid,
13 recovery_target_environment text,
14 recovery_target_storage text,
15 recovery_target_public_base_url text,
16 recovery_cursor uuid,
17 recovery_verified bigint NOT NULL DEFAULT 0,
18 recovery_complete boolean NOT NULL DEFAULT false,
19 recovery_started_at timestamptz,
20 CONSTRAINT recovery_progress CHECK (recovery_verified >= 0),
21 CONSTRAINT recovery_operation CHECK (
22 (serving_state = 'active' AND recovery_token IS NULL)
23 OR (serving_state = 'recovering' AND recovery_token IS NOT NULL
24 AND recovery_source_epoch IS NOT NULL AND recovery_target_environment IS NOT NULL
25 AND recovery_started_at IS NOT NULL))
26);
27​
28CREATE TABLE __SCHEMA__.live_capacity (
29 singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
30 max_readers integer NOT NULL CHECK (max_readers BETWEEN 1 AND 100000)
31);
32INSERT INTO __SCHEMA__.live_capacity (max_readers) VALUES (80);
33CREATE TABLE __SCHEMA__.live_leases (
34 id uuid PRIMARY KEY,
35 serving_epoch uuid NOT NULL,
36 expires_at timestamptz NOT NULL
37);
38CREATE INDEX live_leases_expiry ON __SCHEMA__.live_leases (expires_at);