RE: Insert data if it is not existing

From: Steven Winfield <Steven(dot)Winfield(at)cantabcapital(dot)com>
To: tango ward <tangoward15(at)gmail(dot)com>
Cc: pgsql-generallists(dot)postgresql(dot)org <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: RE: Insert data if it is not existing
Date: 2018-05-23 10:09:14
Message-ID: E9FA92C2921F31408041863B74EE4C2001AEF00676@CCPMAILDAG03.cantab.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

From the docs:
“ON CONFLICT can be used to specify an alternative action to raising a unique constraint or exclusion constraint violation error.”

So if the INSERT part succeeds then the ON CONFLICT part is never executed.
If the INSERT fails with due to a violation of the constraint you specified (or was implied) then the ON CONFLICT part is executed instead. An UPDATE here can raise further errors, of course.
If the INSERT fails for a different reason then the ON CONFLICT part is not executed.

Steve.

From: tango ward [mailto:tangoward15(at)gmail(dot)com]
Sent: 23 May 2018 10:46
To: Steven Winfield
Cc: pgsql-generallists.postgresql.org
Subject: Re: Insert data if it is not existing

thanks for the response Steven.

Will ON CONFLICT DO UPDATE/NOTHING if there's no error?

On Wed, May 23, 2018 at 5:43 PM, Steven Winfield <Steven(dot)Winfield(at)cantabcapital(dot)com<mailto:Steven(dot)Winfield(at)cantabcapital(dot)com>> wrote:
INSERT … ON CONFLICT DO UPDATE … is probably what you want, perhaps using a specified unique index/constraint:
https://www.postgresql.org/docs/10/static/sql-insert.html#SQL-ON-CONFLICT<https://www.postgresql.org/docs/10/static/sql-insert.html#SQL-ON-CONFLICT>

Steve.

From: tango ward [mailto:tangoward15(at)gmail(dot)com<mailto:tangoward15(at)gmail(dot)com>]
Sent: 23 May 2018 10:04
To: pgsql-generallists.postgresql.org<http://pgsql-generallists.postgresql.org>
Subject: Insert data if it is not existing

Hi,

I just want to ask if it's possible to insert data if it's not existing yet. I was able to play around with UPSERT before but that was when there was an error for duplicate data. In my scenario, no error message is showing.

Any suggestion?

Thanks,
J

________________________________
This email is confidential. If you are not the intended recipient, please advise us immediately and delete this message. The registered name of Cantab- part of GAM Systematic is Cantab Capital Partners LLP. See - http://www.gam.com/en/Legal/Email+disclosures+EU<http://www.gam.com/en/Legal/Email+disclosures+EU> for further information on confidentiality, the risks of non-secure electronic communication, and certain disclosures which we are required to make in accordance with applicable legislation and regulations. If you cannot access this link, please notify us by reply message and we will send the contents to you.

GAM Holding AG and its subsidiaries (Cantab – GAM Systematic) will collect and use information about you in the course of your interactions with us. Full details about the data types we collect and what we use this for and your related rights is set out in our online privacy policy at https://www.gam.com/en/legal/privacy-policy<https://www.gam.com/en/legal/privacy-policy>. Please familiarise yourself with this policy and check it from time to time for updates as it supplements this notice
________________________________

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sangeeth Keeriyadath 2018-05-23 10:39:15 Postgresql process aborted on shutdown of filesystem holding the database
Previous Message legrand legrand 2018-05-23 09:57:16 Re: Help in Postgresql