{"record":{"id":"14369541d8c54181","repo":"temporalio/temporal","slug":"rowsaffected-returned-v-shards-instead-of-one","errorCode":null,"errorMessage":"rowsAffected returned %v shards instead of one","messagePattern":"rowsAffected returned (.+?) shards instead of one","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/persistence/sql/shard.go","lineNumber":109,"sourceCode":"\t\t\tm.logger,\n\t\t); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tresult, err := tx.UpdateShards(ctx, &sqlplugin.ShardsRow{\n\t\t\tShardID:      request.ShardID,\n\t\t\tRangeID:      request.RangeID,\n\t\t\tData:         request.ShardInfo.Data,\n\t\t\tDataEncoding: request.ShardInfo.EncodingType.String(),\n\t\t})\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\trowsAffected, err := result.RowsAffected()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"rowsAffected returned error for shardID %v: %v\", request.ShardID, err)\n\t\t}\n\t\tif rowsAffected != 1 {\n\t\t\treturn fmt.Errorf(\"rowsAffected returned %v shards instead of one\", rowsAffected)\n\t\t}\n\t\treturn nil\n\t})\n}\n\nfunc (m *sqlShardStore) AssertShardOwnership(\n\tctx context.Context,\n\trequest *persistence.AssertShardOwnershipRequest,\n) error {\n\t// AssertShardOwnership is not implemented for sql shard store\n\treturn nil\n}\n\n// initiated by the owning shard\nfunc lockShard(\n\tctx context.Context,\n\ttx sqlplugin.Tx,\n\tshardID int32,","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/temporalio/temporal/blob/bde624efd13fbd3843654058db6d9c716166318b/common/persistence/sql/shard.go#L91-L127","documentation":"Same create-shard verification path: the INSERT of a shard row must affect exactly 1 row; any other count causes this error. It guarantees one canonical shard row per shardID, so a non-1 count is treated as a storage invariant violation.","triggerScenarios":"Creating a shard where result.RowsAffected() returns 0 (row not actually inserted) or >1 (e.g., driver counting quirks).","commonSituations":"Replication setups reporting odd affected-row counts; triggers on the shards table; silent insert suppression from stale connections or unusual driver semantics.","solutions":["Query the shards table for the given shardID to see whether the row exists; delete duplicates if more than one.","Check for triggers or modified schema affecting the shards table.","Retry the operation; if the shard already exists, the normal read path (GetShard/ownership checks) will handle it.","Verify driver and backend compatibility; test the same INSERT manually against the database."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var n int\ndb.QueryRow(\"SELECT COUNT(*) FROM shards WHERE shard_id = ?\", shardID).Scan(&n)\n// n should be 0 before creation; n > 1 means duplicates already exist","typeGuard":null,"tryCatchPattern":"if strings.Contains(err.Error(), \"shards instead of one\") {\n    // inspect shards table for the shardID; repair duplicates, then retry\n}","preventionTips":["Keep the shards schema (primary key on shard_id) unmodified from upstream.","Avoid triggers on the shards table.","Run one shard-creation owner per shard to prevent races."],"tags":["database","shard","persistence","concurrency"],"backgroundTag":"unexpected-rows-affected","analyzedSha":"bde624efd13fbd3843654058db6d9c716166318b","analyzedAt":"2026-09-01T07:18:39.080Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}