Insert, on duplicate update in PostgreSQL?

前端 未结 16 2309
别那么骄傲
别那么骄傲 2020-11-21 04:52

Several months ago I learned from an answer on Stack Overflow how to perform multiple updates at once in MySQL using the following syntax:

INSERT INTO table          


        
16条回答
  •  故里飘歌
    2020-11-21 05:48

    PostgreSQL since version 9.5 has UPSERT syntax, with ON CONFLICT clause. with the following syntax (similar to MySQL)

    INSERT INTO the_table (id, column_1, column_2) 
    VALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')
    ON CONFLICT (id) DO UPDATE 
      SET column_1 = excluded.column_1, 
          column_2 = excluded.column_2;
    

    Searching postgresql's email group archives for "upsert" leads to finding an example of doing what you possibly want to do, in the manual:

    Example 38-2. Exceptions with UPDATE/INSERT

    This example uses exception handling to perform either UPDATE or INSERT, as appropriate:

    CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
    
    CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
    $$
    BEGIN
        LOOP
            -- first try to update the key
            -- note that "a" must be unique
            UPDATE db SET b = data WHERE a = key;
            IF found THEN
                RETURN;
            END IF;
            -- not there, so try to insert the key
            -- if someone else inserts the same key concurrently,
            -- we could get a unique-key failure
            BEGIN
                INSERT INTO db(a,b) VALUES (key, data);
                RETURN;
            EXCEPTION WHEN unique_violation THEN
                -- do nothing, and loop to try the UPDATE again
            END;
        END LOOP;
    END;
    $$
    LANGUAGE plpgsql;
    
    SELECT merge_db(1, 'david');
    SELECT merge_db(1, 'dennis');
    

    There's possibly an example of how to do this in bulk, using CTEs in 9.1 and above, in the hackers mailing list:

    WITH foos AS (SELECT (UNNEST(%foo[])).*)
    updated as (UPDATE foo SET foo.a = foos.a ... RETURNING foo.id)
    INSERT INTO foo SELECT foos.* FROM foos LEFT JOIN updated USING(id)
    WHERE updated.id IS NULL;
    

    See a_horse_with_no_name's answer for a clearer example.

提交回复
热议问题