Re: [EXTERNAL]: Re: UPSERT in Postgres

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Louis Tian <louis(dot)tian(at)aquamonix(dot)com(dot)au>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: [EXTERNAL]: Re: UPSERT in Postgres
Date: 2023-04-07 20:52:17
Message-ID: 9b903183-ba46-d802-59c3-c59af7793780@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 4/6/23 18:27, Louis Tian wrote:
> Hi Adrian,
>
> Thank you. I think this is a better approach than trigger-based
> solution, at least for my taste.
> That being said, it does require some logic to push to the client side
> (figuring out which required column value is missing and set it value to
> the existing one via reference of the table name).
> Still wish there would be UPSERT statement that can handle this and make
> dev experience better.

Another way to make the experience easier:

alter table person alter COLUMN name set default 'cat';

\d person
Table "public.person"
Column | Type | Collation | Nullable | Default
-----------+---------+-----------+----------+-------------
id | integer | | not null |
name | text | | not null | 'cat'::text
is_active | boolean | | |

select * from person;
id | name | is_active
----+------+-----------
0 | foo | NULL

insert into person(id, is_active) values (0, true) on conflict ("id")
do update set (id, is_active) = (excluded.id, excluded.is_active);
INSERT 0 1

select * from person;
id | name | is_active
----+------+-----------
0 | foo | t

>
> Cheers,
> Louis Tian

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Telium Technical Support 2023-04-07 22:27:27 pg_ctlcluster is not stopping cluster
Previous Message Adrian Klaver 2023-04-07 20:43:36 Re: [EXTERNAL]: Re: UPSERT in Postgres