Re: how do I capture conflicting rows

From: Rui DeSousa <rui(at)crazybean(dot)net>
To: Nikhil Ingale <niks(dot)bgm(at)gmail(dot)com>
Cc: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: how do I capture conflicting rows
Date: 2023-05-15 14:53:03
Message-ID: 6D26434B-74A9-43FF-B593-07DAD0BBFD9E@crazybean.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

> On May 15, 2023, at 1:32 AM, Nikhil Ingale <niks(dot)bgm(at)gmail(dot)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
;

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Wolfgang Wilhelm 2023-05-15 15:20:24 Re: Options for more aggressive space reclamation in vacuuming?
Previous Message Jeff Janes 2023-05-15 14:40:18 Re: Options for more aggressive space reclamation in vacuuming?