From: Adrian Klaver <adrian.klaver@aklaver.com>
To: 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 12:05:03 -0800
Message-ID: <2d136d22-e82d-c691-775b-c12aa29b079c@aklaver.com> (raw)
In-Reply-To: <SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com>
References: <CAKxhU8isUe6NUs7i7dJzdVeHHoK417Sv=z7jUoHZMHbezG_VkA@mail.gmail.com>
<SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com>
On 2/9/22 10:34, Brent Wood wrote:
> 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.
No:
\d animals
Table "public.animals"
Column | Type | Collation | Nullable | Default
--------+------------------------+-----------+----------+---------
id | integer | | not null |
cond | character varying(200) | | not null |
animal | character varying(200) | | not null |
Indexes:
"animals_pkey" PRIMARY KEY, btree (id)
delete from animals where id = 1;
DELETE 1
insert into animals values (1, 'great', 'opossum');
INSERT 0 1
>
> 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
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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, 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: <2d136d22-e82d-c691-775b-c12aa29b079c@aklaver.com>
References: <CAKxhU8isUe6NUs7i7dJzdVeHHoK417Sv=z7jUoHZMHbezG_VkA@mail.gmail.com> <SY4PR01MB7076851903BD922E73E4988AA12E9@SY4PR01MB7076.ausprd01.prod.outlook.com> <2d136d22-e82d-c691-775b-c12aa29b079c@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