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

From Alvaro Herrera
Subject Re: Is there a "right" way to test if a database is empty?
Date
Msg-id 20180117180631.zu6lj6b5u4ftsawn@alvherre.pgsql
Whole thread Raw
In response to Is there a "right" way to test if a database is empty?  (Graham Leggett <minfrin@sharp.fm>)
List pgsql-hackers
Graham Leggett wrote:
> Hi all,
> 
> I need to test whether a database is empty, in other words “createdb”
> has been executed but no data of any kind appears in that database.

Why do you want to know?

Depends on how you define empty.  If a few functions exist but no
tables, is the database empty?  I'd say no.  One possible approach is:
if no relations (pg_class rows) exist in namespaces other than
pg_catalog, pg_toast, information_schema; and no functions (pg_proc
rows) exist in any schema other than those three, then the database is
empty.  There are a few object types you could create without any
relation and without any function (such as casts, or schemas, or text
search objects) but you probably don't care.

Maybe make sure no extensions are installed also.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


pgsql-hackers by date:

Previous
From: Emre Hasegeli
Date:
Subject: Re: [HACKERS] [PATCH] Improve geometric types
Next
From: Ivan Novick
Date:
Subject: Re: Builtin connection polling