Psql:使用*不*包含特定模式的df显示函数 [英] psql: display functions with df that do *not* contain a certain pattern
问题描述
因为我在(默认)公共模式中安装了dblink扩展,df
将显示所有与dblink相关的函数。我只想查看自定义功能。
到目前为止,我已经试过了。
df !~ *dblink*
df !~ *dblink*
df ! *dblink*
df !*dblink*
df ! *'dblink'
df !~* 'dblink'
df !~ 'dblink'
df !~ dblink
df dblink*
将获得以";dblink";开头的所有函数。我只想要相反的东西。
正则表达式链接:
https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP
PSQL链接:
https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-PATTERNS
推荐答案
您的要求
如何查看不包含文本的函数,
Regular expressions are supported in these patterns.而不添加类似!~*
的运算符。
遗憾的是排除了某些特殊字符。带有negative lookahead的regexp可以很好地解决您的请求。但运气不佳,因为这恰好涉及?
字符和引用the manual:
所有正则表达式特殊字符均按Section 9.7.3中指定的方式工作,但[...]、
?
除外,它被转换为.
[...]
除此之外,我只能将排除模式的字符类作为一种解决办法:
test=> df public.([^d]|.[^b]|..[^l]|...[^i]|....[^n]|.....[^k])*
显示架构public
中不以‘dblink’开头的所有表。它是一个具有分支:的正则表达式
- 第一个分支
[^d]
允许所有不以‘d’开头的名称, - 第二个分支
.[^b]
允许所有第二个字符不带‘b’
等
由于长字符串很难打字,您可以将其保存到一个psql变量中,并插入该变量:
test=> set dbx 'public.([^d]|.[^b]|..[^l]|...[^i]|....[^n]|.....[^k])*'
test=> df :dbx
您甚至可以将set
命令放入~/.psqlrc
文件中,以便将其自动加载到psql中。
SQL解决方案
也可以查询系统目录:
SELECT n.nspname AS "Schema", p.proname AS "Name"
, pg_catalog.pg_get_function_result(p.oid) AS "Result data type"
, pg_catalog.pg_get_function_arguments(p.oid) AS "Argument data types"
, CASE p.prokind WHEN 'a' THEN 'agg' WHEN 'w' THEN 'window' WHEN 'p' THEN 'proc' ELSE 'func' END AS "Type"
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE pg_function_is_visible (p.oid) -- only visible functions like plain df
AND n.nspname NOT LIKE 'pg\_%' -- exclude catalog schemas
AND p.proname NOT LIKE 'dblink%' -- exclude pattern
ORDER BY 1, 2, 4;
虽然它的用途要多得多,但它也更复杂。并且可能会在未来版本中更改...
更简单:扩展的单独架构
也就是说,干净的解决方案是避免混乱,将扩展安装到单独的架构中,如a_horse already hinted。此相关答案有详细说明:
如果您选择该路线,并使用名为extensions
的架构,则只需更改安装了dblink
的架构:
ALTER EXTENSION dblink SET SCHEMA extensions;
这篇关于Psql:使用*不*包含特定模式的df显示函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!