-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(jobsdb): latest job status query optimization #2693
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
sql/migrations/jobsdb/000010_add_index_job_status_table.down.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{range .Datasets}} | ||
DROP INDEX IF EXISTS "idx_{{$.Prefix}}_job_status_{{.}}_jid_id"; | ||
DROP VIEW IF EXISTS "v_last_{{$.Prefix}}_job_status_{{.}}"; | ||
{{end}} |
4 changes: 4 additions & 0 deletions
4
sql/migrations/jobsdb/000010_add_index_job_status_table.up.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{{range .Datasets}} | ||
CREATE INDEX IF NOT EXISTS "idx_{{$.Prefix}}_job_status_{{.}}_jid_id" ON "{{$.Prefix}}_job_status_{{.}}" (job_id asc,id desc); | ||
CREATE OR REPLACE VIEW "v_last_{{$.Prefix}}_job_status_{{.}}" AS SELECT DISTINCT ON (job_id) * FROM "{{$.Prefix}}_job_status_{{.}}" ORDER BY job_id ASC,id DESC; | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP FUNCTION IF EXISTS unionjobsdb(prefix text, num int); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- unionjobsdb function automatically joins datasets and returns jobs | ||
-- along with their latest jobs status (or null) | ||
-- | ||
-- Parameters | ||
-- prefix: table prefix, e.g. gw, rt, batch_rt | ||
-- num: number of datasets to include in the query, e.g. 4 | ||
CREATE OR REPLACE FUNCTION unionjobsdb(prefix text, num int) | ||
RETURNS table ( | ||
t_name text, | ||
job_id bigint, | ||
workspace_id text, | ||
uuid uuid, | ||
user_id text, | ||
parameters jsonb, | ||
custom_val character varying(64), | ||
event_payload jsonb, | ||
event_count integer, | ||
created_at timestamp with time zone, | ||
expire_at timestamp with time zone, | ||
status_id bigint, | ||
job_state character varying(64), | ||
attempt smallint, | ||
error_code character varying(32), | ||
error_response jsonb | ||
) | ||
AS $$ | ||
DECLARE | ||
qry text; | ||
BEGIN | ||
SELECT string_agg( | ||
format('SELECT %1$L, j.job_id, j.workspace_id, j.uuid, j.user_id, j.parameters, j.custom_val, j.event_payload, j.event_count, j.created_at, j.expire_at, latest_status.id, latest_status.job_state, latest_status.attempt, latest_status.error_code, latest_status.error_response FROM %1$I j LEFT JOIN %2$I latest_status on latest_status.job_id = j.job_id', alltables.table_name, 'v_last_' || prefix || '_job_status_'|| substring(alltables.table_name, char_length(prefix)+7,30)), | ||
' UNION ') INTO qry | ||
FROM (select table_name from information_schema.tables | ||
WHERE table_name LIKE prefix || '_jobs_%' order by table_name asc LIMIT num) alltables; | ||
RETURN QUERY EXECUTE qry; | ||
END; | ||
$$ LANGUAGE plpgsql; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: all gateways will need to be upgraded to the latest version so that the necessary schema migration will be performed on
gw
datasets.