有没有办法组织 Postgres 函数(使用 pgAdmin)?
Is there a way to organize Postgres Functions (using pgAdmin)?
我正在为 Windows
使用 pgAdmin 4.23、PostgreSQL 12.3
看起来所有功能都被转储到一个“文件夹”中。我安装了 uuid 和 tablefunc 扩展,它们与我自己的用户定义函数一起使用。至少这10个uuid都是以“uuid_”为前缀的。 11个tablefunc都是以"connectby", "crosstab"*, "normal_rand".
开头
我确实为自己的函数添加了前缀,这样至少可以将它们组合在一起。但是随着这个东西的增长和我添加扩展,我担心维护会变得更加困难。是否缺少某种子文件夹选项,或者命名约定是否是组织的正常方法?看起来存储过程的工作方式相同。
如果能够根据名称过滤函数,那就太好了。我看到“搜索对象”弹出窗口,但它不如过滤器有用。
要根据名称过滤函数,您可以使用此函数:
CREATE OR REPLACE FUNCTION public.find_function(fname text DEFAULT NULL::text)
RETURNS TABLE(routine_name text, routine_schema text, return_type text)
LANGUAGE sql
AS $function$
select routine_name, routine_schema, data_type
from information_schema.routines
where specific_schema not in ('pg_catalog', 'information_schema')
and case when fname is null then true else routine_name ~* fname end
order by routine_name;
$function$;
这是一个示例 - 查找名称中包含“test”的所有函数:
select * from find_function('test');
+-----------------------------+----------------+-------------+
| routine_name | routine_schema | return_type |
+-----------------------------+----------------+-------------+
| clear_web_tests | datavato | void |
+-----------------------------+----------------+-------------+
| etl_generic_tests | webaccess | text |
+-----------------------------+----------------+-------------+
| fill_web_tests | datavato | void |
+-----------------------------+----------------+-------------+
| pan_arguments_test | helpers | jsonb |
+-----------------------------+----------------+-------------+
| test_bizday | public | boolean |
+-----------------------------+----------------+-------------+
| test_checkdigits | public | boolean |
+-----------------------------+----------------+-------------+
| test_jasper_dynamic_columns | scratch | record |
+-----------------------------+----------------+-------------+
我在这里添加这个以防它有助于将来的搜索...
根据上面@horse_with_no_name 的建议和官方文档,我为我的第三方内容使用了一个单独的架构。以下是与我们公司其他数据库人员的对话:
I didn't want third party extensions (like uuid-ossp for Guids)
installed in my public schema since that is where we keep all of our
user defined functions specific to that database. When I originally
installed the extension I put it into the public schema, then just
transferred it to a schema named extfunc. Then we have a table named
usr that references extfunc.uuid_generate_v4() in one of the column
constraints. Everything works as expected.
However, when I try to backup our Dev db and restore to QA, the
pg_dump and pg_restore tasks do not handle it properly. The restore
would error and not create the usr table. The extension was not being
properly restored to extfunc, which prevented the usr table from being
created.
The solution is to create the extension in the desired schema from the
start. Do not create it and try and move it to the destination
schema. Backup/restore now work as expected.
我正在为 Windows
使用 pgAdmin 4.23、PostgreSQL 12.3看起来所有功能都被转储到一个“文件夹”中。我安装了 uuid 和 tablefunc 扩展,它们与我自己的用户定义函数一起使用。至少这10个uuid都是以“uuid_”为前缀的。 11个tablefunc都是以"connectby", "crosstab"*, "normal_rand".
开头我确实为自己的函数添加了前缀,这样至少可以将它们组合在一起。但是随着这个东西的增长和我添加扩展,我担心维护会变得更加困难。是否缺少某种子文件夹选项,或者命名约定是否是组织的正常方法?看起来存储过程的工作方式相同。
如果能够根据名称过滤函数,那就太好了。我看到“搜索对象”弹出窗口,但它不如过滤器有用。
要根据名称过滤函数,您可以使用此函数:
CREATE OR REPLACE FUNCTION public.find_function(fname text DEFAULT NULL::text)
RETURNS TABLE(routine_name text, routine_schema text, return_type text)
LANGUAGE sql
AS $function$
select routine_name, routine_schema, data_type
from information_schema.routines
where specific_schema not in ('pg_catalog', 'information_schema')
and case when fname is null then true else routine_name ~* fname end
order by routine_name;
$function$;
这是一个示例 - 查找名称中包含“test”的所有函数:
select * from find_function('test');
+-----------------------------+----------------+-------------+
| routine_name | routine_schema | return_type |
+-----------------------------+----------------+-------------+
| clear_web_tests | datavato | void |
+-----------------------------+----------------+-------------+
| etl_generic_tests | webaccess | text |
+-----------------------------+----------------+-------------+
| fill_web_tests | datavato | void |
+-----------------------------+----------------+-------------+
| pan_arguments_test | helpers | jsonb |
+-----------------------------+----------------+-------------+
| test_bizday | public | boolean |
+-----------------------------+----------------+-------------+
| test_checkdigits | public | boolean |
+-----------------------------+----------------+-------------+
| test_jasper_dynamic_columns | scratch | record |
+-----------------------------+----------------+-------------+
我在这里添加这个以防它有助于将来的搜索...
根据上面@horse_with_no_name 的建议和官方文档,我为我的第三方内容使用了一个单独的架构。以下是与我们公司其他数据库人员的对话:
I didn't want third party extensions (like uuid-ossp for Guids) installed in my public schema since that is where we keep all of our user defined functions specific to that database. When I originally installed the extension I put it into the public schema, then just transferred it to a schema named extfunc. Then we have a table named usr that references extfunc.uuid_generate_v4() in one of the column constraints. Everything works as expected.
However, when I try to backup our Dev db and restore to QA, the pg_dump and pg_restore tasks do not handle it properly. The restore would error and not create the usr table. The extension was not being properly restored to extfunc, which prevented the usr table from being created.
The solution is to create the extension in the desired schema from the start. Do not create it and try and move it to the destination schema. Backup/restore now work as expected.