I have a function in all of my databases for “grepping” for text in various types of objects in the data dictionary. Here is the code:
CREATE OR REPLACE FUNCTION schema_grep(text)
RETURNS TABLE(schema name, object_name name, match_type text)
LANGUAGE sql
STABLE STRICT
AS $function$
SELECT x.*
FROM (
SELECT n.nspname, c.relname, relkind_to_text(c.relkind) || ' NAME' as match_type
FROM pg_class c, pg_namespace n WHERE c.relname ~* $1 and c.relnamespace = n.oid
UNION ALL
SELECT distinct n.nspname, c.relname, relkind_to_text(c.relkind) || ' ATTRIBUTE'
FROM pg_class c, pg_attribute a, pg_namespace n WHERE a.attrelid = c.oid and c.relkind <> 'v' and a.attname ~* $1 and c.relnamespace = n.oid
UNION ALL
SELECT trigger_schema, format('%s.%s', event_object_table, trigger_name), 'TRIGGER ACTION'
FROM information_schema.triggers WHERE action_statement ~* $1
UNION ALL
SELECT n.nspname, proname, 'FUNCTION SOURCE'
FROM pg_proc, pg_namespace n WHERE prosrc ~* $1 and pronamespace = n.oid
UNION ALL
SELECT n.nspname, proname, 'FUNCTION DEFINITION'
FROM pg_proc, pg_namespace n WHERE proname ~* $1 and pronamespace = n.oid
UNION ALL
SELECT schemaname, viewname, 'VIEW DEFINITION'
FROM pg_views WHERE definition ~* $1
UNION ALL
SELECT schemaname, matviewname, 'MATERIALIZED VIEW DEFINITION'
FROM pg_matviews WHERE definition ~* $1
UNION ALL
SELECT n.nspname, relname, 'CLASS CONSTRAINT'
FROM pg_class c, pg_constraint cx, pg_namespace n WHERE cx.conrelid = c.oid and pg_get_constraintdef(cx.oid) ~* $1 and relnamespace = n.oid
UNION ALL
SELECT distinct n.nspname, c.relname, 'COMMENT'
FROM pg_class c, pg_description d, pg_namespace n WHERE d.objoid = c.oid and d.description ~* $1 and c.relnamespace = n.oid
) x
$function$
I have one (and only one) database with a foreign data wrapper to another Postgres database. The relkind for this object is “f” – However, relkind_to_text returns null. Please see the following:
select distinct c.relkind, relkind_to_text(c.relkind) from pg_class as c;
+---------+-------------------+
| relkind | relkind_to_text |
+---------+-------------------+
| m | MATERIALIZED VIEW |
| f | NULL |
| t | TOAST TABLE |
| S | SEQUENCE |
| v | VIEW |
| i | INDEX |
| c | COMPOSITE TYPE |
| r | TABLE |
+---------+-------------------+
(8 rows)
I’m currently using PostgreSQL 14. I can certainly handle it in my schema_grep function, but is this a bug? Has it been fixed in later versions of Postgresql? Are there any other types of relkinds that return null I might need to code for if I make a change this this function?