多个连接上的查询 Postgressql / DBeaver
我通常需要在多个连接 dev、prod 等上执行单个查询来比较输出。
通常我只是切换连接并复制/粘贴结果,以便进行比较。
现在我需要在超过 100 个连接上执行此操作,并且按照我以前的方式执行此操作将花费太多时间。
我查了很多,好像不太可能?我希望我错了?
我正在寻找的是能够做这样的事情
select 'boston prod',count(*) from persons --connection 1 (boston prod)
union all
select 'boston dev'count(*) from persons --connection 2 (boston dev)
union all
select 'new york prod'count(*) from persons --connection 3 (new york prod)
union all
select 'new york dev'count(*) from persons --connection 4 (new york dev )
来帮助指示数据来自第一列的位置可能是结果来自的连接名称。
我正在使用 Postgres v.10 和 DBeaver。
如果 DBeaver 无法做到这一点,是否还有其他工具可以做到这一点? 我可以在哪里选择更多连接并运行查询?
更新:所以使用 dblink 我能够做到这一点:
Select * from
dblink( 'host=boston_prod user=admin password=***** dbname=r2',
' select count(*) from persons ') as (Total_Persons integer)
union all
Select * from
dblink( 'host=boston_dev user=admin password=***** dbname=r2',
' select count(*) from persons ') as (Total_Persons integer)
工作正常。由于我有大约 100 多个连接,我需要添加我希望能够重用此脚本进行其他查询。
我想我可以用 select 和别名以数据类型作为参数来构建一个函数,如下所示。
当我可以让它工作时,我只需要付出努力并列出所有连接一次,就可以进行动态查询。
CREATE OR REPLACE FUNCTION custom.dblink_function(select varchar, alias varchar)
RETURNS TABLE(total_persons integer)
LANGUAGE sql
IMMUTABLE
AS $function$
Select * from
dblink( 'host=boston_prod user=admin password=***** dbname=r2',
'dblink_function.select') as (dblink_function.alias)
union all
Select * from
dblink( 'host=boston_dev user=admin password=***** dbname=r2',
'dblink_function.select') as (dblink_function.alias)
$function$
;
之后我想像这样调用该函数:
select * from custom.dblink_function('select count(*) from persons','Total_Persons integer')
遗憾的是它不起作用并且需要一些时间。有人可以帮助我吗?您对我的想法有何看法?
It is common that I need to execute a single query on multiple connections dev, prod, etc. to compare the output.
Normally I just switch connections and copy/paste results so I can compare.
Now I need to do this on over 100+ connections and it will take too much time to do this the same way as I use to.
I searched a lot and it seems it is not possible? I hope I'm wrong?
What I'm looking for is to be able to do something like this
select 'boston prod',count(*) from persons --connection 1 (boston prod)
union all
select 'boston dev'count(*) from persons --connection 2 (boston dev)
union all
select 'new york prod'count(*) from persons --connection 3 (new york prod)
union all
select 'new york dev'count(*) from persons --connection 4 (new york dev )
To help indicate where the data is from the first column could be the connection name that results came from.
I'm working with Postgres v.10 and DBeaver.
If it's not possible with DBeaver is there some other tool that can do this?
Where I can select more connections and run the query?
UPDATE: So with dblink i was able to do this:
Select * from
dblink( 'host=boston_prod user=admin password=***** dbname=r2',
' select count(*) from persons ') as (Total_Persons integer)
union all
Select * from
dblink( 'host=boston_dev user=admin password=***** dbname=r2',
' select count(*) from persons ') as (Total_Persons integer)
Works fine. Since i have like 100+ connections i need to add i want to be able to reuse this script for other queries.
I was thinking i could build a function with the select and the alias with datatype as parameters like this.
When i could get this to work i only need to make the effort and list all connections once and can have a dynamic query.
CREATE OR REPLACE FUNCTION custom.dblink_function(select varchar, alias varchar)
RETURNS TABLE(total_persons integer)
LANGUAGE sql
IMMUTABLE
AS $function$
Select * from
dblink( 'host=boston_prod user=admin password=***** dbname=r2',
'dblink_function.select') as (dblink_function.alias)
union all
Select * from
dblink( 'host=boston_dev user=admin password=***** dbname=r2',
'dblink_function.select') as (dblink_function.alias)
$function$
;
After that i want to call the function like this:
select * from custom.dblink_function('select count(*) from persons','Total_Persons integer')
Sadly it doesn't work and it needs some tweeks. Can someone help me and what you think about my idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论