Skip to content

Commit 612d49f

Browse files
committed
upgrade: remove last full scan from get jobs query
This removes the last full scan from the query used to find migration jobs. ``` • root │ ├── • hash join │ │ equality: (job_id) = (id) │ │ right cols are key │ │ │ ├── • render │ │ │ │ │ └── • lookup join │ │ │ table: job_info@primary │ │ │ equality: (id, lookup_join_const_col_@16) = (job_id,info_key) │ │ │ │ │ └── • render │ │ │ │ │ └── • scan buffer │ │ label: buffer 1 (running_migration_jobs) │ │ │ └── • scan buffer │ label: buffer 1 (running_migration_jobs) │ └── • subquery │ id: @s1 │ original sql: SELECT id, status FROM system.jobs WHERE (status IN ('running', 'pending', 'cancel-requested', 'pause-requested', 'reverting', 'paused')) AND (job_type = 'MIGRATION') │ exec mode: all rows │ └── • buffer │ label: buffer 1 (running_migration_jobs) │ └── • filter │ filter: status IN ('cancel-requested', 'pause-requested', 'paused', 'pending', 'reverting', 'running') │ └── • index join │ table: jobs@primary │ └── • scan missing stats table: jobs@jobs_job_type_idx spans: [/'MIGRATION' - /'MIGRATION'] ``` Note that if a migration job has more than 1 legacy_payload key, it will cause an assertion failure error since the query will produce multiple rows. We should not have multiple legacy_payload keys because we delete any existing payload keys anytime we write a new payload key. Epic: none Release note: None
1 parent e147a55 commit 612d49f

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

pkg/upgrade/upgrademanager/manager.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -792,28 +792,28 @@ FROM (
792792
WHERE status IN ` + jobs.NonTerminalStatusTupleString + `
793793
)
794794
WHERE ((pl->'migration')->'clusterVersion') = $1::JSONB`
795+
// postJobInfoQuery avoids the crdb_internal.system_jobs table
796+
// to avoid expensive full scans.
795797
postJobInfoTableQuery = `
796-
WITH latestpayload AS (
797-
SELECT job_id, value FROM system.job_info AS payload
798-
WHERE info_key = 'legacy_payload'
799-
ORDER BY written DESC
798+
WITH
799+
running_migration_jobs AS (
800+
SELECT id, status
801+
FROM system.jobs
802+
WHERE status IN ` + jobs.NonTerminalStatusTupleString + `
803+
AND job_type = 'MIGRATION'
804+
),
805+
payloads AS (
806+
SELECT job_id, value
807+
FROM system.job_info AS payload
808+
WHERE info_key = 'legacy_payload'
809+
AND job_id IN (SELECT id FROM running_migration_jobs)
810+
ORDER BY written DESC
800811
)
801-
SELECT id, status
802-
FROM (
803-
SELECT
804-
distinct(id),
805-
status,
806-
crdb_internal.pb_to_json(
807-
'cockroach.sql.jobs.jobspb.Payload',
808-
payload.value,
809-
false -- emit_defaults
810-
) AS pl
811-
FROM system.jobs AS j
812-
INNER JOIN latestpayload AS payload ON j.id = payload.job_id
813-
WHERE j.status IN ` + jobs.NonTerminalStatusTupleString + `
814-
AND j.job_type = 'MIGRATION'
815-
)
816-
WHERE ((pl->'migration')->'clusterVersion') = $1::JSONB`
812+
SELECT id, status FROM (
813+
SELECT id, status, crdb_internal.pb_to_json('cockroach.sql.jobs.jobspb.Payload', payloads.value, false) AS pl
814+
FROM running_migration_jobs AS j
815+
INNER JOIN payloads ON j.id = payloads.job_id
816+
) WHERE ((pl->'migration')->'clusterVersion') = $1::JSONB`
817817
)
818818

819819
func (m *Manager) getRunningMigrationJob(

0 commit comments

Comments
 (0)