6.6. List In-Flight Query Jobs at a Given Time

Below is an example of a query that lists in-flight query jobs during some point in time or during some interval.

Search in the query for TODO to find lines where common changes to the query can be made. Script variables can be modified as needed. selected_start_time should be set to the point in time or the start of the interval during which you want to include in-flight query jobs. selected_end_time should be set to the point in time or the end of the interval during which you want to include in-flight query jobs. Choose an appropriate source view: JOBS_BY_USER, JOBS_BY_PROJECT, JOBS_BY_ORGANIZATION, or an archive of one of these. Modify the filters as needed.

-- TODO: modify script variables as needed
declare selected_start_time timestamp default
--     '1970-01-01 00:00:00.000'
    current_timestamp
;
declare selected_end_time timestamp default
--     '1970-01-01 00:00:00.000'
    selected_start_time
;
declare selected_job_types array<string> default
--     ['LOAD', 'QUERY', 'COPY', 'EXTRACT']
    -- note that only QUERY jobs count toward interactive query concurrency limits
    -- https://cloud.google.com/bigquery/quotas#query_jobs
    ['QUERY']
;

select
    format_timestamp(
        '%Y-%m-%d %H:%M:%E*S'
        ,j.creation_time
    ) as creation_time
    ,selected_start_time
    ,selected_end_time
    ,format_timestamp(
        '%Y-%m-%d %H:%M:%E*S'
        ,j.end_time
    ) as end_time
    ,timestamp_diff(
        selected_start_time
        ,j.creation_time
        ,millisecond
    ) as creation_to_selected_start_time_ms
    ,timestamp_diff(
        coalesce(
            j.end_time
            ,current_timestamp
        )
        ,j.creation_time
        ,millisecond
    ) as creation_to_end_or_current_ms
    ,j.job_type
    ,j.statement_type
    ,j.cache_hit
    ,j.state
    ,j.error_result.reason as error_reason
    ,j.error_result.message as error_message
    ,j.total_slot_ms
    ,j.user_email
    ,j.destination_table.project_id as destination_project_id
    ,j.destination_table.dataset_id as destination_dataset_id
    ,j.destination_table.table_id as destination_table_id
    ,to_json_string(
        j.labels
        ,true -- pretty_print
    ) as labels
    ,substr(
        j.query
        ,1
        ,32767 -- maximum number of characters an Excel cell can contain
    ) as query
-- TODO: comment/uncomment lines as needed to choose jobs source
--     from `PROJECT_ID.region-us.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION` j
--     from `PROJECT_ID.region-us.INFORMATION_SCHEMA.JOBS_BY_PROJECT` j
--     from `PROJECT_ID.region-us.INFORMATION_SCHEMA.JOBS_BY_USER` j
--     from `PROJECT_ID.ARCHIVE_DATASET.JOBS` j
    -- default to this query's billing project
--     from `region-us.INFORMATION_SCHEMA.JOBS_BY_ORGANIZATION` j
--     from `region-us.INFORMATION_SCHEMA.JOBS_BY_PROJECT` j
    from `region-us.INFORMATION_SCHEMA.JOBS_BY_USER` j
where
    -- ensure partition elimination
    date(j.creation_time) between
        -- include prior day for jobs that cross days
        date_add(
            date(selected_start_time)
            ,interval -1 day
        )
        and date(selected_end_time)
    and j.job_type in unnest(selected_job_types)
    -- filters below ensure only queries in-flight at the selected time are included
    and j.creation_time < selected_end_time
    and (
        j.end_time > selected_start_time
        or j.end_time is null
    )
order by
    j.creation_time
;