One of the things I would like to do is move some of the application logic into stored procedures, so I'm here asking for DOs and DON'Ts (best practices) on using stored procedures in PostgreSQL (9.0), specifically regarding performance pitfalls.
Functions in PostgreSQL execute queries like prepared statements, which cuts off some of the overhead and make them generally faster that the equivalent SQL statements. This may be a noticeable effect depending on circumstances.
This carries the advantages and disadvantages of prepared statement - as described in the linked manual page. In the case of queries with very uneven data distribution and varying parameters and results it may be of advantage to use dynamic SQL with EXECUTE, because the gain from an optimized execution plan is bigger than the loss due to re-planning every time.
PostgreSQL 9.2 brought a major improvement in this area: The planner now plans the query at execution and decides whether it could be worth to replan with the current parameter values. So you get the best of both worlds, performance-wise, and you don't have to (ab)use
You can win big with server side functions when you prevent additional journeys to the database server from your application. Have the server execute as much as possible at once and only return a well defined result.
Avoid nesting of complex functions, especially table functions (
The exception to this rule are simple SQL functions (
In PostgreSQL a function is always an automatic transaction. All of it succeeds or nothing. If an exception occurs, everything is rolled back. But there is error handling ...
Here is a somewhat conservative but informative review of the capabilities of PL/pgSQL by the Czech PostgreSQL community. Note that it is a bit outdated by now (written for 8.1) and PL/pgSQL has seen a number of major improvements since.
I have written thousands of plpgsql functions over the years.
You can do some very interesting stuff using user defined functions (UDF) in postgresql. For instance, there's dozens of possible languages you can use. The built in pl/sql and pl/pgsql are both capable and reliable and use a sandbox method to keep users from doing anything too terribly dangerous. UDFs written in C give you the ultimate in power and performance, since they run in the same context as the database itself. However, it's like playing with fire, because even small mistakes can cause huge problems, with backends crashing or data getting corrupted. The custome pl languages, like pl/R, pl/ruby, pl/perl, and so on provide you with the ability to write both database and app layers in the same languages. This can be handy, since it means that you don't have to teach a perl programmer java or pl/pgsql etc to write a UDF.
Lastly, there is the pl/proxy language. This UDF language allows you to run your application across dozens or more backend postgresql servers for scaling purposes. It was developed by the good folks at Skype and basically allows for a poor man's horizontal scaling solution. It's surprisingly easy to write in as well.
Now, as to the performance issue. This is a gray area. Are you writing an app for one person? Or for 1,000? or for 10,000,000? The way you build your app and use UDFs will depend a LOT on how you're trying to scale. If you're writing for thousands and thousands of users, then the main thing you want to do is reduce the load on the db as much as possible. UDFs that reduce the amount of data being moved out and back into the database will help reduce IO load. However, if they start to increase CPU load, they may be an issue then. Generally speaking reducing IO load is the first priority, and making sure the UDFs are efficient so as not to overload your CPUs is next.
Generally speaking moving application logic into the database will mean it is faster - after all it will be running closer to the data.
I believe (but am not 100% sure) that SQL language functions are faster than those using any other languages because they do not require context switching. The downside is that no procedural logic is allowed.