Is it possible to ignore the error and continue execute update in PostgreSQL

postgresql

I have a table stored all my project rss channel url, now I found some url end with '/' but some sub url are not. I my app I have to handle this situation in everywhere. Then I want to store all the sub url link without the last '/', if the url end with '/', I want to delete the end of '/'. I have write the update sql command like this:

UPDATE rss_sub_source 
SET sub_url = SUBSTRING(sub_url, 1, CHAR_LENGTH(sub_url) - 1) 
WHERE sub_url LIKE '%/';

when I execute the sql:

SQL Error [23505]: ERROR: duplicate key value violates unique constraint "unique_sub_url"
  Detail: Key (sub_url)=(https://physicsworld.com/feed) already exists.

the error shows that some url without '/' have already exists. when I update wht end with '/' url, it will conflict with the exists one because I add an uniq constraint. There table contains thousands of url, update one by one obviously impossible. So I want to ignore and jump to update the url if it did not obey the uniq constraint, only update the success record. Finnaly delete the end with '/' record.

Is it possible to ignore the update error events in PostgreSQL? if not what should I do to make all rss url did not end with '/'?

Best Answer

You can check if such a sub_url already exists:

UPDATE rss_sub_source 
SET sub_url = SUBSTRING(sub_url, 1, CHAR_LENGTH(sub_url) - 1) 
WHERE sub_url LIKE '%/' 
AND NOT EXISTS 
(
  SELECT 1 FROM rss_sub_source 
  WHERE sub_url = SUBSTRING
                  (
                    sub_url, 1, CHAR_LENGTH(sub_url) - 1
                  ) 
)