{"record":{"id":"44ba2cb77627d804","repo":"vitessio/vitess","slug":"shard-s-does-not-have-a-primary","errorCode":null,"errorMessage":"shard: %s does not have a primary","messagePattern":"shard: (.+?) does not have a primary","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/schemamanager/tablet_executor.go","lineNumber":123,"sourceCode":"// hasProvidedUUIDs returns true when UUIDs were provided\nfunc (exec *TabletExecutor) hasProvidedUUIDs() bool {\n\treturn len(exec.uuids) != 0\n}\n\n// Open opens a connection to the primary for every shard.\nfunc (exec *TabletExecutor) Open(ctx context.Context, keyspace string) error {\n\tif !exec.isClosed {\n\t\treturn nil\n\t}\n\texec.keyspace = keyspace\n\tshards, err := exec.ts.FindAllShardsInKeyspace(ctx, keyspace, nil)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to get shards for keyspace: %s, error: %v\", keyspace, err)\n\t}\n\texec.tablets = make([]*topodatapb.Tablet, 0, len(shards))\n\tfor shardName, shardInfo := range shards {\n\t\tif !shardInfo.HasPrimary() {\n\t\t\treturn fmt.Errorf(\"shard: %s does not have a primary\", shardName)\n\t\t}\n\t\ttabletInfo, err := exec.ts.GetTablet(ctx, shardInfo.PrimaryAlias)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to get primary tablet info, keyspace: %s, shard: %s, error: %v\", keyspace, shardName, err)\n\t\t}\n\t\texec.tablets = append(exec.tablets, tabletInfo.Tablet)\n\t}\n\n\tif len(exec.tablets) == 0 {\n\t\treturn fmt.Errorf(\"keyspace: %s does not contain any primary tablets\", keyspace)\n\t}\n\texec.isClosed = false\n\treturn nil\n}\n\n// Validate validates a list of sql statements.\nfunc (exec *TabletExecutor) Validate(ctx context.Context, sqls []string) error {\n\tif exec.isClosed {","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/schemamanager/tablet_executor.go#L105-L141","documentation":"During TabletExecutor.Open, the executor lists all shards of the keyspace and refuses to proceed if any shard has no primary tablet. Vitess cannot run schema management against a shard without an elected primary.","triggerScenarios":"Calling schemamanager (e.g. ApplySchema via tablet_executor) when one or more shards of the target keyspace lack a primary (ShardInfo.HasPrimary() == false), typically after a failed primary election or an empty/disabled keyspace.","commonSituations":"Running ApplySchema on a keyspace with a shard still in orphaned state, a shard whose primary alias was pruned by the new shard pruning policy, or during resharding when the target shard has no primary yet.","solutions":["Fix replication on the affected shard: run `vttablet`/`vtctl InitShardPrimary` (or PlannedReparentShard) to elect a primary","Verify shard state with `vtctl GetShard <keyspace>/<shard>` and confirm PrimaryAlias is set","If the shard should not exist, remove it with `vtctl DeleteShard` or correct the keyspace targeted by the schema operation","Retry the schema application once all shards have primaries"],"exampleFix":"// before: applying schema to keyspace with empty shard\nvtctlclient ApplySchema -sql \"...\" commerce\n// after: ensure primary exists first, or prune empty shards\nvtctlclient RemoveKeyspace -recursive empty_ks\nvtctlclient ApplySchema -sql \"...\" commerce","handlingStrategy":"validation","validationCode":"shards, err := ts.GetShardMap(ctx, keyspace)\nif err != nil { return err }\nfor name, info := range shards {\n    if !info.HasPrimary() {\n        return fmt.Errorf(\"shard %s has no primary; fix topology before ApplySchema\", name)\n    }\n}","typeGuard":"func shardHasPrimary(s *topodatapb.Shard) bool {\n    return s != nil && s.PrimaryAlias != nil && s.PrimaryAlias.Uid != 0\n}","tryCatchPattern":"if err := executor.Open(ctx, keyspace); err != nil {\n    if strings.Contains(err.Error(), \"does not have a primary\") {\n        return fmt.Errorf(\"keyspace %s not ready for schema change: %w\", keyspace, err)\n    }\n    return err\n}","preventionTips":["Check GetShard for PrimaryAlias before schema operations","Elect primaries with PlannedReparentShard before maintenance windows","Avoid running ApplySchema during resharding until target shards have primaries"],"tags":["schemamanager","topology","replication"],"backgroundTag":"shard-has-no-primary","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}