Viktor/sqlfunctions

This is a massive rewrite of a lot of stuff, but it will let us lock down the database a lot better.

I've tested everything affected afaik, but I could have missed something. We need to write real integration tests.
It sounds like the only advantage of stable is that if it's called twice within a transaction it will be evaluated only once. We never call any of these functions twice within a transaction, so changing that won't make a difference. It's more correct, but if we starting setting functions to stable, I would rather do it for all functions instead of just this one. In conclusion, I think we should do this in a separate commit later on. This commit shouldn't change too many things at once. It's already massive as it is.