According to my research, the only way to do this is to write a bash script in Linux and insert it with the loop.
Right. It is fundamentally impossible to store something in the database capable of executing multiple commands and not execute it within a normal transaction. The maintenance work that cannot be done within a transaction must be sourced externally and executed in an implicit transaction that auto-commits after the command finishes.
David J.