I'm trying to create the following table in PostgreSQL 13:
CREATE TABLE cached (
text VARCHAR NOT NULL,
text_hash BYTEA GENERATED ALWAYS AS (sha256(convert_to(text, 'LATIN1'))) STORED PRIMARY KEY
);
However, I'm getting the following error:
generation expression is not immutable
I'm guessing that this is because convert_to is not immutable. How do I work around this? Is there a way of converting the text column to bytea in an immutable way?
Things I'd rather avoid:
- Casting to
text::bytea. It won't work correctly as explained here. - Using triggers.