84 points · by craigkerstiens · 13y ago · 11 comments
CREATE UNIQUE INDEX foo_fidx ON foo (lower(a));
Will reject values of "a" that have the same lower-case value as something that already exists, even if the values of "a" are different. Exclusion Constraints[1] can make use of this, as well.Also, you can do partial indexes, which have a WHERE clause. You can combine those with unique indexes, too:
CREATE UNIQUE INDEX foo_idx ON foo (lower(a))
WHERE b <> 0;
Exclusion constraints also accept a WHERE clause.Additionally, if you do a partial and/or functional index, then postgres will collect stats specific to the function and predicate, which improves plans even if the index isn't used.
[1] http://www.postgresql.org/docs/current/static/sql-createtabl...
IMMUTABLE indicates that the function cannot modify the database and always returns the same result when given the same argument values; that is, it does not do database lookups or otherwise use information not directly present in its argument list. If this option is given, any call of the function with all-constant arguments can be immediately replaced with the function value.
STRICT does not mean 'no side effects'; it means 'returns NULL when given a NULL argument'. From the same page:
CALLED ON NULL INPUT (the default) indicates that the function will be called normally when some of its arguments are null. It is then the function author's responsibility to check for null values if necessary and respond appropriately.
RETURNS NULL ON NULL INPUT or STRICT indicates that the function always returns null whenever any of its arguments are null. If this parameter is specified, the function is not executed when there are null arguments; instead a null result is assumed automatically.
[looked that up because I couldn't think of a function that was STRICT but not IMMUTABLE according to the definition given]
STRICT is less advisory: if specified, the function will not be evaluated if it any NULL is seen in the argument list (NULL will be yielded immediately), without ever passing control to the code in the function.