Re: Automatic deletion of orphaned rows - Mailing list pgsql-general

From Greg Sabino Mullane
Subject Re: Automatic deletion of orphaned rows
Date
Msg-id CAKAnmmLd9okDxM79MMomOvjLECm3zZfqbeUGY0RORCPwzTSqQw@mail.gmail.com
Whole thread Raw
In response to Automatic deletion of orphaned rows  ("Runxi Yu" <me@runxiyu.org>)
List pgsql-general
On Wed, Jan 22, 2025 at 2:00 AM Runxi Yu <me@runxiyu.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

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Automatic deletion of orphaned rows
Next
From: Ron Johnson
Date:
Subject: Re: Automatic deletion of orphaned rows