CREATE TABLE IF NOT EXISTS federated_worker_runs_v018 (
 id VARCHAR(36) PRIMARY KEY, job_id VARCHAR(36) NOT NULL REFERENCES federated_query_jobs_v017(id) ON DELETE CASCADE, worker_name VARCHAR(180) NOT NULL, status VARCHAR(30) NOT NULL DEFAULT 'queued', phase VARCHAR(60) NOT NULL DEFAULT 'queued', checkpoint JSON NOT NULL, memory_limit_mb INTEGER NOT NULL DEFAULT 256, peak_memory_mb INTEGER NOT NULL DEFAULT 0, spilled_bytes INTEGER NOT NULL DEFAULT 0, heartbeat_at TIMESTAMPTZ, error TEXT NOT NULL DEFAULT '', started_at TIMESTAMPTZ, finished_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS ix_worker_runs_job_status_v018 ON federated_worker_runs_v018(job_id,status);
CREATE TABLE IF NOT EXISTS query_spill_segments_v018 (
 id VARCHAR(36) PRIMARY KEY, worker_run_id VARCHAR(36) NOT NULL REFERENCES federated_worker_runs_v018(id) ON DELETE CASCADE, side VARCHAR(20) NOT NULL, partition_no INTEGER NOT NULL, object_key VARCHAR(1000) NOT NULL, checksum VARCHAR(128) NOT NULL, row_count INTEGER NOT NULL DEFAULT 0, size_bytes INTEGER NOT NULL DEFAULT 0, encrypted BOOLEAN NOT NULL DEFAULT FALSE, expires_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS ix_spill_run_side_v018 ON query_spill_segments_v018(worker_run_id,side);
CREATE TABLE IF NOT EXISTS query_exports_v018 (
 id VARCHAR(36) PRIMARY KEY, job_id VARCHAR(36) NOT NULL REFERENCES federated_query_jobs_v017(id) ON DELETE CASCADE, user_id VARCHAR(36) NOT NULL REFERENCES users(id), format VARCHAR(20) NOT NULL DEFAULT 'csv', status VARCHAR(30) NOT NULL DEFAULT 'queued', object_key VARCHAR(1000) NOT NULL DEFAULT '', checksum VARCHAR(128) NOT NULL DEFAULT '', row_count INTEGER NOT NULL DEFAULT 0, size_bytes INTEGER NOT NULL DEFAULT 0, error TEXT NOT NULL DEFAULT '', expires_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS ix_exports_user_status_v018 ON query_exports_v018(user_id,status);
