0

I have a table users : id,name, etc...

I want to get a list of all tables,views that use users.id

for example if table user_new is refrencing users.id as FK I want it to be desplayed in the query.

How do I do that?

Un Peu
  • 131
  • 3
  • 13
  • If this is a one-off check, the easiest way is to run `BEGIN; ALTER TABLE users DROP COLUMN id; ROLLBACK;`. The `DROP` should fail, and the referencing constraints and views will be reported in the error message. – Nick Barnes Sep 24 '15 at 08:20
  • @NickBarnes It's really a bad idea !! – Vivek S. Sep 24 '15 at 09:48

3 Answers3

2

Use pg_depend.

Example:

create table some_table (
    id serial primary key);

create table child_table (
    id int, 
    master_id int references some_table(id));

create view some_view as 
    select * from some_table;

select distinct pg_describe_object(classid, objid, objsubid)
from pg_depend 
where refobjid = 'some_table'::regclass

                     pg_describe_object
------------------------------------------------------------
 sequence some_table_id_seq
 constraint some_table_pkey on table some_table
 type some_table
 default for table some_table column id
 constraint child_table_master_id_fkey on table child_table
 rule _RETURN on view some_view
(6 rows)

The above query selects all objects referencing to some_table. You can filter the result for a specific column and/or desired type of relationship. To select only tables and views depending on the first column, use:

select distinct pg_describe_object(classid, objid, objsubid)
from pg_depend
where 
    refobjid = 'some_table'::regclass 
    and refobjsubid = 1 -- only for column #1
    and deptype = 'n'; -- a normal relationship between separately-created objects

                     pg_describe_object
------------------------------------------------------------
 constraint child_table_master_id_fkey on table child_table
 rule _RETURN on view some_view
(2 rows)    
klin
  • 112,967
  • 15
  • 204
  • 232
  • The query in my answer selects **all** objects referencing to `some_table`. Of course, you can filter the result for a desired type of object. – klin Sep 24 '15 at 10:12
1
 SELECT conrelid::regclass table_name
    FROM pg_constraint c
    WHERE c.confrelid = 'user'::regclass::oid
        AND c.confkey @> (
            SELECT array_agg(attnum)
            FROM pg_attribute
            WHERE attname = 'id'
                AND attrelid = c.confrelid
            )
        AND contype = 'f'

As per PostgreSQL Doc

pg_constraint :

The catalog pg_constraint stores check, primary key, unique, foreign key, and exclusion constraints on tables

contype : - c = check constraint, f = foreign key constraint, p = primary key constraint, u = unique constraint, t = constraint trigger, x = exclusion constraint,
conrelid : The table this constraint is on,
confrelid : If a foreign key, the referenced table,
conkey : If a table constraint (including foreign keys, but not constraint triggers), list of the constrained columns

Refer Object Identifier Types for oid and regclass

Vivek S.
  • 19,945
  • 7
  • 68
  • 85
0

try to use information_schema

for views:

select table_schema,table_name from information_schema.views where view_definition ilike '%users.id%';

for tables:

select table_schema,table_name from information_schema.columns where table_name='users' and column_name='id';

  • The second query only shows the PK field. To get the FK fields, you need to use `information_schema.referential_constraints`; see [here](http://stackoverflow.com/questions/3907879/sql-server-howto-get-foreign-key-reference-from-information-schema) – Nick Barnes Sep 24 '15 at 08:26