Delete all records NOT referenced by Foreign Keys - Mailing list pgsql-general

From D. Dante Lorenso
Subject Delete all records NOT referenced by Foreign Keys
Date
Msg-id 3FDACBBF.8050901@lorenso.com
Whole thread Raw
Responses Re: Delete all records NOT referenced by Foreign Keys
Re: Delete all records NOT referenced by Foreign Keys
List pgsql-general
I'd like to run a clean up command on my tables to
eliminate rows that I'm no longer using in the database.

I want to do something like this:

    DELETE FROM tablename
    WHERE IS_REFERENCED_BY_FOREIGN_KEY IS FALSE;

Does anyone know how something like this could be done
in PostgreSQL?  I know I can search all the tables that
I know refer to this table and see if my primary key
exists, but I want a solution that does not require me to
rewrite my code every time a new foreign key constraint
is added to the database.

There must be a way to ask PostgreSQL for a reference count
on a given row or something.

Thanks,

Dante

---------
D. Dante Lorenso
dante@lorenso.com






pgsql-general by date:

Previous
From: "Matthew T. O'Connor"
Date:
Subject: Re: Windows Version of PostgreSQL
Next
From: "Keith C. Perry"
Date:
Subject: Re: tablespaces in 7.5?