免责声明:理论问题.
这里有几个问题,询问如何在PostgreSQL upsert语句中区分插入和更新的行.
这是一个简单的例子:
nd@postgres=# create table t(i int primary key, x int);
nd@postgres=# insert into t values(1,1);
nd@postgres=# insert into t values(1,11),(2,22)
on conflict(i) do update set x = excluded.i*11
returning *, xmin, xmax;
????????????????????????
? i ? x ? xmin ? xmax ?
????????????????????????
? 1 ? 11 ? 7696 ? 7696 ?
? 2 ? 22 ? 7696 ? 0 ?
????????????????????????
Run Code Online (Sandbox Code Playgroud)
所以,xmax> 0(或xmax= xmin) - 行已更新; xmax= 0 - …
我得到了一个典型的例子:
INSERT INTO user_logins (username, logins)
VALUES ('Naomi',1),('James',1)
ON CONFLICT (username)
DO UPDATE SET logins = user_logins.logins + EXCLUDED.logins;
Run Code Online (Sandbox Code Playgroud)
但现在我还需要知道: