[1.6.x] Fixed #21109 -- made db cursor error wrapping faster

Backpatch of 94001421321bd8808c4027a72aa32a1eef005764 from master.