Skip to content

Instantly share code, notes, and snippets.

@CAFxX
Last active April 23, 2022 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CAFxX/31023c99556443d058577346c7ce691e to your computer and use it in GitHub Desktop.
Save CAFxX/31023c99556443d058577346c7ce691e to your computer and use it in GitHub Desktop.
Ensure at most a single BigQuery scheduled query is running at one time
DECLARE check_running_jobs DEFAULT (
SELECT IF(COUNT(*) > 1, ERROR('query already running'), NULL)
FROM `region-name`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE state <> 'DONE'
);
@CAFxX
Copy link
Author

CAFxX commented Apr 23, 2022

Note: you can't use this if you use a destination table. In this case you should remove the destination table from the scheduled query definition and rewrite your query as

DECLARE check_running_jobs DEFAULT ( 
  SELECT IF(COUNT(*) > 1, ERROR('query already running'), NULL) 
  FROM `region-name`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
  WHERE state <> 'DONE'
);
CREATE OR REPLACE TABLE `destination-table` AS (
  SELECT ...
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment