Heroku + Apartment PG::Error: ERROR: function pg_stat_statements_reset() does not exist

主宰稳场 提交于 2019-12-09 16:32:45

问题


I use Apartment gem in Rails 4 to support multi-tenancy in Postgres 9.3.3 on Heroku.

An error is occurred when Apartment gem creates a new tenant.

Deep investigation showed that a schema was created, but no tables inside.

Heroku logs showed an error:

PG::Error: ERROR:  function pg_stat_statements_reset() does not exist

回答1:


When a new schema is created Postgres is trying to reset stats by executing the function pg_stat_statements_reset()

By default, this function can only be executed by superusers (from original doc).

Heroku doesn't give you superuser privileges. So you need to disable extension pg_stat_statements.

Solution 1 - Quick hot fix directly in DB

Execute SQL statement in schema public

DROP EXTENSION pg_stat_statements;

Solution 2 - via migration

1) Check the file db/schema.rb. Most probably it contains a line

enable_extension "pg_stat_statements"

2) Create a migration file

rails g migration DropExtensionPgStatStatements

3) define self.up method

def self.up
  disable_extension "pg_stat_statements"
end

4) apply the migration

rake db:migrate

5) Now the file db/schema.rb should not contain that line

6) Commit changes (schema and migration files) and deploy to Heroku

rake deploy:production:migrations 

Regarding the rake task see deploy.rake



来源:https://stackoverflow.com/questions/28452481/heroku-apartment-pgerror-error-function-pg-stat-statements-reset-does-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!