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

From David G. Johnston
Subject Re: Is there a "right" way to test if a database is empty?
Date
Msg-id CAKFQuwa8MBs_jMnBixw+GDf-OCsB8cdYbOiPC-GdF-O-a5NnuQ@mail.gmail.com
Whole thread Raw
In response to Re: Is there a "right" way to test if a database is empty?  (Graham Leggett <minfrin@sharp.fm>)
Responses Re: Is there a "right" way to test if a database is empty?
List pgsql-hackers
On Wed, Jan 17, 2018 at 9:10 AM, Graham Leggett <minfrin@sharp.fm> wrote:

db=# select count(s.nspname) from pg_class c join pg_namespace s on s.oid = c.relnamespace where s.nspname in ('public');
 count 
-------
     0
(1 row)

It is based on the idea that the database is not empty if there are any class entries in the “public” namespace?


​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.

David J.

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: master make check fails on Solaris 10
Next
From: Graham Leggett
Date:
Subject: Re: Is there a "right" way to test if a database is empty?