Suppose a table defined like this :
CREATE TABLE clients (
active BOOLEAN
,deceased BOOLEAN
);
I define a function specific to that table (I don't remember how this is called nor can I find it in the CREATE FUNCTION
documentation or search engine) :
CREATE FUNCTION isvisible(clients) RETURNS BOOLEAN
AS $BODY$
BEGIN
RETURN $1.active AND NOT $1.deceased;
END;
$BODY$ LANGUAGE plpgsql STABLE;
I then get the list of clients with :
SELECT * FROM clients c WHERE c.isvisible;
In the actual table, there are several fields that could change the visible status of the record (mainly in the list of clients). I am planning to have such function so that the visible status is consistent across the application.
- Is it safe to declare such a function as IMMUTABLE ? I am wondering because any real field (ie:
active
ordeceased
) could potentially change within a transaction. - I would like to minimize the performance degradation that such function might induce. And since I have several cases where I'd like to implement such helper functions, I'd like to hear your advices before generalizing the concept.