ALTER TABLE gateway_worker_instances
  ADD COLUMN hard_capacity_limit integer NOT NULL DEFAULT 0,
  ADD COLUMN safe_capacity integer NOT NULL DEFAULT 0,
  ADD COLUMN heavy_capacity integer NOT NULL DEFAULT 0,
  ADD COLUMN active_tasks integer NOT NULL DEFAULT 0,
  ADD COLUMN preparing_tasks integer NOT NULL DEFAULT 0,
  ADD COLUMN waiting_upstream_tasks integer NOT NULL DEFAULT 0,
  ADD COLUMN finalizing_tasks integer NOT NULL DEFAULT 0,
  ADD COLUMN pressure_state text NOT NULL DEFAULT 'unknown',
  ADD COLUMN pressure_reason text NOT NULL DEFAULT '',
  ADD COLUMN load_sampled_at timestamptz,
  ADD CONSTRAINT gateway_worker_instances_adaptive_capacity_check
    CHECK (
      hard_capacity_limit >= 0
      AND safe_capacity >= 0
      AND heavy_capacity >= 0
      AND active_tasks >= 0
      AND preparing_tasks >= 0
      AND waiting_upstream_tasks >= 0
      AND finalizing_tasks >= 0
      AND active_tasks = preparing_tasks + waiting_upstream_tasks + finalizing_tasks
    ) NOT VALID,
  ADD CONSTRAINT gateway_worker_instances_pressure_state_check
    CHECK (pressure_state IN ('unknown', 'normal', 'busy', 'critical')) NOT VALID;

ALTER TABLE gateway_concurrency_leases
  ADD COLUMN limit_value numeric,
  ADD CONSTRAINT gateway_concurrency_leases_limit_value_check
    CHECK (limit_value IS NULL OR limit_value > 0) NOT VALID;

UPDATE gateway_worker_instances
SET hard_capacity_limit = capacity_limit,
    safe_capacity = capacity_limit,
    heavy_capacity = capacity_limit
WHERE hard_capacity_limit = 0;

ALTER TABLE gateway_worker_instances
  VALIDATE CONSTRAINT gateway_worker_instances_adaptive_capacity_check;

ALTER TABLE gateway_worker_instances
  VALIDATE CONSTRAINT gateway_worker_instances_pressure_state_check;

ALTER TABLE gateway_concurrency_leases
  VALIDATE CONSTRAINT gateway_concurrency_leases_limit_value_check;
