pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rui DeSousa <rui@crazybean.net>
To: Nikhil Ingale <niks.bgm@gmail.com>
Cc: pgsql-admin@lists.postgresql.org
Subject: Re: how do I capture conflicting rows
Date: Mon, 15 May 2023 10:53:03 -0400
Message-ID: <6D26434B-74A9-43FF-B593-07DAD0BBFD9E@crazybean.net> (raw)
In-Reply-To: <CALXkTpx3iLuGRx1_4N6GJ85ptoW4n4XSdh3ChJLE-hrFGC982g@mail.gmail.com>
References: <CALXkTpx3iLuGRx1_4N6GJ85ptoW4n4XSdh3ChJLE-hrFGC982g@mail.gmail.com>



> On May 15, 2023, at 1:32 AM, Nikhil Ingale <niks.bgm@gmail.com> wrote:
> 
> INSERT INTO test (id,name,age,branch) SELECT * FROM student ON CONFLICT DO NOTHING;
> 
> How do I capture the conflicting records to a file while non conflicting records are inserted to the table?
> 

You can return the rows inserted and from that you can determine which rows had conflicts by returning the inserted rows. 

with x (id, name, age, branch) as (
  select id, name, age, branch
  from student 
), insrt (id) as (
  insert into test (id,name,age,branch) 
  select id, name, age, branch from x 
  on conflict do nothing 
  returning id
)
select x.*
from x
left out join insrt on insrt.id = x.id
where insrt.id is null
;=

view thread (8+ messages)

Message-ID: <6D26434B-74A9-43FF-B593-07DAD0BBFD9E@crazybean.net>
Permalink:  ../6D26434B-74A9-43FF-B593-07DAD0BBFD9E@crazybean.net/
Also on:    postgresql.org/message-id/6D26434B-74A9-43FF-B593-07DAD0BBFD9E@crazybean.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: rui@crazybean.net, niks.bgm@gmail.com, pgsql-admin@lists.postgresql.org
  Subject: Re: how do I capture conflicting rows
  In-Reply-To: <6D26434B-74A9-43FF-B593-07DAD0BBFD9E@crazybean.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox