当前位置: 首页 > 面试题库 >

如何在postgres中获取UPSERT操作的插入和更新行

鱼旺
2023-03-14
问题内容

我有这样的UPSERT操作:

INSERT INTO people (SELECT * FROM people_update)
  ON CONFLICT (name,surname)
    DO UPDATE SET age = EXCLUDED.age , street = EXCLUDED.street , city = EXCLUDED.city , postal = EXCLUDED.postal
    WHERE 
      (people.age,people.street,people.city,people.postal) IS DISTINCT FROM (EXCLUDED.age,EXCLUDED.street,EXCLUDED.city,EXCLUDED.postal)
RETURNING case when xmax::text::int > 0 then 'updated' else 'inserted' end,name,surname,age,street,city,postal;

(name,surname) 是一个复合主键,与“ people”表相比,“ people_update”表包含其他行和已更改的行。

我的问题是:有没有办法将查询的插入行和更新行作为返回行?

编辑:我通过添加一个RETURNING子句中途解决了问题,但我也想在我的返回值中获取旧值。


问题答案:

如果将布尔更新的列添加到people表中:

ALTER TABLE people ADD COLUMN updated bool DEFAULT FALSE;

那么您可以通过updated = TRUEDO UPDATE SET子句中进行设置来标识更新的行:

INSERT INTO people (SELECT * FROM people_update)
  ON CONFLICT (name,surname)
    DO UPDATE SET age = EXCLUDED.age , street = EXCLUDED.street , city = EXCLUDED.city 
        , postal = EXCLUDED.postal
        , updated = TRUE
    WHERE 
      (people.age,people.street,people.city,people.postal) IS DISTINCT FROM 
      (EXCLUDED.age,EXCLUDED.street,EXCLUDED.city,EXCLUDED.postal)
RETURNING *;

例如,

CREATE TABLE test.people (
    name text
    , surname text
    , age float
    , street text
    , city text
    , postal int
);
CREATE UNIQUE INDEX people_idx on people (name, surname);
ALTER TABLE people ADD COLUMN updated bool;
ALTER TABLE people ADD COLUMN prior_age float;
ALTER TABLE people ADD COLUMN prior_street text;
ALTER TABLE people ADD COLUMN prior_city text;
ALTER TABLE people ADD COLUMN prior_postal int;

INSERT INTO people (name, surname, age, street, city, postal) VALUES 
('Sancho', 'Panza', 414, '1 Manchego', 'Barcelona', 01605)
, ('Oliver', 'Twist', 182, '2 Stilton', 'London', 01837)
, ('Quasi', 'Modo', 188, $$3 Rue d'Arcole$$, 'Paris' , 01831 )
;

CREATE TABLE test.people_update (
    name text
    , surname text
    , age float
    , street text
    , city text
    , postal int
);

INSERT INTO people_update (name, surname, age, street, city, postal) VALUES 
('Sancho', 'Panza', 4140, '10 Idiazabal', 'Montserrat', 16050)
, ('Quasi', 'Modo', 1880, $$30 Champs Elys茅e$$ , 'Paris', 18310 )
, ('Pinocchio', 'Geppetto', 1380, '40 Nerbone', 'Florence', 18810)
;

INSERT INTO people (SELECT * FROM people_update)
  ON CONFLICT (name,surname)
    DO UPDATE SET 
        updated = TRUE
        , prior_age = (CASE WHEN people.age = EXCLUDED.age THEN NULL ELSE people.age END)
        , prior_street = (CASE WHEN people.street = EXCLUDED.street THEN NULL ELSE people.street END)
        , prior_city = (CASE WHEN people.city = EXCLUDED.city THEN NULL ELSE people.city END)
        , prior_postal = (CASE WHEN people.postal = EXCLUDED.postal THEN NULL ELSE people.postal END)
        , age = EXCLUDED.age 
        , street = EXCLUDED.street 
        , city = EXCLUDED.city 
        , postal = EXCLUDED.postal
    WHERE 
      (people.age,people.street,people.city,people.postal) IS DISTINCT FROM 
      (EXCLUDED.age,EXCLUDED.street,EXCLUDED.city,EXCLUDED.postal)
RETURNING *;

产量

| name       | surname  |  age | street           | city       | postal | updated | prior_age | prior_street   | prior_city | prior_postal |
|------------+----------+------+------------------+------------+--------+---------+-----------+----------------+------------+--------------|
| Sancho     | Panza    | 4140 | 10 Idiazabal     | Montserrat |  16050 | t       |       414 | 1 Manchego     | Barcelona  |         1605 |
| Quasi      | Modo     | 1880 | 30 Champs Elys茅e | Paris      |  18310 | t       |       188 | 3 Rue d'Arcole |            |         1831 |
| Pinocchio  | Geppetto | 1380 | 40 Nerbone       | Florence   |  18810 | f       |           |                |            |              |

updated列显示('Sancho', 'Panza')('Quasi', 'Modo')行已更新,并且 ('Pinocchio', 'Geppetto')是新插入的。



 类似资料:
  • 但现在我也需要知道: 插入了多少行 由于现有而更新了多少行 由于约束无法插入多少行 如果最后一行没有遵守约束,那么以前插入/更新的行是否会保留在数据库中?

  • 问题内容: UPSERT操作会更新表或在表中插入一行,这取决于表是否已经有与数据匹配的行: 由于Oracle没有特定的UPSERT语句,执行此操作的最佳方法是什么? 问题答案: MERGE(“老式方式”)的替代方法:

  • 和都属于类型或任何其他数字数据类型。是我的PK。 现在,我希望在表中执行更新查询,如果该行不存在,我希望插入该行。如果用户已经存在,我希望将点数增加1,否则插入用户ID,点数默认为1。 我知道我可以这样做: 但是,在我的情况下,更新操作比插入新行更频繁。假设每天有5000个查询,其中大约4500行是对现有行的操作。做相反的upsert将是更有利的,因为冲突将减少到500次,而不是4500次。我想先

  • 问题内容: MySQL有这样的东西: 据我所知,SQLite中不存在此功能,我想知道的是,是否有任何方法可以实现相同的效果而不必执行两个查询。另外,如果这不可能,那么您更喜欢什么: SELECT +(插入或更新) 或 UPDATE( 如果UPDATE失败,则 + INSERT ) 问题答案: 因为3.24.0 SQLite还支持upsert ,所以现在您可以简单地编写以下内容

  • 查看Postgres 9.4数据类型JSONB的留档,我不知道如何对JSONB列进行更新。 JSONB类型和函数的文档: http://www.postgresql.org/docs/9.4/static/functions-json.html http://www.postgresql.org/docs/9.4/static/datatype-json.html 举个例子,我有一个基本的表格结构

  • 问题内容: 在Slick 3.0中执行批量insertOrUpdate的正确方法是什么? 我正在使用适当的查询的MySQL MySQL批量插入或更新 这是我当前的代码,它很慢:-( 我正在寻找的是等价的 问题答案: 您可以通过多种方法使此代码更快(每个代码都 应该 比前面的代码更快,但是它的习惯用法越来越少): 运行而不是if on slick-pg 0.16.1+ 一次运行所有DBIO事件,而不