siyuan-note/siyuan · error

view state patch contains too many entries

Error message

view state patch contains too many entries

What it means

PatchViewState enforces a limit (maxViewStatePatchCount = 1000) on the total number of entries in a single patch — len(values) + len(removeKeys). Patches exceeding this count are rejected with this error to bound per-request work and storage churn on view-state.json.

Solutions

  1. Split the patch into batches of at most ~1000 combined entries and call PatchViewState per batch
  2. Reduce entries by removing only stale keys rather than sending the entire state each time
  3. If legitimately needing large bulk updates, perform sequential smaller patches inside a loop

Example fix

// before
PatchViewState(key, allValues /* 5000 entries */, allRemoveKeys)
// after
for batch := range chunks(allValues, 500) {
    PatchViewState(key, batch, nil)
}
Defensive patterns

Strategy: validation

Validate before calling

func canPatch(values map[string]any, removeKeys []string) bool {
    return len(values)+len(removeKeys) <= 1000
}

Try / catch

ret, err := PatchViewState(key, values, removeKeys)
if err != nil && err.Error() == "view state patch contains too many entries" {
    // retry in batches of <= 1000 entries
}

Prevention

When it happens

Trigger: Calling PatchViewState (or the view state HTTP API) with a values map plus removeKeys list whose combined size exceeds 1000 entries, e.g. bulk-clearing thousands of view fields in one call.

Common situations: A frontend flushing a large in-memory state object in a single patch; a bulk migration writing thousands of keys at once; a loop accumulating removeKeys unboundedly before one final patch.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/58df254ec0ed303d. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/view_state.go:87

	defer viewStateStorageLock.Unlock()

	storage, err := getViewStateStorage()
	if err != nil {
		return nil, err
	}
	state := storage.Views[key]
	if nil == state || nil == state.Data {
		return map[string]any{}, nil
	}
	return cloneViewStateData(state.Data)
}

func PatchViewState(key string, values map[string]any, removeKeys []string) (ret map[string]any, err error) {
	if err = validateViewStateKey(key); err != nil {
		return
	}
	if maxViewStatePatchCount < len(values)+len(removeKeys) {
		return nil, errors.New("view state patch contains too many entries")
	}
	for valueKey := range values {
		if err = validateViewStateDataKey(valueKey); err != nil {
			return nil, err
		}
	}
	for _, removeKey := range removeKeys {
		if err = validateViewStateDataKey(removeKey); err != nil {
			return nil, err
		}
	}
	patch := &viewStatePatch{Values: values, RemoveKeys: removeKeys}
	patchData, marshalErr := gulu.JSON.MarshalJSON(patch)
	if nil != marshalErr {
		return nil, marshalErr
	}
	if maxViewStatePatchBytes < len(patchData) {
		return nil, errors.New("view state patch is too large")

View on GitHub (pinned to 9f775e8a12)