Q:

Does PostgreSQL cache function calls? 

Suppose I have a STABLE SQL function taking one parameter (internally the function performs a SELECT on a bunch of tables, though as far as the caller is concerned, the function is really just a black box).  
If this function is invoked multiple times with the same parameter within the same SELECT statement, is PostgreSQL smart enough to only actually call it once and cache the result, or do I have to perform the caching manually using WITH statements (common table expressions)? 

A:

PostgreSQL has no cache for result of stable functions (it has no cache for any function result).  PostgreSQL try to minimize number of calls, but it has no cache.  You can try to penalize these calls by higher COST attribute.  Using WITH should be good idea. 