pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Leandro Guimarães <leo.guimaraes@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Copy Bulk Ignore Duplicated
Date: Mon, 17 Jun 2019 10:01:14 -0700
Message-ID: <3e8bd3be-407f-8bcf-1b53-8dd0726c0116@aklaver.com> (raw)
In-Reply-To: <5380793b-0380-f494-c7a4-488770df3a3e@aklaver.com>
References: <CAJV35FN-pL3-oYHVDpGUM0coD0zgcF8c+xgcAu+VmUs27JyCmg@mail.gmail.com>
	<87o92zpzba.fsf@gmail.com>
	<CAJV35FPDDhgTcvtp=e_PBvjBe0+5b3+KfGHrVSzK+nuBT5+uxw@mail.gmail.com>
	<c426e95f-3144-410b-413c-4c6b016d7d2b@aklaver.com>
	<CAJV35FN428XGBeNYUf_y5YnydTG4mW2Chk1w9_fLUMd-tJaKHA@mail.gmail.com>
	<c051176c-3727-5fc3-fcf2-6550abe0714f@aklaver.com>
	<CAJV35FP03Vzw7=KhGt1AoieZSvDmZLrHUTDN=MJAYUhDYpVHRw@mail.gmail.com>
	<5380793b-0380-f494-c7a4-488770df3a3e@aklaver.com>

On 6/17/19 9:22 AM, Adrian Klaver wrote:
> On 6/17/19 9:06 AM, Leandro Guimarães wrote:
> Please reply to list also.
> Ccing list.
>> Ugh My bad again.
>>
>> They are UNIQUE:
>> CONSTRAINT unique_const_value_20190501_45 UNIQUE (customer_id, 
>> date_time, indicator_id, element_id),
>>
>> I've made a mistake typing "check constraint" before because these are 
>> partitioned tables and I have the CHECK CONSTRAINT to partition.
>>
>> Is that clear?
> 
> Yes.
> 
> To get back to the original issue, the problem is that when you COPY in 
> new data you may get rows that conflict on the above UNIQUE constraint, 
> correct?

Assuming the above is correct, would not something like below work?:

create table orig_tbl(id integer, fld_1 varchar, fld_2 integer, fld_3 
varchar, CONSTRAINT u_idx UNIQUE(id, fld_1, fld_2));

\d orig_tbl
                    Table "public.orig_tbl"
  Column |       Type        | Collation | Nullable | Default
--------+-------------------+-----------+----------+---------
  id     | integer           |           |          |
  fld_1  | character varying |           |          |
  fld_2  | integer           |           |          |
  fld_3  | character varying |           |          |
Indexes:
     "u_idx" UNIQUE CONSTRAINT, btree (id, fld_1, fld_2)

create table tmp_tbl(id integer, fld_1 varchar, fld_2 integer, fld_3 
varchar);

insert into orig_tbl values (1, 'test', 3, 'test'), (2, 'foo', 5, 
'bar'), (3, 'cat', 8, 'dog');

select * from orig_tbl ;

  id | fld_1 | fld_2 | fld_3
----+-------+-------+-------
   1 | test  |     3 | test
   2 | foo   |     5 | bar
   3 | cat   |     8 | dog
(3 rows)

insert into tmp_tbl values (1, 'test', 3, 'test'), (4, 'fish', 6, 
'bird'), (7, 'rabbit', 8, 'squirrel'), (10, 'plant', 2, 'animal');

select * from tmp_tbl ;

  id | fld_1  | fld_2 |  fld_3
----+--------+-------+----------
   1 | test   |     3 | test
   4 | fish   |     6 | bird
   7 | rabbit |     8 | squirrel
  10 | plant  |     2 | animal
(4 rows)

select * from tmp_tbl AS tt left join orig_tbl AS ot on (tt.id, 
tt.fld_1, tt.fld_2) = (ot.id, ot.fld_1, ot.fld_2) where ot.id is null;

  id | fld_1  | fld_2 |  fld_3   | id | fld_1 | fld_2 | fld_3
----+--------+-------+----------+----+-------+-------+-------
   4 | fish   |     6 | bird     |    |       |       |
   7 | rabbit |     8 | squirrel |    |       |       |
  10 | plant  |     2 | animal   |    |       |       |

> 
>>
>> Thanks for your patience!
>> Leandro Guimarães
>>
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (16+ messages)  latest in thread

Message-ID: <3e8bd3be-407f-8bcf-1b53-8dd0726c0116@aklaver.com>
Permalink:  ../3e8bd3be-407f-8bcf-1b53-8dd0726c0116@aklaver.com/
Also on:    postgresql.org/message-id/3e8bd3be-407f-8bcf-1b53-8dd0726c0116@aklaver.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, leo.guimaraes@gmail.com
  Subject: Re: Copy Bulk Ignore Duplicated
  In-Reply-To: <3e8bd3be-407f-8bcf-1b53-8dd0726c0116@aklaver.com>
  References: <CAJV35FN-pL3-oYHVDpGUM0coD0zgcF8c+xgcAu+VmUs27JyCmg@mail.gmail.com> <87o92zpzba.fsf@gmail.com> <CAJV35FPDDhgTcvtp=e_PBvjBe0+5b3+KfGHrVSzK+nuBT5+uxw@mail.gmail.com> <c426e95f-3144-410b-413c-4c6b016d7d2b@aklaver.com> <CAJV35FN428XGBeNYUf_y5YnydTG4mW2Chk1w9_fLUMd-tJaKHA@mail.gmail.com> <c051176c-3727-5fc3-fcf2-6550abe0714f@aklaver.com> <CAJV35FP03Vzw7=KhGt1AoieZSvDmZLrHUTDN=MJAYUhDYpVHRw@mail.gmail.com> <5380793b-0380-f494-c7a4-488770df3a3e@aklaver.com> <3e8bd3be-407f-8bcf-1b53-8dd0726c0116@aklaver.com>

* 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