Understanding why my Rails 4 app randomly shuts down

Over the past few months, my site (running on Rails 4 with Heroku) has not experienced any problems. Traffic was stable and response times below 500 ms.

By chance today, the response time has increased to 20,000 ms +.

Heroku response time

When I look at my logs, I noticed that ActiveRecord takes an unusually long time to run SELECT DISTINCT COUNT. This, apparently, caused slow loads.

(5029.1ms)  SELECT DISTINCT COUNT(DISTINCT "impressions"."session_hash") FROM "impressions" WHERE "impressions"."impressionable_id" = $1 AND "impressions"."impressionable_type" = $2  [["impressionable_id", 826], ["impressionable_type", "Article"]]

I want to say that this is caused by the gem of impressionism and my counting cache, which I use to display the number of articles, but I'm not sure.

Now I see this error in my logs for the first time:

PG::DuplicatePstatement: ERROR:  prepared statement "a9" already exists

, , . , . , , .

Rack Timeout Heroku. , , , . , - , .

:

(< 500 ). , .

SELECT DISTINCT COUNT , :

<%= @article.impressionist_count(filter: :session_hash) %>
+4
1

PG::DuplicatePstatement: ERROR: prepared statement "a9" already exists - , . , . . , . , pg gem.

, database.yml

production:
    adapter: postgresql
    prepared_statements: false

.

, COUNT Postgres , DISTINCT, , , . , , AUTOVACUUM. , AUTOVACUUM , , .. -, . 20% , . . VACUUM ANALYZE table_name; psql.

, autovacuum ( , , ..). - https://github.com/heroku/heroku-pg-extras heroku pg:vacuum-stats

0

All Articles