当前位置:网站首页>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.
边栏推荐
- MRS: Alluxio的使用介绍
- pnpm 是凭什么对 npm 和 yarn 降维打击的
- C语言--环形缓存区
- 系统太多,多账号互通如何实现?
- 异步编程解决方案 Generator生成器函数、iterator迭代器、async/await、Promise
- 创新互融|华秋赋能助力OpenHarmony生态硬件开发落地
- 【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
- Brush esp8266-01 s firmware steps
- new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
- Shell 函数
猜你喜欢
[Playwright Test Tutorial] 5 minutes to get started
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
2022年最新海南建筑八大员(材料员)模拟考试试题及答案
The general SQL injection flow (sample attached)
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
外卖店优先级
一个属于程序员的七夕节!
数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
案例 | 重庆银行流动数据安全挑战及应对实践
随机推荐
Shell 函数
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
db2中kettle报错 Field [XXX] is required and couldn‘t be found 解决方法
Based on the statistical QDirStat Qt directory
MCU C language -> usage, and meaning
The general SQL injection flow (sample attached)
基本表单验证流程
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
外卖店优先级
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
Sfdp 超级表单开发平台 V6.0.5 正式发布
DIY电工维修如何拆卸和安装开关面板插座
How to drop all tables under database in MySQL
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
马尔可夫链
There are too many systems, how to realize multi-account interworking?
打造一份优雅的简历
MySQL query optimization and tuning
golang中的unsafe.Pointer,指针,引用