Re: Is there a "right" way to test if a database is empty? - Mailing list pgsql-hackers

From Graham Leggett
Subject Re: Is there a "right" way to test if a database is empty?
Date
Msg-id EC1D397E-6EBF-4E99-B8B8-EB6F79C88655@sharp.fm
Whole thread Raw
In response to Re: Is there a "right" way to test if a database is empty?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: Is there a "right" way to test if a database is empty?
List pgsql-hackers
On 17 Jan 2018, at 6:34 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:

​That was my original thought - though comparing the size of template1 to the target database should be reasonably safe...

If you do go for object detection you will want to ensure that no schemas other than public exist in addition to ensuring that public is empty.  That doesn't prevent people from installing stuff to pg_catalog but normally only extensions would end up there.

What led me here was this, which didn’t work for me, although the idea to not just assume the default namespace is valid:


Would it be true to say that if this query returned more than zero rows the database is not empty?

db=# select distinct s.nspname from pg_class c join pg_namespace s on s.oid = c.relnamespace where s.nspname not in ('pg_toast','information_schema','pg_catalog');
 nspname 
---------
 public
(1 row)

Regards,
Graham
--

Attachment

pgsql-hackers by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Is there a "right" way to test if a database is empty?
Next
From: "David G. Johnston"
Date:
Subject: Re: Is there a "right" way to test if a database is empty?