From: Erik Wienhold <ewie@ewie.name>
To: Dominique Devienne <ddevienne@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: DELETE trigger, direct or indirect?
Date: Thu, 16 Feb 2023 17:09:21 +0100 (CET)
Message-ID: <1955533673.226407.1676563761989@office.mailbox.org> (raw)
In-Reply-To: <CAFCRh-_aJjgHvjbE4OvNHMAWygdJAC026_rgGYj1kfTmB-EvJA@mail.gmail.com>
References: <CAFCRh-_aJjgHvjbE4OvNHMAWygdJAC026_rgGYj1kfTmB-EvJA@mail.gmail.com>
> On 16/02/2023 14:23 CET Dominique Devienne <ddevienne@gmail.com> wrote:
>
> Hi. This is a bit unusual. We have a foreign key between two tables, with
> ON DELETE CASCADE, to preserve referential integrity. But we apparently
> also need to preserve the severed reference (by natural key, i.e. its name),
> to later on reconnect the two entities after-the-fact, should the parent
> row re-appear later on (in the same transaction or not it still unclear).
>
> To achieve this weird requirement, I'd like to know if it is possible in an
> ON DELETE trigger to know whether the deletion is coming from a direct-DELETE
> in the "child table", or whether the deletion is coming from the "parent
> table" CASCADEd to the child table.
Not to my knowledge. ON DELETE CASCADE behaves like a manual DELETE on the
child table that happens before the DELETE on the parent table.
The process you describe shows that it's not known until the end of the
transaction which parent rows can be deleted. You can instead track the
parent rows as candidates for deletion in a temp table. Insert the primary
key of parent rows if you deem them deletable and delete the primary key if
you detect the opposite. At the end the temp table only contains IDs of
parent rows that can be deleted for sure.
--
Erik
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: ewie@ewie.name, ddevienne@gmail.com
Subject: Re: DELETE trigger, direct or indirect?
In-Reply-To: <1955533673.226407.1676563761989@office.mailbox.org>
References: <CAFCRh-_aJjgHvjbE4OvNHMAWygdJAC026_rgGYj1kfTmB-EvJA@mail.gmail.com> <1955533673.226407.1676563761989@office.mailbox.org>
* 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