pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Dropping dependent tables
3+ messages / 2 participants
[nested] [flat]

* Dropping dependent tables
@ 2021-06-03 13:41 Tiffany Thang <tiffanythang@gmail.com>
  2021-06-03 14:18 ` Re: Dropping dependent tables Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Tiffany Thang @ 2021-06-03 13:41 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

 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&...;
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&...;
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Dropping dependent tables
  2021-06-03 13:41 Dropping dependent tables Tiffany Thang <tiffanythang@gmail.com>
@ 2021-06-03 14:18 ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-06-04 01:10   ` Re: Dropping dependent tables Tiffany Thang <tiffanythang@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2021-06-03 14:18 UTC (permalink / raw)
  To: Tiffany Thang <tiffanythang@gmail.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

Tiffany Thang <tiffanythang@gmail.com> writes:
> 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.

Uh ... it's not clear to me why that wouldn't work.  CASCADE should
be able to take care of foreign keys:

postgres=# create table t1 (f1 int primary key);
CREATE TABLE
postgres=# create table t2 (f2 int references t1);
CREATE TABLE
postgres=# drop table t1;
ERROR:  cannot drop table t1 because other objects depend on it
DETAIL:  constraint t2_f2_fkey on table t2 depends on table t1
HINT:  Use DROP ... CASCADE to drop the dependent objects too.
postgres=# drop table t1 cascade;
NOTICE:  drop cascades to constraint t2_f2_fkey on table t2
DROP TABLE

Could you enlarge on what problem you saw, specifically?

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Dropping dependent tables
  2021-06-03 13:41 Dropping dependent tables Tiffany Thang <tiffanythang@gmail.com>
  2021-06-03 14:18 ` Re: Dropping dependent tables Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-06-04 01:10   ` Tiffany Thang <tiffanythang@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Tiffany Thang @ 2021-06-04 01:10 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Thu, Jun 3, 2021 at 10:18 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Tiffany Thang <tiffanythang@gmail.com> writes:
> > 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.
>
> Uh ... it's not clear to me why that wouldn't work.  CASCADE should
> be able to take care of foreign keys:
>
> postgres=# create table t1 (f1 int primary key);
> CREATE TABLE
> postgres=# create table t2 (f2 int references t1);
> CREATE TABLE
> postgres=# drop table t1;
> ERROR:  cannot drop table t1 because other objects depend on it
> DETAIL:  constraint t2_f2_fkey on table t2 depends on table t1
> HINT:  Use DROP ... CASCADE to drop the dependent objects too.
> postgres=# drop table t1 cascade;
> NOTICE:  drop cascades to constraint t2_f2_fkey on table t2
> DROP TABLE
>
> Could you enlarge on what problem you saw, specifically?
>
>                         regards, tom lane
>

Thanks Tom for your response. I tried it again and I was not able to
reproduce the issue.

Obviously I did something incorrectly previously.

Thanks.

Tiff

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2021-06-04 01:10 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-06-03 13:41 Dropping dependent tables Tiffany Thang <tiffanythang@gmail.com>
2021-06-03 14:18 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-06-04 01:10   ` Tiffany Thang <tiffanythang@gmail.com>

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