PostgreSQL:在完成之前通过dblink获取查询的PID

问题描述 投票:1回答:1

我想监视我通过dblink dblink_send_query发送的查询,因此我需要它是PID,以便可以在pg表中搜索它。但是到目前为止,我只能在dblink完成之后通过返回值pg_backend_pid()来获取PID,或者通过查询字符串从pg_stat_activity中获取它来做相当笨拙,这不能说明多个相同的查询,或者对于文本较长的查询确实不理想。

那么,如何在完成之前获得PID?

select dblink_send_query('cn1', 'select pg_sleep(60)');
select dblink_send_query('cn2', 'select pg_sleep(60)');
select dblink_send_query('cn3', 'select pg_sleep(60)');

x86_64-pc-linux-gnu上的PostgreSQL 11.6,由gcc(GCC)4.8.5编译20150623(Red Hat 4.8.5-39),64位

postgresql pid dblink
1个回答
2
投票

后端进程ID在dblink连接的整个生命周期中都将保持不变。

因此,当您打开连接时,请先运行SELECT pg_backend_pid(),然后将结果保存在与连接关联的数据结构中。然后,您可以查询该数据结构以获得该连接的后端进程ID。

© www.soinside.com 2019 - 2024. All rights reserved.