Immutable field/cell in Postgres
-
12-03-2021 - |
Вопрос
Is there a way to create an immutable field/cell in a Postgres record? AKA, only allow the field to be written once, perhaps just on insert?
Use case: I have a "uuid" field for a record, and it should never ever be changed (same with the PK as well).
Решение
There are several techniques for that:
Using column permissions, grant
UPDATE
only on the other columns.Create an
UPDATE
trigger that throws an exception whenOLD.id <> NEW.id
.
Note that all these measures can never deny the table owner access, since the owner can always change permissionsand drop triggers.
Не связан с dba.stackexchange