Re: Automatic deletion of orphaned rows

From: Greg Sabino Mullane <htamfids(at)gmail(dot)com>
To: Runxi Yu <me(at)runxiyu(dot)org>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Automatic deletion of orphaned rows
Date: 2025-01-22 15:11:26
Message-ID: CAKAnmmLd9okDxM79MMomOvjLECm3zZfqbeUGY0RORCPwzTSqQw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jan 22, 2025 at 2:00 AM Runxi Yu <me(at)runxiyu(dot)org> wrote:

> I therefore propose a feature, to be able to specify in a table schema
> that a row should be deleted if orphaned.
>

I think you mean "childless" rows, as "orphaned" has a different meaning
traditionally.

When and how would this deletion take place? And why not just run the
delete yourself?
It would help to show us exactly the behavior you want. Here's some sample
tables we
can use:

create table parent( id int primary key );

create table kid( refid int references parent(id) );

insert into parent values (1),(2),(3);

insert into kid values (1);

-- remove any rows non-referenced rows (aka childless)
delete from parent where not exists (select 1 from kid where refid=parent.id
);

select * from parent;
id
----
1

Cheers,
Greg

--
Crunchy Data - https://www.crunchydata.com
Enterprise Postgres Software Products & Tech Support

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2025-01-22 15:15:04 Re: Automatic deletion of orphaned rows
Previous Message David G. Johnston 2025-01-22 14:37:17 Re: Automatic deletion of orphaned rows