PostgreSQL Generated Columns vs Triggers: When to Use Each for Derived Data

In PostgreSQL, derived column values can be maintained either through generated columns or triggers, and the right choice depends on what data the value needs to access. Generated columns work best when the value depends only on other columns in the same row using immutable functions, as the database enforces correctness and blocks manual overwrites. Triggers are necessary when the derived value requires data from other tables, the current timestamp, or non-immutable functions. Performance testing on PostgreSQL 18 showed that a row-level PL/pgSQL trigger added roughly 72% overhead to bulk inserts compared to a plain table, while a stored generated column added about 16% and a virtual generated column added none. Beyond performance, generated columns also improve schema clarity since their logic is visible directly in the table definition, unlike trigger-maintained columns which appear as ordinary writable columns.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in