SELECT_SQL = f"\nselect\n thread_id,\n checkpoint,\n checkpoint_ns,\n metadata,\n (
\n select array_agg(array[bl.channel::bytea, bl.type::bytea, bl.blob])\n from jsonb_each_text(checkpoint -> 'channel_versions')\n inner join checkpoint_blobs bl\n on bl.thread_id = checkpoints.thread_id\n and bl.checkpoint_ns = checkpoints.checkpoint_ns\n and bl.channel = jsonb_each_text.key\n ) as channel_values,\n (\n select\n array_agg(array[cw.task_id::text::bytea, cw.channel::bytea, cw.type::bytea, cw.blob] order by cw.task_id,
cw.idx)\n from checkpoint_writes cw\n where cw.thread_id = checkpoints.thread_id\n and cw.checkpoint_ns = checkpoints.checkpoint_ns\n and cw.checkpoint_id = (checkpoint->>'id')\n ) as pending_writes,\n (\n select array_agg(array[cw.type::bytea, cw.blob] order by cw.task_path, cw.task_id, cw.idx)\n from checkpoint_writes cw\n where cw.thread_id = checkpoints.thread_id\n and cw.checkpoint_ns = checkpoints.checkpoint_ns\n and cw.channel = '{TASKS}'\n
) as pending_sends\nfrom checkpoints "