uid <-> scalar schema change is refused after all of a predicate's data is deleted, for an unpredictable time
Describe the bug
Changing a predicate between uid and a scalar type is refused with Schema change not allowed from scalar to uid or vice versa while there is data for pred even after every value of the predicate has been deleted and has() returns 0.
The refusal lasts until every deleted key has been rolled up, which takes an unpredictable time. With one deleted edge
it cleared within seconds. With 150 deleted edges, separate runs on fresh containers cleared after 30 s, 30 s and
180 s, and two others were still refused at 180 s and at 600 s, when we stopped checking.
The check is hasEdges (worker/mutation.go:355-378). It counts any key whose newest version is not an empty rollup
(BitEmptyPosting) as data, and its own comment flags this:
// NOTE: This is NOT correct.
// An incorrect, but efficient way to quickly check if we have at least one non-empty
// posting. This does NOT consider those posting lists which can have multiple deltas
// summing up to an empty posting list.
A delete leaves delete deltas, and they count as data until a rollup writes the empty posting. The list to non-list
check in checkSchema (worker/mutation.go:444) calls the same hasEdges, so it should be affected the same way. We
read that in source and did not run it.
To Reproduce
This needs only Docker. Requests run inside the container, so no whitelist change is needed.
#!/usr/bin/env bash
set -euo pipefail
IMG=${IMG:-dgraph/dgraph:v25.5.1}
C=dg-hasedges-repro
docker rm -f "$C" >/dev/null 2>&1 || true
docker run -d --name "$C" "$IMG" sh -c 'dgraph zero >/tmp/z.log 2>&1 & sleep 3; exec dgraph alpha --zero localhost:5080' >/dev/null
trap 'docker rm -f "$C" >/dev/null' EXIT
d() { docker exec "$C" curl -s "$@"; }
until d localhost:8080/health 2>/dev/null | grep -q '"status":"healthy"'; do sleep 1; done; sleep 3
retype() { d -H 'Content-Type: application/dql' localhost:8080/alter -d "$1: string ." | grep -o '"message":"[^"]*"\|"code":"Success"' | head -1; }
count() { d -H 'Content-Type: application/dql' localhost:8080/query -d "{ q(func: has($1)) { count(uid) } }" | grep -o '"count":[0-9]*'; }
run_case() { # $1 predicate, $2 number of nodes
local p=$1 n=$2 set="" i
d -H 'Content-Type: application/dql' localhost:8080/alter -d "$p: uid ." >/dev/null; sleep 1
for i in $(seq 1 "$n"); do set+="${set:+,}{\"uid\":\"_:s$i\",\"$p\":{\"uid\":\"_:t$i\"}}"; done
d -H 'Content-Type: application/json' "localhost:8080/mutate?commitNow=true" -d "{\"set\":[$set]}" >/dev/null
echo "== $p: $n nodes with a $p edge; $(count "$p")"
d -H 'Content-Type: application/json' "localhost:8080/mutate?commitNow=true" \
-d "{\"query\":\"{ v as var(func: has($p)) }\",\"delete\":[{\"uid\":\"uid(v)\",\"$p\":null}]}" >/dev/null
echo " all $p edges deleted; has($p): $(count "$p")"
local t0=$SECONDS w
for w in 0 5 30 120; do
while (( SECONDS - t0 < w )); do sleep 1; done
count "$p" >/dev/null # a read at latest
echo " t+$((SECONDS - t0))s alter $p: string -> $(retype "$p")"
done
}
run_case A.id 1
run_case B.id 150
echo "== workaround: drop the predicate, then retype"
d -H 'Content-Type: application/json' localhost:8080/alter -d '{"drop_attr": "B.id"}' | grep -o '"code":"Success"'
echo " alter B.id: string -> $(retype B.id)"
Output from one run on dgraph/dgraph:v25.5.1:
== A.id: 1 nodes with a A.id edge; "count":1
all A.id edges deleted; has(A.id): "count":0
t+0s alter A.id: string -> "message":"Schema change not allowed from scalar to uid or vice versa while there is data for pred: A.id"
t+5s alter A.id: string -> "code":"Success"
t+30s alter A.id: string -> "code":"Success"
t+120s alter A.id: string -> "code":"Success"
== B.id: 150 nodes with a B.id edge; "count":150
all B.id edges deleted; has(B.id): "count":0
t+0s alter B.id: string -> "message":"Schema change not allowed from scalar to uid or vice versa while there is data for pred: B.id"
t+5s alter B.id: string -> "message":"Schema change not allowed from scalar to uid or vice versa while there is data for pred: B.id"
t+30s alter B.id: string -> "code":"Success"
t+120s alter B.id: string -> "code":"Success"
== workaround: drop the predicate, then retype
"code":"Success"
alter B.id: string -> "code":"Success"
The 150-node case cleared at 30 s in this run. In other runs on fresh containers it was still refused at 180 s, and in
one at 600 s. The first two checks were refused in every run, with has() already returning 0.
Expected behavior
Once every value of the predicate is deleted, and has() returns 0, the change should be allowed, as it already is
after the deleted keys roll up.
Screenshots
None.
Environment
- OS: macOS (arm64), running the
dgraph/dgraph:v25.5.1Docker image - Language: n/a (DQL over HTTP)
- Version: v25.5.1.
hasEdgesis unchanged onmainat c587cba75.
Additional context
- Workaround: drop the predicate (
{"drop_attr": "<pred>"}), then alter it with the new type. The last step of the script shows this. - An earlier report of the same message: https://discuss.dgraph.io/t/schema-change-not-allowed-from-scalar-to-uid-or-vice-versa-while-there-is-data-for-pred/8898
- A possible fix: when a key's newest version is a delta rather than a complete or empty rollup, read the list at
startTsand check whether it is empty, instead of counting the key as data. Alter is rare, so the extra reads should be affordable. Rolling up the predicate's keys before the check would also work.