dgraph-io/dgraph · error

error while writing string

Error message

error while writing string

What it means

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.

Source

Thrown at edgraph/server.go:1984

			// In the code below, we construct respective query for the above use-cases viz.
			//   __dgraph_uniquecheck_1__ as var(func: eq(email,"example@email.com"))
			//   __dgraph_uniquecheck_2__ as var(func: eq(email, val(queryVariable))){
			//       uid
			//       __dgraph_uniquecheck_val_2__ as email
			//   }
			// Each of the above query will check if there is already an existing value.
			// We can be sure that we may get at most one UID in return.
			// If the returned UID is different than the UID we have
			// in the mutation, then we reject the mutation.

			if !strings.HasPrefix(pred.ObjectId, "val(") {
				if pred.ObjectValue == nil {
					continue
				}
				val := strconv.Quote(fmt.Sprintf("%v", dql.TypeValFrom(pred.ObjectValue).Value))
				query := fmt.Sprintf(`%v as var(func: eq(%v,"%v"))`, queryVar, predicateName, val[1:len(val)-1])
				if _, err := buildQuery.WriteString(query); err != nil {
					return errors.Wrapf(err, "error while writing string")
				}
				qc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{queryVar: queryVar}
			} else {
				if err := validateValObjectId(pred.ObjectId); err != nil {
					return err
				}
				valQueryVar := fmt.Sprintf("__dgraph_uniquecheck_val_%v__", uniqueVarMapKey)
				query := fmt.Sprintf(`%v as var(func: eq(%v,%v)){
					                             uid
					                             %v as %v
				                 }`, queryVar, predicateName, pred.ObjectId, valQueryVar, predicateName)
				if _, err := buildQuery.WriteString(query); err != nil {
					return errors.Wrapf(err, "error while building unique query")
				}
				qc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{valVar: valQueryVar, queryVar: queryVar}
			}
		}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Retry the mutation; if it recurs, check the Alpha process memory (RSS, OOM logs, container limits) and increase available memory
  2. Check Alpha logs for the wrapped cause to confirm it is a builder/alloc failure and not a downstream bug
  3. Report to Dgraph with the full wrapped error and mutation if reproducible, since WriteString on a plain strings.Builder should not fail
  4. Restart the affected Alpha if the process is in a degraded state
Defensive patterns

Strategy: retry

Try / catch

err := mutate(ctx, mu)
if err != nil && strings.Contains(err.Error(), "error while writing string") {
	// transient/internal: retry once with backoff, then escalate with logs
}

Prevention

When it happens

Trigger: 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).

Common situations: Severe memory pressure on the Alpha process; virtually never caused by the caller's mutation content itself — the mutation merely triggers the code path.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/02cca6155c1524d3. Report an issue: GitHub.