Optimize getting pending documents query

Also, fix bug when saving entry without user.