Content Summary: This page shows users how to identify background jobs that take too long to run.
Query Overview
Below is a SQL query, which will help determine background jobs that take too long to run.
Note: This query unions the job and archive table. Adjust the top interval appropriately for your needs.
Instructions
Run the following SQL query to determine background jobs that take too long to run.
WITH--FORINTERVALUSAGE===>NOW() -INTERVAL'4 hours'ASstart_dt--FOR SPECIFIC DATETIME USAGE ===> TO_TIMESTAMP('2021-11-23 12:05:00', 'YYYY-MM-DD hh24:mi:ss')::timestamp without time zone at time zone 'EDT' AS start_dt
varsAS (SELECTNOW() AS end_dt,NOW() - INTERVAL '7 days' AS start_dt),all_jobs_rawAS (SELECTname,state,startedon,createdon,completedon,dataFROMpgboss.job UNION SELECT name, state, startedon, createdon, completedon, data FROM pgboss.archive -- COMMENT OUT THIS TABLE IF LESS THAN 12 HOURS AND RUNNING SLOW
),all_jobsAS (SELECTCASEWHENname='automaticSubscription'ANDdata->>'profileId'ISNULLTHEN'autoSub - ByDataSource'WHENname='automaticSubscription'ANDdata->>'profileId'ISNOTNULLTHEN'autoSub - ByUser'WHENnameILIKE'nativeSqlProfileRefresh_Snowflake%'THEN'nativeSqlProfileRefresh_Snowflake%'WHENnameILIKE'nativeSqlProfileRefresh_Redshift%'THEN'nativeSqlProfileRefresh_Redshift%'ELSEnameENDASname,state,startedon,completedonFROMall_jobs_rawINNERJOINvarsONTRUEWHEREall_jobs_raw.createdon>vars.start_dtandall_jobs_raw.createdon<vars.end_dt),batchesAS (SELECTDISTINCTname,COUNT(*) ASjob_count,AVG(completedon-startedon) ASaverage_timeFROMall_jobsGROUPBYname)SELECT*,job_count*average_timeAStotal_time,end_dt-start_dtASall_timeFROMbatchesJOINvarsONTRUEORDERBYtotal_timeDESC;