In traditional programming there is an axiom that states, "do not use errors for flow control". A general example is to throw an error then catch the error instead of using an ordinary conditional statement or break statement. This is harmful because the application has to unwind the call stack and invoke some relatively expensive exception handling logic versus simply handling a conditional statement.

I am working with a Postgres system where a user is calling a function in Postgres that throws an error instead of returning no rows when a condition is not met. The condition is roughly a "this input value does not exist, nothing to do" as opposed to a truly exceptional case.

Does Postgres incur runtime costs when an error is thrown in a similar manner traditional programming? In other words is using exceptions as flow control in Postgres harmful to performance or just sloppy?

  • I assume you refer to plpgsql functions? And I assume, by "stored procedures" you mean "functions"? (But the same happens to apply to actual stored procedures as well.) Mar 28, 2019 at 14:22

1 Answer 1


Can't tell, from the top of my head, the exact sequence of events in case of an exception. But I can tell you that it is comparatively expensive and should be avoided unless needed. An exception (error) causes the surrounding transaction to be rolled back, unless trapped.

But to trap an error, you need an EXCEPTION clause in a plpgsql block (in a function, procedure or a DO statement). And it is expensive to have an EXCEPTION clause in a plpgsql function block to begin with (even if no exceptions are raised), because such a block effectively forms a subtransaction (that can potentially be rolled back) incurring substantially more overhead. Consequently, the manual warns:

A block containing an EXCEPTION clause is significantly more expensive to enter and exit than a block without one. Therefore, don't use EXCEPTION without need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.