3 ms·
Does anybody know whether PostgreSQL is going to implement UPSERT or MERGE support any time soon? I hate having to lock tables to emulate this.
by FooBarWidget 15y ago
Does anybody know whether PostgreSQL is going to implement UPSERT or MERGE support any time soon? I hate having to lock tables to emulate this.
- saurik 15y agoIt is not actually certain whether a standards-compliant implementation of MERGE would actually solve the UPSERT case (there have been some interesting mailing list discussions related to this, as that seemed to be an incorrect yet long-standing assumption of some people defining the roadmap). Regardless, you don't need a table lock to do it in any case. Doing so without such a lock is actually an example from the documentation: http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE http://www.postgresql.org/docs/current/static/plpgsql-contro... In essence, you just do the UPDATE, and if you ge back no affected rows, you perform an INSERT. If the INSERT fails because the row already exists, you do the UPDATE again.