当前位置:网站首页>Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
2022-08-04 03:30:00 【mingjie73】
1 语法介绍
insert on conflict语法实现了upsert的功能,即在插入发生主键冲突、或唯一约束冲突时,执行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
-- 没有报主键冲突,结果上看插入没有效果。
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
-- 插入时发生主键冲突,执行后面的update语句,将y更新为400,EXCLUDED表示准备要新插入的这一行数据。
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 内核执行流程
注意:后面提到的
speculative insert等价与insert on conflict语法。
2.1 从执行流程观察speculative insert
执行流程:
- spec insert的执行流程和普通insert是分开的,走两个分支。
- spec比较特殊的就是有重试机制,即:
- 在第一次检查如果没发现有唯一键冲突,正常是可以直接insert的。
- 但由于无锁检查,可能在真正insert时又发生了唯一键冲突(前面检查完了,其他并发insert一条冲突数据)
- 那么这时xlog中已经有一条成功的insert了,需要再后面加一条delete(图中第四步冲突发生了)。

2.2 从日志角度观察speculative 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日志。
- 情况二:插入失败
- 不生成日志
- 情况三:插入时还没有冲突,但其他进程并发插入冲突行(并发冲突位置在后面分析)
- 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日志。
- 插入时还没有冲突,但其他进程并发插入冲突行(并发冲突位置在后面分析)
- heap_insert,生成XLOG_HEAP_INSERT日志。
- heap_abort_speculative,生成XLOG_HEAP_DELETE日志。
- 插入成功
所以从日志中可能看到3种情况:
情况一: 第一条XLOG_HEAP_INSERT 第二条XLOG_HEAP_CONFIRM
情况二: 第一条XLOG_HEAP_INSERT 第二条XLOG_HEAP_DELETE
情况三: 第一条XLOG_HEAP_HOT_UPDATE
下一篇继续介绍这几种日志被逻辑复制解析后的情况。
边栏推荐
猜你喜欢

Simple record of Flink principle flow chart

y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)

Brush esp8266-01 s firmware steps

How to drop all tables under database in MySQL

为什么用Selenium做自动化测试

自制蓝牙手机app控制stm8/stm32/C51板载LED

数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书

Development of Taurus. MVC WebAPI introductory tutorial 1: download environment configuration and operation framework (including series directory).
SQL注入中 #、 --+、 --%20、 %23是什么意思?

C language -- ring buffer
随机推荐
Basic form validation process
FFmpeg —— 录制麦克风声音(附源码)
自制蓝牙手机app控制stm8/stm32/C51板载LED
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
STM8S105K4T6------Serial port sending and receiving
uni-app 从零开始-基础模版(一)
sql注入一般流程(附例题)
缓存穿透、缓存击穿、缓存雪崩以及解决方案
查看mysql死锁语法
How many ways do you know about communication between multiple threads?
STM8S-----option byte
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
[Original] Start the XPS/OXPS reader that comes with Windows 10
自定义通用分页标签02
如何读取 resources 目录下的文件路径?
DIY电工维修如何拆卸和安装开关面板插座
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
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
if,case,for,while
typescript type 和 interface 的区别