Good morning, I would like your help with setting up a materialized view which must contain information such as the schema name, the table name, the type (view, table, function, materialized view) and the endpoint for the API. I've done this before but only for views, tables and functions. Now I aim to also integrate materialized views. But the task seems more complicated than expected...
Here is the code for the first version which does not include materialized views:
SELECT DISTINCT
-- Use of the COALESCE function which will return the first non-null value dans la liste
COALESCE(t.table_catalog, r.routine_catalog) AS catalog,
COALESCE(t.table_schema, r.routine_schema) AS schema,
COALESCE(t.table_name, r.routine_name) AS name,
COALESCE(t.table_type, r.routine_type) AS type,
COALESCE(tp.grantee, rp.grantee) AS "user_name",
COALESCE('/' || t.table_name, '/rpc/' || r.routine_name) AS endpoint
----------------------
--------joins---------
----------------------
FROM
(SELECT * FROM information_schema.tables) AS t
FULL OUTER JOIN
(SELECT * FROM information_schema.routines) AS r
ON t.table_catalog = r.routine_catalog
AND t.table_schema = r.routine_schema
AND t.table_name = r.routine_name
LEFT OUTER JOIN information_schema.table_privileges AS tp
ON t.table_catalog = tp.table_catalog
AND t.table_schema = tp.table_schema
AND t.table_name = tp.table_name
LEFT OUTER JOIN information_schema.routine_privileges AS rp
ON r.routine_catalog = rp.routine_catalog
AND r.routine_schema = rp.routine_schema
AND r.routine_name = rp.routine_name
----------------------
------CONDITIONS------
----------------------
WHERE
(
-- Conditions for tables
-- Add conditions if applicable
t.table_schema != 'pg_catalog'
AND t.table_schema != 'information_schema'
AND t.table_schema != 'api_informations'
AND t.table_schema != 'public'
AND t.table_schema != 'cron'
AND tp.grantee != 'usersi_adm'
AND tp.grantee != 'organisationnel_adm'
)
OR (
-- Conditions for functions
-- Add conditions if applicable
(
r.routine_schema != 'pg_catalog'
AND r.routine_schema != 'presentation'
AND r.routine_schema != 'information_schema'
AND r.routine_schema != 'api_informations'
AND r.routine_schema != 'public'
AND r.routine_schema != 'cron'
AND rp.grantee != 'PUBLIC'
AND rp.grantee != 'usersi_adm'
)
);
Then I tried that, it's pretty good but I don't get the permissions back.:
SELECT
n.nspname AS schema_name,
c.relname AS object_name,
CASE
WHEN c.relkind = 'r' THEN 'BASE TABLE'
WHEN c.relkind = 'v' THEN 'VIEW'
WHEN c.relkind = 'm' THEN 'MATERIALIZED VIEW'
END AS object_type,
pg_get_userbyid(c.relowner) AS owner,
COALESCE('' || a.rolname, '') AS permissions,
CASE
WHEN c.relkind IN ('v', 'm') THEN format('/%s/%s', n.nspname, c.relname)
ELSE format('/%s', c.relname)
END AS object_location
FROM
pg_catalog.pg_class c
JOIN
pg_catalog.pg_namespace n ON c.relnamespace = n.oid
LEFT JOIN
pg_catalog.pg_auth_members m ON m.roleid = c.relowner
LEFT JOIN
pg_catalog.pg_roles a ON m.member = a.oid
WHERE
c.relkind IN ('r', 'v', 'm') AND
n.nspname NOT LIKE 'pg_%' AND
n.nspname NOT IN ('information_schema','public','api_informations');
So I need help to set up a code that will also contain the materialized views. Thank you in advance for your help.