无法访问PostgreSQL中的流复制统计信息

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

我有流媒体复制,我需要监控。所以Zabbix有一个特殊的用户。我不想使用pg_mongz并决定将自己的查询设置为pg_catalog schema的视图pg_stat_replication以获取复制状态。

当我使用查询时:

select * 
from pg_stat_replication;

它返回admin的复制状态记录。但是当我以监控用户身份登录时,它只返回:

pid, usesysid, usename, application_name

因此,诸如client_addr,client_hostname,client_port,backend_start,state,sent_location,write_location等参数为空。

首先,我在架构和表格上授予了我的用户权限:

grant usage on schema pg_catalog to usrmonitor;
grant select on all tables in schema pg_catalog to usrmonitor;

但它没有帮助。当我查看视图时,我发现查询使用函数并授予执行:

grant execute on function pg_stat_get_wal_senders() to usrmonitor;
grant execute on function pg_stat_get_activity(integer) to usrmonitor;

但是select查询仍然返回空列。可能是什么问题?

postgresql replication monitoring
2个回答
3
投票

是的,访问这些字段的目的仅限于超级用户。

作为解决方法,您可以将函数用作具有SECURITY DEFINER属性的代理:

SECURITY DEFINER指定使用创建它的用户的权限执行该功能。

所以作为超级用户(通常是postgres用户),请执行以下操作:

CREATE FUNCTION func_stat_replication() RETURNS SETOF pg_stat_replication as
$$ select * from pg_stat_replication; $$
LANGUAGE sql SECURITY DEFINER;

然后撤销/授予使用该功能的权限,以便只允许监视用户执行它:

REVOKE EXECUTE ON FUNCTION func_stat_replication() FROM public;
GRANT EXECUTE ON FUNCTION func_stat_replication() to usrmonitor;

那么usrmonitor应该执行:

 SELECT * FROM func_stat_replication();

它将具有与超级用户相同的结果。


0
投票

自PostgreSQL 10以来,它就像:

GRANT pg_monitor TO monitoring_user;

(来源:https://pganalyze.com/blog/whats-new-in-postgres-10-monitoring-improvements

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.