PostgreSQL merge insert(upsert/insert into on conflict) 如何区分数据是INSERT还是UPDATE

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: postgresql merge insert

2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>


阿里云采购季(云主机223元/3年)活动入口:请点击进入>>>,


阿里云学生服务器(9.5元/月)购买入口:请点击进入>>>,

背景
使用insert into on conflict update语法,可以支持UPSERT的功能,但是到底这条SQL是插入的还是更新的呢?如何判断

通过xmax字段的值是否不为0,可以判断,如果是UPDATE,XMAX里面会填充更新事务号。

注意直接用UPDATE语句更新的话,XMAX会写入0,因为是新版本,而老版本上XMAX会填入更新事务号。

例子
1 insert on conflict
postgres=# create table t(id int primary key, info text, crt_time timestamp);
CREATE TABLE
postgres=# insert into t values (1,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time returning xmax;

xmax

0  

(1 row)

INSERT 0 1
postgres=# insert into t values (1,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time returning xmax;

xmax

160369640
(1 row)

INSERT 0 1
postgres=# select xmin,xmax,* from t;

xmin xmax id info crt_time
160369640 160369640 1 test 2018-10-17 12:09:38.760926

(1 row)

postgres=# insert into t values (1,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time returning xmax;

xmax

160369641
(1 row)

INSERT 0 1
postgres=# select xmin,xmax,* from t;

xmin xmax id info crt_time
160369641 160369641 1 test 2018-10-17 12:10:11.738691

(1 row)

postgres=# insert into t values (2,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time returning xmax;

xmax

0  

(1 row)

INSERT 0 1
postgres=# select xmin,xmax,* from t;

xmin xmax id info crt_time
160369641 160369641 1 test 2018-10-17 12:10:11.738691
160369642 0 2 test 2018-10-17 12:10:24.758745

(2 rows)

postgres=# select ctid,xmin,xmax,* from t;

ctid xmin xmax id info crt_time
(0,3) 160369641 160369641 1 test 2018-10-17 12:10:11.738691
(0,4) 160369642 0 2 test 2018-10-17 12:10:24.758745

(2 rows)

postgres=# insert into t values (2,'test',now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time returning ctid,xmin,xmax,*;

ctid xmin xmax id info crt_time
(0,5) 160369643 160369643 2 test 2018-10-17 12:10:45.951351

(1 row)

INSERT 0 1
postgres=# select ctid,xmin,xmax,* from t;

ctid xmin xmax id info crt_time
(0,3) 160369641 160369641 1 test 2018-10-17 12:10:11.738691
(0,5) 160369643 160369643 2 test 2018-10-17 12:10:45.951351

(2 rows)
2 直接update
postgres=# update t set info='a' returning xmin,xmax,ctid,*;

xmin xmax ctid id info crt_time
160369644 0 (0,6) 1 a 2018-10-17 12:10:11.738691
160369644 0 (0,7) 2 a 2018-10-17 12:10:45.951351

(2 rows)

UPDATE 2
3 update 回滚
postgres=# begin;
BEGIN
postgres=# update t set info='a' returning xmin,xmax,ctid,*;

xmin xmax ctid id info crt_time
160369645 0 (0,8) 1 a 2018-10-17 12:10:11.738691
160369645 0 (0,9) 2 a 2018-10-17 12:10:45.951351

(2 rows)

UPDATE 2
postgres=# rollback;
ROLLBACK
postgres=# select ctid,xmin,xmax,* from t;

ctid xmin xmax id info crt_time
(0,6) 160369644 160369645 1 a 2018-10-17 12:10:11.738691
(0,7) 160369644 160369645 2 a 2018-10-17 12:10:45.951351

(2 rows)
4 delete 回滚
postgres=# begin;
BEGIN
postgres=# delete from t returning ctid,xmin,xmax,*;

ctid xmin xmax id info crt_time
(0,6) 160369644 160369646 1 a 2018-10-17 12:10:11.738691
(0,7) 160369644 160369646 2 a 2018-10-17 12:10:45.951351

(2 rows)

DELETE 2
postgres=# rollback;
ROLLBACK
postgres=# select ctid,xmin,xmax,* from t;

ctid xmin xmax id info crt_time
(0,6) 160369644 160369646 1 a 2018-10-17 12:10:11.738691
(0,7) 160369644 160369646 2 a 2018-10-17 12:10:45.951351

(2 rows)
小结
1、insert into on conflict do update,返回xmax不等于0,表示update,等于0表示insert。

2、直接update,并提交,提交的记录上xmax为0。

3、直接update,并回滚,老版本上的XMAX不为0,表示更新该行的事务号。

4、直接DELETE,并回滚,老版本上的XMAX不为0,表示删除该行的事务号。

ctid表示行号, xmin表示INSERT该记录的事务号,xmax表示删除该记录(update实际上是删除老版本新增新版本,所以老版本上xmax有值)的事务号。

参考
《Greenplum & PostgreSQL UPSERT udf 实现 - 2 batch批量模式》

《Greenplum & PostgreSQL UPSERT udf 实现 - 1 单行模式》

《PostgreSQL 多重含义数组检索与条件过滤 (标签1:属性, 标签n:属性) - 包括UPSERT操作如何修改数组、追加数组元素》

《HTAP数据库 PostgreSQL 场景与性能测试之 22 - (OLTP) merge insert|upsert|insert on conflict|合并写入》

《PostgreSQL upsert功能(insert on conflict do)的用法》

《PostgreSQL 如何实现upsert与新旧数据自动分离》

《[转载]postgresql 9.5版本之前实现upsert功能》

《upsert - PostgreSQL 9.4 pending patch : INSERT...ON DUPLICATE KEY IGNORE》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
4天前
|
关系型数据库 数据库
关系型数据库的数据完整性
关系型数据库通过一系列机制和技术手段来确保数据的完整性,从而为用户提供准确、一致和可靠的数据服务。这些措施对于保障数据的质量、满足用户需求以及维护系统的稳定运行具有重要意义。
22 5
|
4天前
|
存储 关系型数据库 分布式数据库
PolarDB常见问题之PolarDB冷存数据到OSS之后恢复失败如何解决
PolarDB是阿里云推出的下一代关系型数据库,具有高性能、高可用性和弹性伸缩能力,适用于大规模数据处理场景。本汇总囊括了PolarDB使用中用户可能遭遇的一系列常见问题及解答,旨在为数据库管理员和开发者提供全面的问题指导,确保数据库平稳运行和优化使用体验。
|
4天前
|
SQL Oracle 关系型数据库
关系型数据库根据某些条件修改数据
SQL的`UPDATE`语句用于根据条件修改关系型数据库(如MySQL、PostgreSQL、Oracle)中的数据。基本语法是:`UPDATE 表名 SET 列名=新值 WHERE 条件`。例如,要将`students`表中名字为"John Doe"的学生年龄改为25,可以使用`UPDATE students SET age = 25 WHERE name = 'John Doe'`。执行`UPDATE`前需备份数据,先在测试环境验证,并考虑事务处理以确保数据安全。注意优化表结构和使用索引来提升性能。
17 3
|
4天前
|
关系型数据库 数据库 数据安全/隐私保护
关系型数据库的数据完整性保障
【5月更文挑战第9天】关系型数据库的数据完整性保障
8 1
|
3天前
|
存储 SQL Oracle
关系型数据库文件方式存储DATA FILE(数据文件)
【5月更文挑战第11天】关系型数据库文件方式存储DATA FILE(数据文件)
14 3
|
3天前
|
监控 安全 关系型数据库
关系型数据库数据完整性保障
【5月更文挑战第10天】关系型数据库数据完整性保障
15 4
|
4天前
|
关系型数据库 MySQL 分布式数据库
如何恢复PolarDB数据到特定时间点?
【5月更文挑战第13天】如何恢复PolarDB数据到特定时间点?
8 0
|
4天前
|
关系型数据库 MySQL 分布式数据库
如何备份PolarDB数据?
【5月更文挑战第13天】如何备份PolarDB数据?
8 0
|
4天前
|
关系型数据库 MySQL 分布式数据库
如何将数据从MySQL迁移到PolarDB?
【5月更文挑战第13天】如何将数据从MySQL迁移到PolarDB?
19 0
|
4天前
|
关系型数据库 分布式数据库 数据库
「活动预告」PolarDB走进青岛,邀请您一起畅游琴岛山海春韵,共话数据生态创新
由PolarDB开源社区、海信聚好看、PostgreSQL中文社区共同发起的《开源数据库沙龙》,将于青岛举办。
http://www.vxiaotou.com