vitessio/vitess · error

source shard has no primary

Error message

source shard has no primary: %v

What it means

CreateLookupVindex needs to fetch the source table's schema from a source shard's primary tablet. If the first serving shard's shard record has no PrimaryAlias, there is no primary tablet to query and preparation fails.

Solutions

  1. Ensure every source shard has a healthy primary (`vtctldclient Placements`/`GetTablets`, run PRS if needed) and retry
  2. Wait for any in-flight reshard to finish or use a fully-serving source keyspace
  3. Inspect the shard record (`vtctldclient GetShard ks/shard`) and repair missing PrimaryAlias via PlannedReparentShard

Example fix

null
Defensive patterns

Strategy: validation

Validate before calling

shards, err := ts.GetServingShards(ctx, keyspace)
if err != nil { return err }
for _, sh := range shards {
    if sh.PrimaryAlias == nil {
        return fmt.Errorf("shard %s has no primary; fix replication first", sh.ShardName())
    }
}

Type guard

func shardHasPrimary(sh *topodata.Shard) bool { return sh.PrimaryAlias != nil }

Try / catch

if err := createLookupVindex(...); err != nil {
    if strings.Contains(err.Error(), "has no primary") {
        return repairShardPrimary(shardName) // run PRS, then retry
    }
    return err
}

Prevention

When it happens

Trigger: The first serving shard of the source keyspace has no elected primary — e.g. during a reshard while the old shard's primary was removed, after ERS/PRS failure, or a shard record left with PrimaryAlias nil following manual topo edits.

Common situations: Running CreateLookupVindex mid-reshard when a source shard is orphaned; a primary tablet down and not yet promoted; corrupt/legacy topo data lacking primary info.

Related errors


AI-assisted analysis of vitessio/vitess@01a25a7d17 (2026-09-01). Data as JSON: /api/errors/3f2acc464efbf5bf. Report an issue: GitHub.

Appendix: source

Thrown at go/vt/wrangler/materializer.go:656

			continue
		}
		colName := colVindex.Column
		if len(colVindex.Columns) != 0 {
			colName = colVindex.Columns[0]
		}
		if colName == sourceVindexColumns[0] {
			return nil, nil, nil, fmt.Errorf("ColumnVindex for table %v already exists: %v, please remove it and try again", sourceTableName, colName)
		}
	}

	// Validate against source schema
	sourceShards, err := wr.ts.GetServingShards(ctx, keyspace)
	if err != nil {
		return nil, nil, nil, err
	}
	onesource := sourceShards[0]
	if onesource.PrimaryAlias == nil {
		return nil, nil, nil, fmt.Errorf("source shard has no primary: %v", onesource.ShardName())
	}
	req := &tabletmanagerdatapb.GetSchemaRequest{Tables: []string{sourceTableName}}
	tableSchema, err := schematools.GetSchema(ctx, wr.ts, wr.tmc, onesource.PrimaryAlias, req)
	if err != nil {
		return nil, nil, nil, err
	}
	if len(tableSchema.TableDefinitions) != 1 {
		return nil, nil, nil, fmt.Errorf("unexpected number of tables returned from schema: %v", tableSchema.TableDefinitions)
	}

	// Generate "create table" statement
	lines := strings.Split(tableSchema.TableDefinitions[0].Schema, "\n")
	if len(lines) < 3 {
		// Unreachable
		return nil, nil, nil, fmt.Errorf("schema looks incorrect: %s, expecting at least four lines", tableSchema.TableDefinitions[0].Schema)
	}
	var modified []string
	modified = append(modified, strings.Replace(lines[0], sourceTableName, targetTableName, 1))

View on GitHub (pinned to 01a25a7d17)