Skip to content
Advertisement

Why am I getting a an error when creating a generated column in PostgreSQL?

Error: generation expression is not immutable

The goal is to populate the first name and last into the full name column.

Advertisement

Answer

concat() is a not IMMUTABLE (only STABLE) because it can invoke datatype output functions (like timestamptz_out) that depend on locale settings. Tom Lane (core developer) explains it here.

And first_name || ' ' || last_name is not equivalent to concat(first_name, ' ', last_name) while at least one column can be NULL.

Detailed explanation:

Solution

To make it work, exactly the way you demonstrated:

db<>fiddle here

The CASE expression is as fast as it gets – substantially faster than multiple concatenation and function calls. And exactly correct.

Or, if you know what you are doing and have the necessary privileges, create an IMMUTABLE concat-function as demonstrated here (to replace the CASE expression):

Aside: full_name needs to be varchar(101) (50+50+1) to make sense. Or just use text columns instead. See:

General Advice

The best solution depends on how you plan to deal with NULL values (and empty strings) exactly. I would probably not add a generated column, that’s typically more expensive and error prone overall than to concatenate the full name on the fly. Consider a view. Or a function encapsulating the exact concatenation logic.

Related:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement