{"record":{"id":"02cca6155c1524d3","repo":"dgraph-io/dgraph","slug":"error-while-writing-string","errorCode":null,"errorMessage":"error while writing string","messagePattern":"error while writing string","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"edgraph/server.go","lineNumber":1984,"sourceCode":"\t\t\t// In the code below, we construct respective query for the above use-cases viz.\n\t\t\t//   __dgraph_uniquecheck_1__ as var(func: eq(email,\"example@email.com\"))\n\t\t\t//   __dgraph_uniquecheck_2__ as var(func: eq(email, val(queryVariable))){\n\t\t\t//       uid\n\t\t\t//       __dgraph_uniquecheck_val_2__ as email\n\t\t\t//   }\n\t\t\t// Each of the above query will check if there is already an existing value.\n\t\t\t// We can be sure that we may get at most one UID in return.\n\t\t\t// If the returned UID is different than the UID we have\n\t\t\t// in the mutation, then we reject the mutation.\n\n\t\t\tif !strings.HasPrefix(pred.ObjectId, \"val(\") {\n\t\t\t\tif pred.ObjectValue == nil {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tval := strconv.Quote(fmt.Sprintf(\"%v\", dql.TypeValFrom(pred.ObjectValue).Value))\n\t\t\t\tquery := fmt.Sprintf(`%v as var(func: eq(%v,\"%v\"))`, queryVar, predicateName, val[1:len(val)-1])\n\t\t\t\tif _, err := buildQuery.WriteString(query); err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"error while writing string\")\n\t\t\t\t}\n\t\t\t\tqc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{queryVar: queryVar}\n\t\t\t} else {\n\t\t\t\tif err := validateValObjectId(pred.ObjectId); err != nil {\n\t\t\t\t\treturn err\n\t\t\t\t}\n\t\t\t\tvalQueryVar := fmt.Sprintf(\"__dgraph_uniquecheck_val_%v__\", uniqueVarMapKey)\n\t\t\t\tquery := fmt.Sprintf(`%v as var(func: eq(%v,%v)){\n\t\t\t\t\t                             uid\n\t\t\t\t\t                             %v as %v\n\t\t\t\t                 }`, queryVar, predicateName, pred.ObjectId, valQueryVar, predicateName)\n\t\t\t\tif _, err := buildQuery.WriteString(query); err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"error while building unique query\")\n\t\t\t\t}\n\t\t\t\tqc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{valVar: valQueryVar, queryVar: queryVar}\n\t\t\t}\n\t\t}\n","sourceCodeStart":1966,"sourceCodeEnd":2002,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/edgraph/server.go#L1966-L2002","documentation":"addQueryIfUnique builds a DQL var query `X as var(func: eq(P,\"value\"))` into a strings.Builder to check uniqueness of literal mutation values. If WriteString returns an error (out-of-memory or builder misuse), the error is wrapped as 'error while writing string' (edgraph/server.go:1984). In practice this almost never fires from app behavior — it surfaces as an internal failure while constructing the unique-check query for a literal-valued N-Quad on a @unique predicate.","triggerScenarios":"Internal failure while Dgraph appends the unique-check query text for an N-Quad whose object is a literal value (not val(...)) on a @unique predicate — effectively only from strings.Builder write errors (edgraph/server.go:1984).","commonSituations":"Severe memory pressure on the Alpha process; virtually never caused by the caller's mutation content itself — the mutation merely triggers the code path.","solutions":["Retry the mutation; if it recurs, check the Alpha process memory (RSS, OOM logs, container limits) and increase available memory","Check Alpha logs for the wrapped cause to confirm it is a builder/alloc failure and not a downstream bug","Report to Dgraph with the full wrapped error and mutation if reproducible, since WriteString on a plain strings.Builder should not fail","Restart the affected Alpha if the process is in a degraded state"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := mutate(ctx, mu)\nif err != nil && strings.Contains(err.Error(), \"error while writing string\") {\n\t// transient/internal: retry once with backoff, then escalate with logs\n}","preventionTips":["Ensure Alphas have adequate memory headroom","Watch for OOM-killer events and set sane container limits","Keep Dgraph upgraded; a strings.Builder write should never fail","Capture full wrapped error chains in logs when reporting"],"tags":["dgraph","internal","mutation","unique-constraint"],"backgroundTag":"internal-write-failure","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}