Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of ON CONFLICT DO NOTHING for UPDATE postgres

I want to update rows in my postgres database if the updated version wouldn't violate the primary key constraint. If it would, I want to leave the row as it is.

Assuming the table has primary keys on col1, col2, col3, if I run a query like this:

UPDATE table SET (col1, col2) = ('A', 'B') 
      WHERE col1='D' AND col2='E';

The query will fail and I will get a duplicate key error if there exists two entries:

'A', 'B', 'C'
'D', 'E', 'C'

i.e col3 is the same between an existing row and a row to be updated.

If I was INSERTing rows I would use ON CONFLICT DO NOTHING but I can't find an implementation of this for UPDATE. Does an equivalent exist?

like image 920
BHC Avatar asked Jan 11 '19 15:01

BHC


People also ask

What does on conflict do nothing mean?

ON CONFLICT DO NOTHING simply avoids inserting a row as its alternative action. ON CONFLICT DO UPDATE updates the existing row that conflicts with the row proposed for insertion as its alternative action.

How does upsert work in PostgreSQL?

In relational databases, the term upsert is referred to as merge. The idea is that when you insert a new row into the table, PostgreSQL will update the row if it already exists, otherwise, it will insert the new row. That is why we call the action is upsert (the combination of update or insert).

Does Postgres support upsert?

Postgres now supports UPSERT - git.postgresql.org/gitweb/…

Where Not Exists VS on conflict?

NOT EXISTS will still attempt to insert duplicates if these exists in the source (tables), and fail. ON CONFLICT IGNORE will insist (and succeed) on inserting/updating them, in an undefined order.

Is the on conflict clause available in PostgreSQL?

Notice that the ON CONFLICT clause is only available from PostgreSQL 9.5. If you are using an earlier version, you will need a workaround to have the upsert feature. If you are also working with MySQL, you will find that the upsert feature is similar to the insert on duplicate key update statement in MySQL.

How to update the name column on conflict in PostgreSQL?

We use the virtual EXCLUDED table, which contains the items we intended to insert, to update the name column to a new value on conflict. You can show that the records were all updated or added by typing: PostgreSQL's INSERT...ON CONFLICT construct allows you to choose between two options when a proposed record conflicts with an existing record.

What is a do update in PostgreSQL?

DO UPDATE: This tells PostgreSQL that you want to update the row that is already in the table. The syntax for the update mirrors that of the normal UPDATE command. When DO UPDATE is specified, a special virtual table called EXCLUDED is available for use within the UPDATE clause.

What does do nothing do in PostgreSQL?

DO NOTHING: Tells PostgreSQL to leave the conflicting record as-is. In essence, this action makes no changes, but suppresses the error that would normally occur if you tried to insert a row that violates a condition.


2 Answers

You can use a correlated subquery with a WHERE NOT EXISTS clause to ensure that your update will not generate duplicates, like :

UPDATE mytable t
SET (col1, col2) = ('AAA', 'BBB')
WHERE t.col1 = 'AAB' and t.col2 = 'BBA'
AND NOT EXISTS (
   SELECT 1 FROM mytable WHERE col1 = 'AAA' AND col2 = 'BBB' AND col3 = t.col3
);

Tested in this db fiddle.

As commented by Roman Konoval, please note that this would still generate duplicate key error if a concurrent transaction inserts the same key while the UPDATE is running. This pinpoints that updating the primary key of a table is not a good practice (see the below answer from @Lau for a detailed discussion on this matter).

like image 80
GMB Avatar answered Sep 18 '22 10:09

GMB


AFAIK, there is no such equivalent.

Let us say you are developing an application that connects to a postgresql database, there are a few things you need to keep in mind, in the context of your question:

  • It may be counter-intuitive but you should consider errors being thrown by the DB as a good thing.
    This is just about getting a status, it does not mean application crash.
  • For insert, there is an alternative choice of action on conflict (update or nothing) so it makes sense to have a syntax to let you decide.
    For updates, the only thing you can do is ... nothing.
    So why would SQL let you ask to do something specific since there is no choice? Remember that DB reporting errors is good, so let the DB do nothing and tell you why.
  • Last, it is a bad practice to update primary keys.
    The ON CONFLICT ... for inserts is not intended to update the primary key fields. The very opposite in fact: it is intended to update all the fields except the ones from the primary key in a single record.

While I am on that point, please note that there was no need for a conflict on primary key for the query to fail
1 record with the "convenient" ON UPDATE NO ACTION foreign key would have made it fail too (which is still better than updating 10M+ records in 50 tables with a ON UPDATE CASCADE ...). BTW, did you know Oracle does not even have the ON UPDATE CASCADE clause? What do you think is the reason for that?


What can you/should not do in that situation?

  1. Do not update the primary key, like I said. Your question is still valid for UNIQUE constraints but please please please, NEVER update primary keys.
  2. Do not attempt to see if a conflicting record already exists. It may take a long time and still be unreliable.
    Do you really want to select millions of records just to avoid the error codes?
    Also, when you extend to other constraints (CHECK or EXCLUSION), will you really type the additional code it takes with no error in order to, once again, only avoid an error code?
    Last, if you have implemented row-level security, the conflict may arise from a record you cannot see.
  3. Handle the error code in your app. Receiving status is GOOD.
  4. Use save points if you are in the middle of a transaction.
    This is the only annoying thing with DB errors: if you get one in the middle of a transaction, you will start getting current transaction is aborted, commands ignored until end of transaction block for everything.
    Hopefully, you do not need to roll the entire transaction back and redo everything from scratch. You can get away using the following piece of code.

Here you go:

BEGIN;
SAVEPOINT MySavepoint;
UPDATE mytable set myuniquefield = 3; /*2+ records are going to be updated */
rollback to savepoint MySavepoint;
/*Insert Some more queries here*/
COMMIT;
like image 33
FXD Avatar answered Sep 18 '22 10:09

FXD