473,507 Members | 3,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DBD::Pg handling duplicates in a transaction

**** Post for FREE via your newsreader at post.usenet.com ****

I am a newbie try to port my applications to Postgres. I have an
application that is bulk loading a table with autocommit off (with it on
it's way to slow). The logic of the application dictates that I try the
insert, and if it fails because of a duplicate, update the record instead.
The entire bulk load is wrapped in a transaction for speed purposes. When I
hit a duplicate I can detect it, but when I try the update it fails with:

ERROR: current transaction is aborted, commands ignored until end of
transaction block

I assume this is a result of the duplicate error, but is there a way I can
get around this. In my case duplicate is fatal to my transaction...

Thanks. Tim


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
http://www.usenet.com
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Jul 19 '05 #1
1 2434
After a long battle with technology, "Tim Nelson" <ti***************@softhome.net>, an earthling, wrote:
I am a newbie try to port my applications to Postgres. I have an
application that is bulk loading a table with autocommit off (with
it on it's way to slow). The logic of the application dictates that
I try the insert, and if it fails because of a duplicate, update the
record instead. The entire bulk load is wrapped in a transaction
for speed purposes. When I hit a duplicate I can detect it, but
when I try the update it fails with:

ERROR: current transaction is aborted, commands ignored until end of
transaction block

I assume this is a result of the duplicate error, but is there a way
I can get around this. In my case duplicate is fatal to my
transaction...


There is no ready way of blindly doing this.

Several improvements would seem plausible:

1. Try to do groups of 1000 records, committing every 1K, so that if
there's a failure, it is only expected to roll back on the order
of 500 rows.

2. Along with that approach, keep the last 1K records 'buffered' in
memory, so that if you hit a failure, you can quickly go back and
redo the ones that _were_ good, throwing in a COMMIT as soon as
you complete all the records you _know_ to be good.

3. Alternatively, before doing each row, do a query that checks to
see if the relevant key is already there. That is regrettably
likely to be rather time-consuming, and you'll be firing in
great gobs of little queries.

4. You could create a stored procedure that amounts to "insert or
ignore", and pass the data through that.

Thus, instead of
insert into mytable (f1,f2,f3,f4) values ('this', 'that',
'other', 'foo');

You might do...
select add_to_my_table ('this','that','other', 'foo');

5. You can cut down on the number of statements by grouping these
together...

select atmt ([row 1 data]), atmt([row 2 data]), atmt([row3
data]), ... atmt([row 20 data]).

That allows you to submit 20 records in one statement, which
is fairly likely to save some query submission overhead.
It oughtn't make parsing too much more expensive.

6. Stage the bulk load.

Load the data into a "staging" table, which has minimal (if any)
integrity checks; this would be most efficiently handled via
COPY, which would load it all in one transaction.

Then take that data and insert the relevant bits into the new
location.

Thus:

-- This'll be way fast
copy stage_table from '/tmp/cruddy_data.txt';

-- Maybe do some validation, fix crud, delete crud, and such...
select *; -- Look for crud
update *; -- Fix crud
delete *; -- Delete crud

-- stage_table now contains gleaming fixed data; load into final table...
insert into ultimate_table
select * from stage_table s where
not exists (select * from ultimate_table u where u.pkey = s.pkey);

-- Alternatively, if you had deleted the cruddy data, it might
-- just be:

insert into ultimate_table
select * from stage_table s;

I rather like approach #6, FYI...
--
output = ("cbbrowne" "@" "ntlug.org")
http://www3.sympatico.ca/cbbrowne/lsf.html
"The wrath of Holloway is nothing compared to the wrath of Moon."
-- Fred Drenckhahn
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2986
by: Pablo S | last post by:
Hi there mod_perl/Pg folks, I have 2 systems, one OLD, (linux 2.4 running postgresql-7.2.1-5 with a perl web db on Apache/1.3.23 mod_perl 1.26 and dbi 1.37 with peristent db connections via...
0
4643
by: Piotr B. | last post by:
Hello, I want to make use of a Perl script "ora2pg" (Oracle to PostgreSQL schema converter), which requires the following modules: DBI, DBD::Oracle and DBD::Pg. As I don't use Perl on a...
0
2376
by: Envex Developments | last post by:
Hey guys, I have a need to install the DBD::Pg Perl module on many shared web servers, which do not have PostgreSQL installed. Then the DBD::Pg module will just connect to a remote PostgreSQL...
0
2252
by: Envex Developments | last post by:
Hey guys, I have a need to install the DBD::Pg Perl module on many shared web servers, which do not have PostgreSQL installed. Then the DBD::Pg module will just connect to a remote PostgreSQL...
0
1132
by: Alex | last post by:
Hi, I am using DBD::Pg in some of my scripts. I want to customize the error login based on the error received. While I am happy with the Errstr message I want to take specific actions depending...
10
2887
by: Ausrack Webmaster | last post by:
Hi I am trying to insert a simple email address into a text field, and I get the below error: DBD::Pg::st execute failed: ERROR: pg_atoi: error in "<support@somedomain.com>": can't parse...
11
2300
by: greg | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A new version of DBD::Pg is about to be released (1.32), and we need your help to test it out. If you use DBD::Pg, please download and test the...
4
1361
by: Patrick Hatcher | last post by:
Pg: 7.4.2 I use perl scripts to import data into my db. When errors occurred uploading files in Pg ver 7.3.x, $DBI::errstr used to return a row number from the input file. I could then go to...
2
2999
by: David Siebert | last post by:
Anyone using Activestate Perl and DBD-Pg? I am using perl 5.8.3 ppm does not seem to work. I downloaded the DBD-Pg ..zip file I found through google but ppm could not seem to install that. Any...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.