We have occasional need to run very long-running pl/tcl scripts. If the request is cancelled (say, by the user hitting ^c in psql) the server-side script still runs to completion.
There is a C-level variable QueryCancelPending that can be used to monitor for this case, but it’s not visible at the pl/tcl scripting level. This is a simple new command that returns the current state of this variable to Tcl.
We are currently maintaining a fork of pl/tcl at https://github.com/flightaware/pltcl that has this mod, but it would be useful to get the functionality into mainline PostgreSQL.