我将数据库从 AWS RDS 转储并恢复到 Azure Database for PostgreSQL - 灵活服务器(PostgreSQL 版本 14.13),尽管 PgHero 曾经正常工作,但在 Azure 中运行时会抛出权限被拒绝错误
bin/rake pghero:capture_query_stats
:
rake aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: permission denied for function pg_stat_statements_reset
CONTEXT: SQL function "pg_stat_statements_reset" statement 1
/usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:48:in `exec'
/usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:48:in `block (2 levels) in execute'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/dependencies/interlock.rb:41:in `permit_concurrent_loads'
/usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:47:in `block in execute'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/abstract_adapter.rb:765:in `block in log'
/usr/local/bundle/gems/activesupport-7.0.4.3/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/abstract_adapter.rb:756:in `log'
/usr/local/bundle/gems/activerecord-7.0.4.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:46:in `execute'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero/methods/basic.rb:104:in `execute'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero/methods/query_stats.rb:94:in `reset_instance_query_stats'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero/methods/query_stats.rb:152:in `block in capture_query_stats'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero/methods/query_stats.rb:151:in `each'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero/methods/query_stats.rb:151:in `capture_query_stats'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero.rb:195:in `block in capture_query_stats'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero.rb:264:in `block in each_database'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero.rb:262:in `each'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero.rb:262:in `each_database'
/usr/local/bundle/gems/pghero-3.3.3/lib/pghero.rb:192:in `capture_query_stats'
/usr/local/bundle/gems/pghero-3.3.3/lib/tasks/pghero.rake:4:in `block (2 levels) in <top (required)>'
该函数的创建语句为:
CREATE OR REPLACE FUNCTION pghero.pg_stat_statements_reset(userid oid, dbid oid, queryid bigint) RETURNS void AS
$$
SELECT public.pg_stat_statements_reset(userid, dbid, queryid);
$$ LANGUAGE sql VOLATILE SECURITY DEFINER;
我尝试删除架构
pghero
并按照权限指南重新创建它,但它一直抛出相同的错误。
我还尝试使用以下方法授予对该函数的访问权限:
GRANT EXECUTE ON FUNCTION pghero.pg_stat_statements_reset(oid, oid, bigint) TO pghero;
和
GRANT EXECUTE ON FUNCTION public.pg_stat_statements_reset(oid, oid, bigint) TO pghero;
如果我使用
psql
使用 pghero 用户连接到数据库,我可以毫无错误地执行该函数。
任何帮助表示赞赏。
ActiveRecord::StatementInvalid:PG::InsufficientPrivilege:错误:函数 pg_stat_statements_reset 的权限被拒绝
您收到的上述错误是因为
pg_stat_statements_reset
函数需要 超级用户权限。 pg_stat_statements_reset
此功能只能由超级用户执行。可以使用 GRANT
向其他人授予访问权限。
在 Azure Database for PostgreSQL - 灵活服务器中,出于安全原因,服务器管理员帐户无法使用超级用户权限。
您可以尝试使用的替代解决方案是使用查询存储:
在 Azure Database for PostgreSQL 中,灵活的服务器提供了一种不同的方式来监视查询执行统计信息,例如查询存储。为了防止性能开销,建议仅使用一种机制,即 pg_stat_statements 扩展或查询存储
要启用查询存储,请按照以下步骤操作:
转到 Azure Database for PostgreSQL - 灵活服务器 >> 在 设置部分中选择 服务器参数 >> 搜索
pg_qs.query_capture_mode
>> 根据需要将值设置为 TOP
/ ALL
>> 保存。