From 070a799ca8cd4ec785840fe2010ef75d68c79f38 Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Mon, 9 Aug 2021 14:00:45 -0400 Subject: [PATCH] sql: rework SHOW JOBS WHEN COMPLETE delegate The previous pattern was fragile because the filter could be re-arranged. Release note: None --- pkg/sql/delegate/show_jobs.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/sql/delegate/show_jobs.go b/pkg/sql/delegate/show_jobs.go index 1b6c7932f576..80f67bccd986 100644 --- a/pkg/sql/delegate/show_jobs.go +++ b/pkg/sql/delegate/show_jobs.go @@ -62,12 +62,21 @@ SHOW JOBS SELECT id FROM system.jobs WHERE created_by_type='%s' and created_by_i sqlStmt := fmt.Sprintf("%s %s %s", selectClause, whereClause, orderbyClause) if n.Block { sqlStmt = fmt.Sprintf( - `SELECT * FROM [%s] - WHERE - IF(finished IS NULL, - IF(pg_sleep(1), crdb_internal.force_retry('24h'), 0), - 0 - ) = 0`, sqlStmt) + ` + WITH jobs AS (SELECT * FROM [%s]), + sleep_and_restart_if_unfinished AS ( + SELECT IF(pg_sleep(1), crdb_internal.force_retry('24h'), 1) + = 0 AS timed_out + FROM (SELECT job_id FROM jobs WHERE finished IS NULL LIMIT 1) + ), + fail_if_slept_too_long AS ( + SELECT crdb_internal.force_error('55000', 'timed out waiting for jobs') + FROM sleep_and_restart_if_unfinished + WHERE timed_out + ) +SELECT * + FROM jobs + WHERE NOT EXISTS(SELECT * FROM fail_if_slept_too_long)`, sqlStmt) } return parse(sqlStmt) }