pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tiffany Thang <tiffanythang@gmail.com>
Cc: Forums postgresql <pgsql-general@postgresql.org>
Subject: Re: List of objects owned by a schema/owner
Date: Fri, 03 Aug 2018 10:52:04 -0400
Message-ID: <14401.1533307924@sss.pgh.pa.us> (raw)
In-Reply-To: <CAB_W-NOH2U3qb9YS0DemzsaN+FXdsOsDOzXgfoHnMrWp7HBTxw@mail.gmail.com>
References: <CAB_W-NOH2U3qb9YS0DemzsaN+FXdsOsDOzXgfoHnMrWp7HBTxw@mail.gmail.com>

Tiffany Thang <tiffanythang@gmail.com> writes:
> Does anyone have a query that will list all the objects (composite types,
> sequences, tables, triggers, functions, indices, etc) owned by a schema or
> owner? I find fragments of information here and there that query on
> pg_tables, pg_views, etc.

Well, you could attack it the hard way:

  select relname from pg_class where relowner = [oid of role of interest]
  union all
  ... similar select from every other catalog that has an owner column ...

I don't know if anyone's built such a query before, but a little quality
time with the system catalog documentation would get you there:
https://www.postgresql.org/docs/current/static/catalogs.html

Another idea is to rely on owner dependencies recorded in pg_shdepend,
along the lines of

select pg_describe_object(classid,objid,objsubid)
from pg_shdepend where deptype = 'o' and
  refobjid = [oid of role of interest] and
  dbid = [oid of current database];

That won't work for objects owned by the bootstrap superuser, and
I think there are some other omissions --- for instance, it looks
like we only record an owner dependency for a table, not for its
indexes.

Or, if you're feeling truly lazy, you can do

begin;
drop user joe;
-- read the error message whining about what joe owns
rollback;

That's largely a hacky way to get the same info as the pg_shdepend
query I sketched before, since the error message is derived from
exactly that info.

You have the same three options for schemas, though the details of each
are a bit different (in particular, schema dependencies would be found
in pg_depend not pg_shdepend).

			regards, tom lane




view thread (3+ messages)  latest in thread

Message-ID: <14401.1533307924@sss.pgh.pa.us>
Permalink:  ../14401.1533307924@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/14401.1533307924@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, tiffanythang@gmail.com
  Subject: Re: List of objects owned by a schema/owner
  In-Reply-To: <14401.1533307924@sss.pgh.pa.us>
  References: <CAB_W-NOH2U3qb9YS0DemzsaN+FXdsOsDOzXgfoHnMrWp7HBTxw@mail.gmail.com> <14401.1533307924@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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