pg.ddx.io pgsql-general@postgresql.org mailing list archivehelp / color / mirror / Atom feed
From: Brent Wood <Brent.Wood@niwa.co.nz>
To: Andrew Hardy <andrew.hardy@sabstt.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: DELETING then INSERTING record with same PK in the same TRANSACTION
Date: Wed, 9 Feb 2022 18:34:29 +0000
Message-ID: <SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com> (raw)
In-Reply-To: <CAKxhU8isUe6NUs7i7dJzdVeHHoK417Sv=z7jUoHZMHbezG_VkA@mail.gmail.com>
References: <CAKxhU8isUe6NUs7i7dJzdVeHHoK417Sv=z7jUoHZMHbezG_VkA@mail.gmail.com>
My take on this...
Because both statements are in the transaction, the delete is not fully actioned until the commit. So it still exists in the table when you try to insert the record with the duplicate key.
Check if the error is generated during the transaction or at the commit stage, run it without the commit, rollback instead to check this.
I don't see how you can do this within a transaction, someone else might?
Brent Wood
Principal Technician, Fisheries
NIWA
DDI: +64 (4) 3860529
________________________________
From: Andrew Hardy <andrew.hardy@sabstt.com>
Sent: Thursday, February 10, 2022 07:11
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: DELETING then INSERTING record with same PK in the same TRANSACTION
Hi,
When I:
Begin a transaction
DELETE from <table> where id (PK) = <somevalue>
INSERT INTO <table> VALUES (<values - same PK>)
...
...
...
COMMIT
I get
insert into "itinerary" ("dk", "locator") values ($1, $2) - duplicate key value violates unique constraint "itinerary_pkey"
Do I need some particular kind of settings on my transaction to be able to delete and insert afresh in the same transaction?
In case it is relevant - the first delete will lead to cascaded deletes on children.
Alternatively I wonder if I change the PK column value to "<somevalue>-FORDELETION" will I be free to insert under the same original PK value in the same transaction, then delete the FORDELETE item just before committing or will I hit the same issue?
Thanks,
Andrew
[https://www.niwa.co.nz/static/niwa-2018-horizontal-180.png] <https://www.niwa.co.nz;
Brent Wood
Principal Technician - GIS and Spatial Data Management
Programme Leader - Environmental Information Delivery
+64-4-386-0529
National Institute of Water & Atmospheric Research Ltd (NIWA)
301 Evans Bay Parade Hataitai Wellington New Zealand
Connect with NIWA: niwa.co.nz<https://www.niwa.co.nz; Facebook<https://www.facebook.com/nzniwa; LinkedIn<https://www.linkedin.com/company/niwa; Twitter<https://twitter.com/niwa_nz; Instagram<https://www.instagram.com/niwa_science;
To ensure compliance with legal requirements and to maintain cyber security standards, NIWA's IT systems are subject to ongoing monitoring, activity logging and auditing. This monitoring and auditing service may be provided by third parties. Such third parties can access information transmitted to, processed by and stored on NIWA's IT systems
view thread (11+ messages) latest in thread
Message-ID: <SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com> Permalink: ../SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com/ Also on: postgresql.org/message-id/SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.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: Brent.Wood@niwa.co.nz, andrew.hardy@sabstt.com Subject: Re: DELETING then INSERTING record with same PK in the same TRANSACTION In-Reply-To: <SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com> References: <CAKxhU8isUe6NUs7i7dJzdVeHHoK417Sv=z7jUoHZMHbezG_VkA@mail.gmail.com> <SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.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