当前位置:网站首页>Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
2022-08-04 03:35:00 【mingjie73】
1 语法介绍
insert on conflict语法实现了upsert的功能,That is, a primary key conflict occurs on the insert、or a unique constraint violation,执行on conflict后面的语句,将insert变成update或do nothing避免报错.
语法手册:https://www.postgresql.org/docs/current/sql-insert.html
测试用例:
drop table decoding_test;
CREATE TABLE decoding_test(x integer primary key, y text);
postgres=# select * from decoding_test;
x | y
----+---
12 | 9
postgres=# INSERT INTO decoding_test(x,y) values(12,9) on conflict (x) do nothing;
INSERT 0 1
postgres=# select * from decoding_test;
x | y
----+---
12 | 9
-- No primary key conflict was reported,As a result, the insertion has no effect.
postgres=# INSERT INTO decoding_test(x,y) values(12,9) on conflict (x) do nothing;
INSERT 0 0
postgres=# select * from decoding_test;
x | y
----+---
12 | 9
(1 row)
postgres=# INSERT INTO decoding_test(x,y) values(101,20) on conflict (x) do update set y=EXCLUDED.y;
INSERT 0 1
postgres=#
postgres=# select * from decoding_test;
x | y
-----+----
12 | 9
101 | 20
-- A primary key violation occurred while inserting,执行后面的update语句,将y更新为400,EXCLUDEDIndicates that this row of data is ready to be newly inserted.
postgres=# INSERT INTO decoding_test(x,y) values(101,400) on conflict (x) do update set y=EXCLUDED.y;
INSERT 0 1
postgres=# select * from decoding_test;
x | y
-----+-----
12 | 9
101 | 400
(2 rows)
2 Kernel execution flow
注意:后面提到的
speculative insert等价与insert on conflict语法.
2.1 Observed from the execution flowspeculative insert
执行流程:
- spec insertThe execution flow and commoninsert是分开的,走两个分支.
- specThe special thing is that there is a retry mechanism,即:
- In the first check if no unique key conflict is found,正常是可以直接insert的.
- But due to no lock check,Possibly in realinsertA unique key violation occurred again(Checked before,other concurrencyinsertA piece of conflicting data)
- 那么这时xlogOne has already been successfulinsert了,Need to add another onedelete(The fourth step in the figure conflict occurs).

2.2 Observed from a log perspectivespeculative insert
INSERT INTO decoding_test(x,y) values(12,9) on conflict (x) do nothing;- 情况一:插入成功
- heap_insert,生成XLOG_HEAP_INSERT日志.
- heap_finish_speculative,生成XLOG_HEAP_CONFIRM日志.
- 情况二:插入失败
- 不生成日志
- 情况三:There are no conflicts yet when inserting,But other processes insert conflicting rows concurrently(Concurrency conflict locations are analyzed later)
- heap_insert,生成XLOG_HEAP_INSERT日志.
- heap_abort_speculative,生成XLOG_HEAP_DELETE日志.
- 情况一:插入成功
INSERT INTO decoding_test(x,y) values(20,9) on conflict (x) do update set y=100;- 插入成功
- heap_insert,生成XLOG_HEAP_INSERT日志.
- heap_finish_speculative,生成XLOG_HEAP_CONFIRM日志.
- 更新成功:转换为update语句执行
- log_heap_update,生成XLOG_HEAP_HOT_UPDATE日志.
- There are no conflicts yet when inserting,But other processes insert conflicting rows concurrently(Concurrency conflict locations are analyzed later)
- heap_insert,生成XLOG_HEAP_INSERT日志.
- heap_abort_speculative,生成XLOG_HEAP_DELETE日志.
- 插入成功
So it may be seen from the log3种情况:
情况一: 第一条XLOG_HEAP_INSERT 第二条XLOG_HEAP_CONFIRM
情况二: 第一条XLOG_HEAP_INSERT 第二条XLOG_HEAP_DELETE
情况三: 第一条XLOG_HEAP_HOT_UPDATE
The next article will continue to introduce these types of logs after they are parsed by logical replication.
边栏推荐
- ingress 待完善
- typescript type 和 interface 的区别
- if,case,for,while
- 学会iframe并用其解决跨域问题
- KingbaseES数据库启动失败,报“内存段超过可用内存”
- How to read the resources files in the directory path?
- How to automatically export or capture abnormal login ip and logs in elastic to the database?
- 【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
- 哎,又跟HR在小群吵了一架!
- This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
猜你喜欢
随机推荐
keytool命令
2022杭电多校联赛第五场 题解
Introduction to the memory model of the JVM
Asynchronous programming solution Generator generator function, iterator iterator, async/await, Promise
系统太多,多账号互通如何实现?
基本表单验证流程
RSS订阅微信公众号初探-feed43
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
高效IO模型
目标检测-中篇
JVM内存和垃圾回收-07.堆
机器学习之视频学习【更新】
【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
什么是数字孪生智慧城市应用场景
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
基于Qt的目录统计QDirStat
小程序+新零售,玩转行业新玩法!
The general SQL injection flow (sample attached)
返回字符串中的最大回文数
自定义通用分页标签02









![[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this](/img/d0/6ac51d0d51c907ed0e1578e038fffd.jpg)