简体   繁体   中英

How to use batch in upsert data in postgres sql?

I have written an upsert query in postgres which upsert the data in to the table. Here I am trying to upsert ~ 5 to 10 million of rows.

INSERT INTO table_1 (col1, col2, col3)                      
(
    SELECT temp.col1 :: varchar, temp.col2 :: varchar, temp.col3 :: timestamp without time zone 
    
    FROM 
         (
            ('1', '1', '2021-03-02T09:16:54.358258'::timestamp),
            ('2', '2', '2021-03-02T09:16:54.358258'::timestamp)
         ) as temp

         (col1, col2, col3)
) ON CONFLICT (col1, col2) DO UPDATE SET col1 = EXCLUDED.col1, col2 = EXCLUDED.col2, col3 = EXCLUDED.col3;

The query is working as expected.

But I want to use batch in the query.

How to use batch in the same query?

I don't exactly understand "batch" in that context. But I think that the best thing you can do I to use COPY to a temp table and then use that to make INSERT... ON CONFLICT statement. You can see example here: https://stackoverflow.com/a/49836011/1958544

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM