Skip to content
Advertisement

Postgres on Conflict Update With Default Column Expression

In case a column has a default value expression

create table A (
...
b numeric default cos(54)
)

Is it possible for on conflict statement reset the column with the default expression without

  • copy-paste the expression it self
  • using default as a value while composing excluded record it self

I.e. just to refer the default exactly from the update statement. Something like

on conflict
update set b = default

or even

on conflict
update set b = case when some_condition then default else A.b end

Advertisement

Answer

Yes, you can use the DEFALT value in an upsert:

INSERT INTO t (foo,bar) VALUES (1,2)
ON CONFLICT (foo) DO UPDATE
SET bar = DEFAULT;

Demo: db<>fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement