Instead of checking its plan node's type, I suggest you check its reference. Because if "candidate" is a index scan plan node even before your "indexAccessForGroupByExprs" function, you do not need to all the cleanChildren and addAndLinkChild logic.