这个插件安全吗?

Is this insert safe?

我有一个项目,我必须定期从远程源插入一堆记录(每 10 分钟约 5k 条记录)。我有一个 staging table 与最终目标 table 具有相同的结构(没有主键,id 是 varchar 而不是唯一的),以确保不会有任何重复,我正在使用下一个 sql 命令:

insert into g_his 
select * 
from tmp_his h 
where not exists (select id, times 
                  from g_his g 
                  where g.id = h.id 
                    and g.times = h.times);

完成后 tmp_his table 被截断。

这是否可能在 g_his table 上获得隐式锁定并阻止任何其他插入?或者它可以是一个沉重的负担?或者有更好的主意吗?

数据库是 Postgres 9.6

g_his(id, times) 上创建唯一约束并使用

INSERT INTO g_his 
   SELECT * 
   FROM tmp_his h
ON CONFLICT (id, times) DO NOTHING;