是否可以使用 "Generated Columns" 在 PostgreSQL 中使用 "current_timestamp" 更新列(自动)?

Is it possible to update a column(automatically) with "current_timestamp" in PostgreSQL using "Generated Columns"?

是否可以在 PostgreSQL 中使用“Generated Columns”更新带有“current_timestamp”的列(自动),每当行更新时?

目前我正在使用触发器来更新审计字段last_update_date。但我打算切换到生成的列

ALTER TABLE test ADD COLUMN last_update_date timestamp without time zone 
GENERATED ALWAYS AS (current_timestamp) STORED;

更改列时出错

ERROR: generation expression is not immutable

不,由于错误中指定的原因,这行不通。

生成列中使用的函数必须始终return相同参数的相同值,也就是说,只依赖于当前数据库行。 current_timestamp显然不是那种

如果 PostgreSQL 确实允许在生成的列中使用此类函数,那么如果数据库从 pg_dump 还原,则列的值将会更改。

为此使用 BEFORE INSERT OR UPDATE 触发器。