Backport relation merger updates from 9675c7d to 4-0-stable

At @rafaelfranca's request, this backports commit 9675c7d into the 4-0-stable branch to fix #10995. All tests are passing.

This is the first time I've ever attempted backporting; any feedback is greatly appreciated!