CREATE TABLE community_serving_write_leases ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), community_id UUID NOT NULL REFERENCES communities(id), operation TEXT NOT NULL, owner TEXT NOT NULL, generation BIGINT NOT NULL DEFAULT 1 CHECK (generation > 0), -- Community fence generation observed when this lease was acquired. fence_generation BIGINT NOT NULL CHECK (fence_generation >= 0), lease_until TIMESTAMPTZ NOT NULL, heartbeat_at TIMESTAMPTZ NOT NULL DEFAULT now(), created_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX community_serving_write_leases_active ON community_serving_write_leases (community_id, lease_until);