PG_Depend Column Alias (view) to Source Column (table) mapping dependancy

322 Views Asked by At

I'm using version 8.2 Postresql

Have been trying to do this for days now, and I keep hitting the same wall. How to Display the following:

Source Table || Source_column || Target View || Target Column

Stg_table1|| customerid || vw_customer || Customer_details_id

The biggest problem I have is that I can't link the Alias names in the view, back to the Source_column. This is because the Ordinal position of the columns in the Source_table, is different to the columns in the Target_view.

I have tried using these scripts, with no luck (this joins the columns back on using information schema, and Ordinal Position)

    select distinct 
a.attname AS SOURCE
, a.attname::information_schema.sql_identifier AS column_name
--,  format_type(a.atttypid, NULL)  AS Source_Type
, d.refobjid::regclass AS Source_table 
, r.ev_class::regclass AS Target_View
--, pt.Typname AS TYPE, a.*
--, c.Column_name AS Target
--, c.Data_type
from pg_attribute as a
join pg_depend as d on d.refobjid = a.attrelid and d.refobjsubid = a.attnum
join pg_rewrite as r on d.objid = r.oid
--join information_schema.columns c ON a.attnum = c.ordinal_position --and r.ev_class::regclass = c.table_schema||'.'||c.table_name
--join pg_type as pt on a.atttypid = pt.oid
JOIN pg_class 
ON pg_class.oid = a.attrelid 
   AND a.attnum > 0
where
r.ev_class = 'abc.vw_customer'::regclass
and c.table_schema||'.'||c.table_name = 'vw_customer';

And I have also tried it this way:

SELECT distinct dependent.relname, pg_attribute.attname
, pg_attrdef.*
--, pg_attribute.attname::information_schema.sql_identifier AS column_name
--, pg_depend.objid, pg_attribute.*
FROM pg_depend 
JOIN pg_rewrite ON pg_depend.objid = pg_rewrite.oid 
JOIN pg_class as dependee ON pg_rewrite.ev_class = dependee.oid 
JOIN pg_class as dependent ON pg_depend.refobjid = dependent.oid 
JOIN pg_attribute ON pg_depend.refobjid = pg_attribute.attrelid 
    --AND pg_depend.refobjsubid = pg_attribute.attnum 
LEFT JOIN pg_attrdef ON pg_attribute.attrelid = pg_attrdef.adrelid AND pg_attribute.attnum = pg_attrdef.adnum   
WHERE dependee.relname = 'vw_customer'
--and attname in ('Customer_details_id', 'customerid')
AND pg_attribute.attnum > 0 
0

There are 0 best solutions below