Schowalter Space πŸš€

How to UPSERT MERGE INSERT ON DUPLICATE UPDATE in PostgreSQL

February 16, 2025

πŸ“‚ Categories: Postgresql
How to UPSERT MERGE INSERT  ON DUPLICATE UPDATE in PostgreSQL

Efficaciously managing information is important for immoderate exertion, and a center facet of this includes updating current information oregon inserting fresh ones once they don’t be. This cognition, generally recognized arsenic UPSERT (Replace + INSERT), is a cornerstone of businesslike information manipulation. Successful PostgreSQL, respective methods execute this, together with the versatile MERGE message (launched successful PostgreSQL 15), the classical INSERT ... Connected Struggle Bash Replace/Thing, and the INSERT ... RETURNING mixed with consequent updates. This article volition delve into these strategies, offering broad examples and champion practices for implementing UPSERT performance successful your PostgreSQL database. Mastering these strategies volition streamline your information dealing with processes and importantly heighten your exertion’s show.

Utilizing the MERGE Message (PostgreSQL 15 and future)

The MERGE message supplies a concise and almighty manner to execute UPSERT operations. It permits you to specify antithetic actions primarily based connected whether or not a matching line exists successful the mark array. This methodology simplifies analyzable conditional updates and inserts, enhancing codification readability and maintainability.

Present’s a basal illustration:

MERGE INTO customers Arsenic mark Utilizing (Choice 'john.doe@illustration.com' Arsenic e-mail, 'John Doe' Arsenic sanction) Arsenic origin Connected mark.e-mail = origin.electronic mail Once MATCHED Past Replace Fit sanction = origin.sanction Once NOT MATCHED Past INSERT (e mail, sanction) VALUES (origin.e-mail, origin.sanction); 

This message makes an attempt to lucifer the electronic mail from the origin with the mark array. If a lucifer is recovered, the sanction is up to date; other, a fresh line is inserted.

Leveraging INSERT … Connected Struggle Bash Replace

For PostgreSQL variations anterior to 15, INSERT ... Connected Struggle Bash Replace is the modular attack for UPSERTs. This bid makes an attempt to insert a line and, if a constraint usurpation happens (usually a alone cardinal struggle), performs an replace alternatively.

Illustration:

INSERT INTO customers (e mail, sanction) VALUES ('jane.doe@illustration.com', 'Jane Doe') Connected Struggle (electronic mail) Bash Replace Fit sanction = EXCLUDED.sanction; 

Present, if a person with the fixed e mail already exists, the sanction tract is up to date with the fresh worth. The EXCLUDED key phrase refers to the line that would person been inserted.

Implementing UPSERT with INSERT … RETURNING and Updates

Different technique entails utilizing INSERT ... RETURNING to effort an insert and past performing a abstracted replace if the insert fails owed to a alone constraint usurpation. This attack presents much flexibility for analyzable replace logic however tin beryllium little performant than Connected Struggle Bash Replace.

Bash $$ Statesman INSERT INTO customers (e mail, sanction) VALUES ('peter.cookware@illustration.com', 'Peter Cookware') RETURNING id; Objection Once unique_violation Past Replace customers Fit sanction = 'Peter Cookware Jr.' Wherever electronic mail = 'peter.cookware@illustration.com'; Extremity $$; 

This illustration showcases dealing with exceptions to find whether or not the insert succeeded oregon an replace is essential.

Selecting the Correct UPSERT Methodology

The optimum methodology relies upon connected your circumstantial wants and PostgreSQL interpretation. MERGE is perfect for analyzable situations and improved readability successful newer variations. INSERT ... Connected Struggle supplies a concise resolution for less complicated instances, piece INSERT ... RETURNING gives flexibility once analyzable replace logic is required. See show implications and the complexity of your queries once making your prime. Usage PostgreSQL’s Explicate Analyse bid to benchmark antithetic strategies towards your information and workload.

  • Show: Connected Struggle is mostly sooner than INSERT ... RETURNING.
  • Readability: MERGE provides the about readable syntax for analyzable situations.
  1. Analyse your PostgreSQL interpretation.
  2. Find the complexity of your UPSERT logic.
  3. Take the about due methodology (MERGE, Connected Struggle, oregon INSERT ... RETURNING).
  4. Trial show utilizing Explicate Analyse.

Infographic Placeholder: Ocular examination of the 3 UPSERT strategies.

Champion Practices and Concerns

Careless of the chosen methodology, guarantee appropriate indexing connected the columns active successful the Wherever clause oregon Connected Struggle constraint for optimum show. See possible contest circumstances successful concurrent environments and instrumentality due locking mechanisms once essential. Trial completely to guarantee information integrity and anticipated behaviour nether antithetic eventualities.

FAQ

Q: What is the quality betwixt Connected Struggle Bash Replace and Connected Struggle Bash Thing?

A: Bash Replace performs an replace if a struggle happens, piece Bash Thing merely skips the insert with out immoderate additional act.

Knowing the nuances of all UPSERT methodology successful PostgreSQL empowers you to take the champion attack for your circumstantial exertion wants. By leveraging these strategies and pursuing the offered champion practices, you tin optimize information direction operations, better show, and guarantee information integrity. Research the sources beneath for additional studying and delve deeper into precocious UPSERT methods. Contemporary database purposes request businesslike information dealing with, and mastering PostgreSQL’s UPSERT performance is a important measure in direction of attaining that end. Instrumentality these methods present and witnesser a noticeable enhancement successful your database interactions.

Question & Answer :
A precise often requested motion present is however to bash an upsert, which is what MySQL calls INSERT ... Connected DUPLICATE Replace and the modular helps arsenic portion of the MERGE cognition.

Fixed that PostgreSQL doesn’t activity it straight (earlier pg 9.5), however bash you bash this? See the pursuing:

Make Array testtable ( id integer Capital Cardinal, somedata matter NOT NULL ); INSERT INTO testtable (id, somedata) VALUES (1, 'fred'), (2, 'bob'); 

Present ideate that you privation to “upsert” the tuples (2, 'Joe'), (three, 'Alan'), truthful the fresh array contents would beryllium:

(1, 'fred'), (2, 'Joe'), -- Modified worth of present tuple (three, 'Alan') -- Added fresh tuple 

That’s what group are speaking astir once discussing an upsert. Crucially, immoderate attack essential beryllium harmless successful the beingness of aggregate transactions running connected the aforesaid array - both by utilizing express locking, oregon other defending in opposition to the ensuing contest circumstances.

This subject is mentioned extensively astatine Insert, connected duplicate replace successful PostgreSQL?, however that’s astir alternate options to the MySQL syntax, and it’s grown a just spot of unrelated item complete clip. I’m running connected definitive solutions.

These strategies are besides utile for “insert if not exists, other bash thing”, i.e. “insert … connected duplicate cardinal disregard”.

9.5 and newer:

PostgreSQL 9.5 and newer activity INSERT ... Connected Struggle (cardinal) Bash Replace (and Connected Struggle (cardinal) Bash Thing), i.e. upsert.

Examination with Connected DUPLICATE Cardinal Replace.

Speedy mentation.

For utilization seat the guide - particularly the conflict_action clause successful the syntax diagram, and the explanatory matter.

Dissimilar the options for 9.four and older that are fixed beneath, this characteristic plant with aggregate conflicting rows and it doesn’t necessitate unique locking oregon a retry loop.

The perpetrate including the characteristic is present and the treatment about its improvement is present.


If you’re connected 9.5 and don’t demand to beryllium backward-suitable you tin halt speechmaking present.


9.four and older:

PostgreSQL doesn’t person immoderate constructed-successful UPSERT (oregon MERGE) installation, and doing it effectively successful the expression of concurrent usage is precise hard.

This article discusses the job successful utile item.

Successful broad you essential take betwixt 2 choices:

  • Idiosyncratic insert/replace operations successful a retry loop; oregon
  • Locking the array and doing batch merge

Idiosyncratic line retry loop

Utilizing idiosyncratic line upserts successful a retry loop is the tenable action if you privation galore connections concurrently making an attempt to execute inserts.

The PostgreSQL documentation incorporates a utile process that’ll fto you bash this successful a loop wrong the database. It guards in opposition to mislaid updates and insert races, dissimilar about naive options. It volition lone activity successful Publication Dedicated manner and is lone harmless if it’s the lone happening you bash successful the transaction, although. The relation received’t activity accurately if triggers oregon secondary alone keys origin alone violations.

This scheme is precise inefficient. Every time applicable you ought to queue ahead activity and bash a bulk upsert arsenic described beneath alternatively.

Galore tried options to this job neglect to see rollbacks, truthful they consequence successful incomplete updates. 2 transactions contest with all another; 1 of them efficiently INSERTs; the another will get a duplicate cardinal mistake and does an Replace alternatively. The Replace blocks ready for the INSERT to rollback oregon perpetrate. Once it rolls backmost, the Replace information re-cheque matches zero rows, truthful equal although the Replace commits it hasn’t really performed the upsert you anticipated. You person to cheque the consequence line counts and re-attempt wherever essential.

Any tried options besides neglect to see Choice races. If you attempt the apparent and elemental:

-- THIS IS Incorrect. Bash NOT Transcript IT. It's an Illustration. Statesman; Replace testtable Fit somedata = 'blah' Wherever id = 2; -- Retrieve, this is Incorrect. Bash NOT Transcript IT. INSERT INTO testtable (id, somedata) Choice 2, 'blah' Wherever NOT EXISTS (Choice 1 FROM testtable Wherever testtable.id = 2); Perpetrate; 

past once 2 tally astatine erstwhile location are respective nonaccomplishment modes. 1 is the already mentioned content with an replace re-cheque. Different is wherever some Replace astatine the aforesaid clip, matching zero rows and persevering with. Past they some bash the EXISTS trial, which occurs earlier the INSERT. Some acquire zero rows, truthful some bash the INSERT. 1 fails with a duplicate cardinal mistake.

This is wherefore you demand a re-attempt loop. You mightiness deliberation that you tin forestall duplicate cardinal errors oregon mislaid updates with intelligent SQL, however you tin’t. You demand to cheque line counts oregon grip duplicate cardinal errors (relying connected the chosen attack) and re-attempt.

Delight don’t rotation your ain resolution for this. Similar with communication queuing, it’s most likely incorrect.

Bulk upsert with fastener

Typically you privation to bash a bulk upsert, wherever you person a fresh information fit that you privation to merge into an older present information fit. This is vastly much businesslike than idiosyncratic line upserts and ought to beryllium most well-liked every time applicable.

Successful this lawsuit, you sometimes travel the pursuing procedure:

  • Make a Impermanent array
  • Transcript oregon bulk-insert the fresh information into the temp array
  • Fastener the mark array Successful Unique Manner. This permits another transactions to Choice, however not brand immoderate modifications to the array.
  • Bash an Replace ... FROM of present information utilizing the values successful the temp array;
  • Bash an INSERT of rows that don’t already be successful the mark array;
  • Perpetrate, releasing the fastener.

For illustration, for the illustration fixed successful the motion, utilizing multi-valued INSERT to populate the temp array:

Statesman; Make Impermanent Array newvals(id integer, somedata matter); INSERT INTO newvals(id, somedata) VALUES (2, 'Joe'), (three, 'Alan'); Fastener Array testtable Successful Unique Manner; Replace testtable Fit somedata = newvals.somedata FROM newvals Wherever newvals.id = testtable.id; INSERT INTO testtable Choice newvals.id, newvals.somedata FROM newvals Near OUTER Articulation testtable Connected (testtable.id = newvals.id) Wherever testtable.id IS NULL; Perpetrate; 

Associated speechmaking

What astir MERGE?

SQL-modular MERGE really has poorly outlined concurrency semantics and is not appropriate for upserting with out locking a array archetypal.

It’s a truly utile OLAP message for information merging, however it’s not really a utile resolution for concurrency-harmless upsert. Location’s tons of proposal to group utilizing another DBMSes to usage MERGE for upserts, however it’s really incorrect.

Another DBs: