Let's assume a table t:
create table if not exists t(
a integer primary key generated by default as identity,
b integer,
c text
);
and simple insert insert into t (b, c) values (2, 'abc');
Is there any option how to rewrite this insert and as a result in column b will be coalesce(b, a)?
So when b is null, b equals to generated column a.
I know there is an option to use RETURNING keyword and update or to use before insert trigger.
insert into t (b, c) values (coalesce(null, a), 'abc'); HINT: There is a column named "a" in table "t", but it cannot be referenced from this part of the query.