{"record":{"id":"defe73aa451fad3a","repo":"vitessio/vitess","slug":"shard-v-v-doesn-t-have-a-primary-set","errorCode":null,"errorMessage":"shard %v/%v doesn't have a primary set","messagePattern":"shard (.+?)/(.+?) doesn't have a primary set","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtctl/workflow/utils.go","lineNumber":749,"sourceCode":"\n\tgetVReplicationWorkflowSubType := func(row sqltypes.RowNamedValues) binlogdatapb.VReplicationWorkflowSubType {\n\t\ti, _ := row[\"workflow_sub_type\"].ToInt32()\n\t\treturn binlogdatapb.VReplicationWorkflowSubType(i)\n\t}\n\n\t// We check all shards in the target keyspace. Not all of them may have a\n\t// stream. For example, if we're splitting -80 to [-40,40-80], only those\n\t// two target shards will have vreplication streams, and the other shards in\n\t// the target keyspace will not.\n\tfor _, targetShard := range targetShards {\n\t\tsi, err := ts.GetShard(ctx, targetKeyspace, targetShard)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tif si.PrimaryAlias == nil {\n\t\t\t// This can happen if bad inputs are given.\n\t\t\treturn nil, fmt.Errorf(\"shard %v/%v doesn't have a primary set\", targetKeyspace, targetShard)\n\t\t}\n\n\t\tprimary, err := ts.GetTablet(ctx, si.PrimaryAlias)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\t// NB: changing the whitespace of this query breaks tests for now.\n\t\t// (TODO:@ajm188) extend FakeDBClient to be less whitespace-sensitive on\n\t\t// expected queries.\n\t\tquery := fmt.Sprintf(\"select id, source, message, cell, tablet_types, workflow_type, workflow_sub_type, defer_secondary_keys from _vt.vreplication where workflow=%s and db_name=%s\", encodeString(workflow), encodeString(primary.DbName()))\n\t\tp3qr, err := tmc.VReplicationExec(ctx, primary.Tablet, query)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tif len(p3qr.Rows) < 1 {\n\t\t\tcontinue","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtctl/workflow/utils.go#L731-L767","documentation":"LegacyBuildTargets requires each target shard in the workflow to have an elected primary so it can run VReplicationExec queries against it. When the shard record in the topology has a nil PrimaryAlias, the function aborts with this error, noting it usually stems from bad inputs — e.g. naming a shard that was never initialized or whose primary was removed.","triggerScenarios":"Calling buildTrafficSwitcher → LegacyBuildTargets with a targetShards entry whose shard in topo has no primary: freshly created empty shard, primary tablet deleted/pruned without ERS, or a typo'd shard name pointing at an uninitialized shard.","commonSituations":"Running MoveTables/Reshard SwitchTraffic right after creating a new empty shard before reparenting; primary was deliberately removed during decommissioning; cluster recovered from topo backup where the shard record lost its primary alias.","solutions":["Verify the shard's state with vtctldclient GetShard <keyspace>/<shard> and confirm PrimaryAlias is set.","Elect a primary via vtctldclient PlannedReparentShard or EmergencyReparentShard for that shard.","Correct the shard list passed to the workflow command if the shard name was wrong or shouldn't be included.","If the shard is truly empty/unused, remove it (vtctldclient DeleteShard) or drop it from the migration inputs."],"exampleFix":"// before (shard -80 has no primary)\nvtctldclient Reshard --workflow x --target-keyspace=ks SwitchTraffic --shards=-80\n// after (elect primary first)\nvtctldclient PlannedReparentShard ks/-80 --new-primary=zone1-101","handlingStrategy":"validation","validationCode":"for _, shard := range targetShards {\n\tsi, err := ts.GetShard(ctx, targetKeyspace, shard)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif si.PrimaryAlias == nil {\n\t\treturn fmt.Errorf(\"precheck: %s/%s has no primary\", targetKeyspace, shard)\n\t}\n}","typeGuard":"func shardHasPrimary(si *topo.ShardInfo) bool { return si != nil && si.PrimaryAlias != nil }","tryCatchPattern":"ti, err := workflow.LegacyBuildTargets(ctx, ts, tmc, ks, wf, shards)\nif err != nil {\n\tif strings.Contains(err.Error(), \"doesn't have a primary set\") {\n\t\t// reparent or fix shard list before retrying\n\t}\n\treturn err\n}","preventionTips":["Ensure every shard is reparented (has an elected primary) before running workflow operations","Verify shard names with vtctldclient GetShard before including them in migration commands","Avoid decommissioning primaries while workflows targeting that shard are active"],"tags":["topology","primary","vreplication","workflow"],"backgroundTag":"shard-missing-primary","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}