I found a problem in plpgsql. When there is a large loop in plpgsql, it is found that the change of search_path will cause memory exhaustion and thus disconnect the connection.
It is impossible to prevent queries from exhausting memory so the fact that this one does isn't an indication of a bug on its own. I'm having trouble imagining a reasonable use case for this pattern of changing search_path frequently in a loop within a single execution of a function. That said, I'm not in a position to judge how easy or difficult an improvement in this area may be.