I have a problem in PL/PGSQL with functions.
Basically, I have a query:
SELECT nspname AS schemaname,relname,reltuples
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE
nspname NOT IN ('pg_catalog', 'information_schema') AND
relkind='r' and nspname='1'
ORDER BY relname;
This query derives database statistics of opened rows in tables. But our database has a huge amount of schemas. I have to write functions that iterate over all schemes with that query, getting the number of rows opened in tables in each scheme. I have tried to Google the problem but I couldn’t find any related examples.
You can define return value in your function, just remember to have correct datatypes or prepare to do some typecasting.
create or replace function get_my_database_status() returns TABLE(schemaname name, relname name, reltuples real)AS $$ BEGIN return Query (SELECT N.nspname AS schemaname, C.relname , C.reltuples FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE N.nspname NOT IN ('pg_catalog', 'information_schema') AND C.relkind='r' -- here maybe some filtering ORDER BY relname); END $$ language 'plpgsql';