Dropping dependent tables

From: Tiffany Thang <tiffanythang(at)gmail(dot)com>
To: "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Dropping dependent tables
Date: 2021-06-03 13:41:53
Message-ID: CAB_W-NOtuEuOg=67m_mKyphnWvWRiCR4oSCOVwM14_XE_G-71A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
I would like to write a SQL statement to drop all the tables owned by me
but a problem I’m struggling with is with referential integrity. The
statement I have now to generate the drop statements is

select 'drop table '||tablename||' cascade;' from pg_tables where
tableowner='<myuseraccount>';

The generated SQLs above might attempt to drop the parent tables first
before the child and to be able to drop all the tables, I had to run the
SQL script in multiple iterations. Not very clean.

Can someone advise how I could formulate the SQL to check for table
dependencies to generate a SQL script that drops the child tables first
before the parent? Or are there any better alternatives?

Thank you.

Tiff

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2021-06-03 14:18:43 Re: Dropping dependent tables
Previous Message Ram Pratap Maurya 2021-06-03 12:53:04 RE: BUG #17046: Upgrade postgres 11 to 13 version