0

Does Postgres have any limits for a number of tables to be deleted in one DROP TABLE command (about 10000 in my case)? Does it depend on the version? Will it be faster than executing the command 10 times & 1000 tables?

The possibilities for testing this are limited in my case, so please share if you've had a similar experience.

3
  • Does it represent the entire database?
    – Caius Jard
    Commented Feb 7, 2022 at 6:34
  • Yes, there is a limit imposed by max_locks_per_transaction which is driven by the number of tables dropped in a single transaction.
    – user330315
    Commented Feb 7, 2022 at 6:38
  • @CaiusJard no it's not. And I'm not allowed to use CASCADE and I must specify all the tables explicitly, that's the policy when dealing with customer's data.
    – tconsta
    Commented Feb 7, 2022 at 6:52

2 Answers 2

2

There is no theoretical limit on the number of tables you can drop in one statement. However, each table dropped will require a couple of ACCESS EXCLUSIVE locks, which are retained in the locking table until the end of the transaction, so you will exceed the default limit of 6400 locks at some point. Increasing max_locks_per_transaction will increase the limit and is safe to do.

2
  • As I found out you got 6400 as max_locks_per_transaction × max_connections = 64*100, but I haven't found any references to max_locks_per_session you mentioned
    – tconsta
    Commented Feb 13, 2022 at 7:28
  • That was a mistake obviously. Commented Feb 13, 2022 at 11:33
1

Use This For All Table Of Database.

DO $$ 
  DECLARE 
    r RECORD;
BEGIN
  FOR r IN 
    (
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema=current_schema()
    ) 
  LOOP
     EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.table_name) || ' CASCADE';
  END LOOP;
END $$ ;

1
  • I don't get it; I specifically ask if the OP is trying to drop every table in the database, they say "no", an answer is posted that drops every table in the database (and it gets an upvote).. Life amuses me! :D
    – Caius Jard
    Commented Feb 7, 2022 at 10:48

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.