Partial backport of Core PR13111
https://github.com/bitcoin/bitcoin/pull/13111/commits/0b82bac76d0f842bd2294a290388536951fbc576 (only the remaining change to wallet/db.cpp)
https://github.com/bitcoin/bitcoin/pull/13111/commits/fe65bdec237776dbe094339509dfd2e63329a832
Depends on D4254