Postgres UPSERT (INSERT or UPDATE) only if value is different

前端 未结 6 1560
你的背包
你的背包 2021-02-05 17:26

I\'m updating a Postgres 8.4 database (from C# code) and the basic task is simple enough: either UPDATE an existing row or INSERT a new one if one doesn\'t exist yet. Normally I

6条回答
  •  盖世英雄少女心
    2021-02-05 17:50

    Postgres is getting UPSERT support . It is currently in the tree since 8 May 2015 (commit):

    This feature is often referred to as upsert.

    This is implemented using a new infrastructure called "speculative insertion". It is an optimistic variant of regular insertion that first does a pre-check for existing tuples and then attempts an insert. If a violating tuple was inserted concurrently, the speculatively inserted tuple is deleted and a new attempt is made. If the pre-check finds a matching tuple the alternative DO NOTHING or DO UPDATE action is taken. If the insertion succeeds without detecting a conflict, the tuple is deemed inserted.

    A snapshot is available for download. It has not yet made a release.

提交回复
热议问题