Re: How to skip duplicate records while copying from CSV to table in Postgresql using "COPY"

From: Arup Rakshit <aruprakshit(at)rocketmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to skip duplicate records while copying from CSV to table in Postgresql using "COPY"
Date: 2015-05-24 14:18:03
Message-ID: 10543895.DrSTaAQAv7@linux-wzza.aruprakshit
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sunday, May 24, 2015 07:52:43 AM you wrote:
> >>
> >> Is it if the entire row is duplicated?
> >
> > It is entire row.
>
> So, Olivers second solution.
>

I have done this :

columns_t1 = self.singleton_class.fields.map { |f| "t1.#{f}" }.join(",")
columns_t2 = self.singleton_class.fields.map { |f| "t2.#{f}" }.join(",")
ActiveRecord::Base.transaction do
conn = ActiveRecord::Base.connection
conn.execute "CREATE TEMP TABLE tmp_table AS SELECT * FROM #{table.strip}; "
conn.execute("COPY tmp_table ( #{self.singleton_class.fields.join(',') } ) FROM '#{source_file}' CSV HEADER DELIMITER '\t' QUOTE '|' ;")
conn.execute "INSERT INTO #{table.strip} ( #{self.singleton_class.fields.join(',')} ) SELECT DISTINCT #{columns_t1} FROM tmp_table t1 WHERE NOT EXISTS ( SELECT 1 FROM #{table.strip} t2 WHERE (#{columns_t2}) IS NOT DISTINCT FROM (#{columns_t1}) );"
conn.execute "DROP TABLE IF EXISTS tmp_table;"
End

The SQL wrapped inside the ActiveRecord ORM as you see above. But I hope you got the idea. But I am not sure, if it is the correct way to do it or how it will hit the performance.....

The Application can run on different OS. So I am helpless to use Unix commands.

--
================
Regards,
Arup Rakshit
================
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2015-05-24 14:24:41 Re: How to skip duplicate records while copying from CSV to table in Postgresql using "COPY"
Previous Message Arup Rakshit 2015-05-24 13:24:56 Re: How to skip duplicate records while copying from CSV to table in Postgresql using "COPY"