欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PostgreSQL的upsert实例操作(insert on conflict do)

程序员文章站 2022-04-02 10:13:27
建表语句:drop table if exists "goods";create table "goods" ( "store_cd" int4 not null, "good_cd" varchar...

建表语句:

drop table if exists "goods";
create table "goods" (
 "store_cd" int4 not null,
 "good_cd" varchar(50) collate "pg_catalog"."default" not null,
 "name" varchar(255) collate "pg_catalog"."default"
);
 
insert into "goods" values (101, '1', '张三');
insert into "goods" values (102, '2', '李四');
insert into "goods" values (103, '3', '王五');
 
alter table "goods" add constraint "pr_cd_key" primary key ("store_cd", "good_cd");

表数据:

PostgreSQL的upsert实例操作(insert on conflict do)

数据存在则更新数据,不存在则插入数据

insert into goods values ( 104, '4', '赵六' ) 
on conflict on constraint pr_key_cd do
update 
 set name = '更新' 
where
 goods.store_cd = '104' 
 and goods.good_cd = '4'

pr_key_cd为必须为唯一主键,也可以用下面写法(注意:必须保证筛选出数据唯一)

insert into goods values ( 104, '4', '赵六' ) 
on conflict ( store_cd, good_cd ) do
update 
 set name = '更新' 
where
 goods.store_cd = '104' 
 and goods.good_cd = '4'

上面的两种的写法,是先执行insert如果主键冲突则执行update,没有冲突就执行insert了。要是想先执行update语句呢?

update更新失败执行insert,更新成功则执行update。

with table1 as ( update goods set name = '更新' where store_cd = '104' and good_cd = '4' returning * ) 
insert into goods select 104, '4', '赵六' 
where not exists ( select 1 from table1 where store_cd = '104' and good_cd = '4' )

补充:postgresql插入或更新操作upsert

幂等性的一个要求是多次操作的结果一致。对于update操作,多次直接的结果都是最后update的值,是满足需求的。但对于insert,如果已经插入,第二次会报错,duplicate error, 主键重复或者unique key duplicate。所以需要做一下处理。

最简单的就是,try-catch,当报错的时候,调用update去更新,或者策略更简单点,直接返回就行,不需要更新,以第一条为准。

postgresql从9.5之后就提供了原子的upsert语法: 不存在则插入,发生冲突可以update。

inert语法

官方文档:

[ with [ recursive ] with_query [, ...] ]
insert into table_name [ as alias ] [ ( column_name [, ...] ) ]
  [ overriding { system | user} value ]
  { default values | values ( { expression | default } [, ...] ) [, ...] | query }
  [ on conflict [ conflict_target ] conflict_action ]
  [ returning * | output_expression [ [ as ] output_name ] [, ...] ]
where conflict_target can be one of:
  ( { index_column_name | ( index_expression ) } [ collate collation ] [ opclass ] [, ...] ) [ where index_predicate ]
  on constraint constraint_name
and conflict_action is one of:
  do nothing
  do update set { column_name = { expression | default } |
          ( column_name [, ...] ) = [ row ] ( { expression | default } [, ...] ) |
          ( column_name [, ...] ) = ( sub-select )
         } [, ...]
       [ where condition ]

index_column_name

the name of a table_name column. used to infer arbiter indexes. follows create index format. select privilege on index_column_name is required.

index_expression

similar to index_column_name, but used to infer expressions on table_name columns appearing within index definitions (not simple columns). follows create index format. select privilege on any column appearing within index_expression is required.

使用示例

创建表

create table "test"."upsert_test" (
 "id" int4 not null,
 "name" varchar(255) collate "pg_catalog"."default"
)
;

当主键id冲突时,更新其他字段

insert into test.upsert_test(id, "name")
  values(1, 'm'),(2, 'n'),(4, 'c')
  on conflict(id) do update
  set "name" = excluded.name;

did 冲突的主键

excluded 代指要插入的记录

当主键或者unique key发生冲突时,什么都不做

insert into test.upsert_test(id, "name")
values(1, 'm'),(2, 'n'),(4, 'c')
on conflict(id) do nothing;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

相关标签: PostgreSQL upsert