Giter Site home page Giter Site logo

pawurb / rails-pg-extras Goto Github PK

View Code? Open in Web Editor NEW
1.1K 8.0 30.0 536 KB

Rails PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more.

License: MIT License

Ruby 75.83% HTML 24.17%
rails postgresql activerecord

rails-pg-extras's Introduction

Rails PG Extras Gem Version GH Actions

Rails port of Heroku PG Extras with several additions and improvements. The goal of this project is to provide powerful insights into the PostgreSQL database for Ruby on Rails apps that are not using the Heroku PostgreSQL plugin.

Included rake tasks and Ruby methods can be used to obtain information about a Postgres instance, that may be useful when analyzing performance issues. This includes information about locks, index usage, buffer cache hit ratios and vacuum statistics. Ruby API enables developers to easily integrate the tool into e.g. automatic monitoring tasks.

You can read this blog post for detailed step by step tutorial on how to optimize PostgreSQL using PG Extras library.

Shameless plug: rails-pg-extras is one of the tools that I use when conducting Rails performance audits.

Optionally you can enable a visual interface:

Web interface

Alternative versions:

Installation

In your Gemfile

gem "rails-pg-extras"

calls and outliers queries require pg_stat_statements extension.

You can check if it is enabled in your database by running:

RailsPgExtras.extensions

You should see the similar line in the output:

| pg_stat_statements  | 1.7  | 1.7 | track execution statistics of all SQL statements executed |

ssl_used requires sslinfo extension, and buffercache_usage/buffercache_usage queries need pg_buffercache. You can enable them all by running:

RailsPgExtras.add_extensions

By default a primary ActiveRecord database connection is used for running metadata queries, rake tasks and web UI. To connect to a different database you can specify an ENV['RAILS_PG_EXTRAS_DATABASE_URL'] value in the following format:

ENV["RAILS_PG_EXTRAS_DATABASE_URL"] = "postgresql://postgres:secret@localhost:5432/database_name"

Usage

Each command can be used as a rake task, or a directly from the Ruby code.

rake pg_extras:cache_hit
RailsPgExtras.cache_hit
+----------------+------------------------+
|        Index and table hit rate         |
+----------------+------------------------+
| name           | ratio                  |
+----------------+------------------------+
| index hit rate | 0.97796610169491525424 |
| table hit rate | 0.96724294813466787989 |
+----------------+------------------------+

By default the ASCII table is displayed, to change to format you need to specify the in_format parameter ([:display_table, :hash, :array, :raw] options are available):

RailsPgExtras.cache_hit(in_format: :hash) =>

 [{"name"=>"index hit rate", "ratio"=>"0.97796610169491525424"}, {"name"=>"table hit rate", "ratio"=>"0.96724294813466787989"}]

RailsPgExtras.cache_hit(in_format: :array) =>

 [["index hit rate", "0.97796610169491525424"], ["table hit rate", "0.96724294813466787989"]]

RailsPgExtras.cache_hit(in_format: :raw) =>

 #<PG::Result:0x00007f75777f7328 status=PGRES_TUPLES_OK ntuples=2 nfields=2 cmd_tuples=2>

Some methods accept an optional args param allowing you to customize queries:

RailsPgExtras.long_running_queries(args: { threshold: "200 milliseconds" })

By default, queries target the public schema of the database. You can specify a different schema by passing the schema argument:

RailsPgExtras.table_cache_hit(args: { schema: "my_schema" })

You can customize the default public schema by setting ENV['PG_EXTRAS_SCHEMA'] value.

Diagnose report

The simplest way to start using pg-extras is to execute a diagnose method. It runs a set of checks and prints out a report highlighting areas that may require additional investigation:

RailsPgExtras.diagnose

$ rake pg_extras:diagnose

Diagnose report

Keep reading to learn about methods that diagnose uses under the hood.

Visual interface

You can enable UI using a Rails engine by adding the following code in config/routes.rb:

  mount RailsPgExtras::Web::Engine, at: 'pg_extras'

You can enable HTTP basic auth by specifying RAILS_PG_EXTRAS_USER and RAILS_PG_EXTRAS_PASSWORD variables. Authentication is mandatory unless you specify RAILS_PG_EXTRAS_PUBLIC_DASHBOARD=true or setting RailsPgExtras.configuration.public_dashboard to true.

You can configure available web actions in config/initializers/rails_pg_extras.rb:

RailsPgExtras.configure do |config|
  # Rails-pg-extras does not enable all the web actions by default. You can check all available actions via `RailsPgExtras::Web::ACTIONS`.
  # For example, you may want to enable the dangerous `kill_all` action.

  config.enabled_web_actions = %i[kill_all pg_stat_statements_reset add_extensions]
end

Available methods

measure_queries

This method displays query types executed when running a provided Ruby snippet, with their avg., min., max., and total duration in miliseconds. It also outputs info about the snippet execution duration and the portion spent running SQL queries (total_duration/sql_duration). It can help debug N+1 issues and review the impact of configuring eager loading:

RailsPgExtras.measure_queries { User.limit(10).map(&:team) }

{:count=>11,
 :queries=>
  {"SELECT \"users\".* FROM \"users\" LIMIT $1"=>
    {:count=>1,
     :total_duration=>1.9,
     :min_duration=>1.9,
     :max_duration=>1.9,
     :avg_duration=>1.9},
   "SELECT \"teams\".* FROM \"teams\" WHERE \"teams\".\"id\" = $1 LIMIT $2"=>
    {:count=>10,
     :total_duration=>0.94,
     :min_duration=>0.62,
     :max_duration=>1.37,
     :avg_duration=>0.94}},
 :total_duration=>13.35,
 :sql_duration=>11.34}

RailsPgExtras.measure_queries { User.limit(10).includes(:team).map(&:team) }

{:count=>2,
 :queries=>
  {"SELECT \"users\".* FROM \"users\" LIMIT $1"=>
    {:count=>1,
     :total_duration=>3.43,
     :min_duration=>3.43,
     :max_duration=>3.43,
    :avg_duration=>3.43},
   "SELECT \"teams\".* FROM \"teams\" WHERE \"teams\".\"id\" IN ($1, $2, $3, $4, $5, $6, $7, $8)"=>
    {:count=>1,
     :total_duration=>2.59,
     :min_duration=>2.59,
     :max_duration=>2.59,
     :avg_duration=>2.59}},
 :total_duration=>9.75,
 :sql_duration=>6.02}

Optionally, by including Marginalia gem and configuring it to display query backtraces:

config/development.rb

Marginalia::Comment.components = [:line]

you can add this info to the output:

Marginalia logs

table_info

This method displays metadata metrics for all or a selected table. You can use it to check the table's size, its cache hit metrics, and whether it is correctly indexed. Many sequential scans or no index scans are potential indicators of misconfigured indexes. This method aggregates data provided by other methods in an easy to analyze summary format.

RailsPgExtras.table_info(args: { table_name: "users" })

| Table name | Table size | Table cache hit   | Indexes cache hit  | Estimated rows | Sequential scans | Indexes scans |
+------------+------------+-------------------+--------------------+----------------+------------------+---------------+
| users      | 2432 kB    | 0.999966685701511 | 0.9988780464661853 | 16650          | 2128             | 512496        |

index_info

This method returns summary info about database indexes. You can check index size, how often it is used and what percentage of its total size are NULL values. Like the previous method, it aggregates data from other helper methods in an easy-to-digest format.

RailsPgExtras.index_info(args: { table_name: "users" })

| Index name                    | Table name | Columns        | Index size | Index scans | Null frac |
+-------------------------------+------------+----------------+------------+-------------+-----------+
| users_pkey                    | users      | id             | 1152 kB    | 163007      | 0.00%     |
| index_users_on_slack_id       | users      | slack_id       | 1080 kB    | 258870      | 0.00%     |
| index_users_on_team_id        | users      | team_id        | 816 kB     | 70962       | 0.00%     |
| index_users_on_uuid           | users      | uuid           | 1032 kB    | 0           | 0.00%     |
| index_users_on_block_uuid     | users      | block_uuid     | 776 kB     | 19502       | 100.00%   |
| index_users_on_api_auth_token | users      | api_auth_token | 1744 kB    | 156         | 0.00%     |

cache_hit

RailsPgExtras.cache_hit

$ rake pg_extras:cache_hit

      name      |         ratio
----------------+------------------------
 index hit rate | 0.99957765013541945832
 table hit rate |                   1.00
(2 rows)

This command provides information on the efficiency of the buffer cache, for both index reads (index hit rate) as well as table reads (table hit rate). A low buffer cache hit ratio can be a sign that the Postgres instance is too small for the workload.

More info

index_cache_hit

RailsPgExtras.index_cache_hit

$ rake pg_extras:index_cache_hit

| name                  | buffer_hits | block_reads | total_read | ratio             |
+-----------------------+-------------+-------------+------------+-------------------+
| teams                 | 187665      | 109         | 187774     | 0.999419514948821 |
| subscriptions         | 5160        | 6           | 5166       | 0.99883855981417  |
| plans                 | 5718        | 9           | 5727       | 0.998428496595076 |
(truncated results for brevity)

The same as cache_hit with each table's indexes cache hit info displayed separately.

More info

table_cache_hit

RailsPgExtras.table_cache_hit

$ rake pg_extras:table_cache_hit

| name                  | buffer_hits | block_reads | total_read | ratio             |
+-----------------------+-------------+-------------+------------+-------------------+
| plans                 | 32123       | 2           | 32125      | 0.999937743190662 |
| subscriptions         | 95021       | 8           | 95029      | 0.999915815172211 |
| teams                 | 171637      | 200         | 171837     | 0.99883610631005  |
(truncated results for brevity)

The same as cache_hit with each table's cache hit info displayed seperately.

More info

db_settings

RailsPgExtras.db_settings

$ rake pg_extras:db_settings

             name             | setting | unit |
------------------------------+---------+------+
 checkpoint_completion_target | 0.7     |      |
 default_statistics_target    | 100     |      |
 effective_cache_size         | 1350000 | 8kB  |
 effective_io_concurrency     | 1       |      |
(truncated results for brevity)

This method displays values for selected PostgreSQL settings. You can compare them with settings recommended by PGTune and tweak values to improve performance.

More info

ssl_used

RailsPgExtras.ssl_used

| ssl_is_used                     |
+---------------------------------+
| t                               |

Returns boolean indicating if an encrypted SSL is currently used. Connecting to the database via an unencrypted connection is a critical security risk.

index_usage

RailsPgExtras.index_usage

$ rake pg_extras:index_usage

       relname       | percent_of_times_index_used | rows_in_table
---------------------+-----------------------------+---------------
 events              |                          65 |       1217347
 app_infos           |                          74 |        314057
 app_infos_user_info |                           0 |        198848
 user_info           |                           5 |         94545
 delayed_jobs        |                          27 |             0
(5 rows)

This command provides information on the efficiency of indexes, represented as what percentage of total scans were index scans. A low percentage can indicate under indexing, or wrong data being indexed.

locks

RailsPgExtras.locks(args: { limit: 20 })

$ rake pg_extras:locks

 procpid | relname | transactionid | granted |     query_snippet     | mode             |       age        |   application |
---------+---------+---------------+---------+-----------------------+------------------------------------------------------
   31776 |         |               | t       | <IDLE> in transaction | ExclusiveLock    |  00:19:29.837898 |  bin/rails
   31776 |         |          1294 | t       | <IDLE> in transaction | RowExclusiveLock |  00:19:29.837898 |  bin/rails
   31912 |         |               | t       | select * from hello;  | ExclusiveLock    |  00:19:17.94259  |  bin/rails
    3443 |         |               | t       |                      +| ExclusiveLock    |  00:00:00        |  bin/sidekiq
         |         |               |         |    select            +|                  |                  |
         |         |               |         |      pg_stat_activi   |                  |                  |
(4 rows)

This command displays queries that have taken out an exclusive lock on a relation. Exclusive locks typically prevent other operations on that relation from taking place, and can be a cause of "hung" queries that are waiting for a lock to be granted.

More info

all_locks

RailsPgExtras.all_locks

$ rake pg_extras:all_locks

This command displays all the current locks, regardless of their type.

outliers

RailsPgExtras.outliers(args: { limit: 20 })

$ rake pg_extras:outliers

                   qry                   |    exec_time     | prop_exec_time |   ncalls    | sync_io_time
-----------------------------------------+------------------+----------------+-------------+--------------
 SELECT * FROM archivable_usage_events.. | 154:39:26.431466 | 72.2%          | 34,211,877  | 00:00:00
 COPY public.archivable_usage_events (.. | 50:38:33.198418  | 23.6%          | 13          | 13:34:21.00108
 COPY public.usage_events (id, reporte.. | 02:32:16.335233  | 1.2%           | 13          | 00:34:19.784318
 INSERT INTO usage_events (id, retaine.. | 01:42:59.436532  | 0.8%           | 12,328,187  | 00:00:00
 SELECT * FROM usage_events WHERE (alp.. | 01:18:10.754354  | 0.6%           | 102,114,301 | 00:00:00
 UPDATE usage_events SET reporter_id =.. | 00:52:35.683254  | 0.4%           | 23,786,348  | 00:00:00
(truncated results for brevity)

This command displays statements, obtained from pg_stat_statements, ordered by the amount of time to execute in aggregate. This includes the statement itself, the total execution time for that statement, the proportion of total execution time for all statements that statement has taken up, the number of times that statement has been called, and the amount of time that statement spent on synchronous I/O (reading/writing from the file system).

Typically, an efficient query will have an appropriate ratio of calls to total execution time, with as little time spent on I/O as possible. Queries that have a high total execution time but low call count should be investigated to improve their performance. Queries that have a high proportion of execution time being spent on synchronous I/O should also be investigated.

More info

calls

RailsPgExtras.calls(args: { limit: 10 })

$ rake pg_extras:calls

                   qry                   |    exec_time     | prop_exec_time |   ncalls    | sync_io_time
-----------------------------------------+------------------+----------------+-------------+--------------
 SELECT * FROM usage_events WHERE (alp.. | 01:18:11.073333  | 0.6%           | 102,120,780 | 00:00:00
 BEGIN                                   | 00:00:51.285988  | 0.0%           | 47,288,662  | 00:00:00
 COMMIT                                  | 00:00:52.31724   | 0.0%           | 47,288,615  | 00:00:00
 SELECT * FROM  archivable_usage_event.. | 154:39:26.431466 | 72.2%          | 34,211,877  | 00:00:00
 UPDATE usage_events SET reporter_id =.. | 00:52:35.986167  | 0.4%           | 23,788,388  | 00:00:00
(truncated results for brevity)

This command is much like pg:outliers, but ordered by the number of times a statement has been called.

More info

blocking

RailsPgExtras.blocking

$ rake pg_extras:blocking

 blocked_pid |    blocking_statement    | blocking_duration | blocking_pid |                                        blocked_statement                           | blocked_duration
-------------+--------------------------+-------------------+--------------+------------------------------------------------------------------------------------+------------------
         461 | select count(*) from app | 00:00:03.838314   |        15682 | UPDATE "app" SET "updated_at" = '2013-03-04 15:07:04.746688' WHERE "id" = 12823149 | 00:00:03.821826
(1 row)

This command displays statements that are currently holding locks that other statements are waiting to be released. This can be used in conjunction with pg:locks to determine which statements need to be terminated in order to resolve lock contention.

More info

total_index_size

RailsPgExtras.total_index_size

$ rake pg_extras:total_index_size

  size
-------
 28194 MB
(1 row)

This command displays the total size of all indexes on the database, in MB. It is calculated by taking the number of pages (reported in relpages) and multiplying it by the page size (8192 bytes).

index_size

RailsPgExtras.index_size

$ rake pg_extras:index_size
                             name                              |  size   | schema |
---------------------------------------------------------------+-------------------
 idx_activity_attemptable_and_type_lesson_enrollment           | 5196 MB | public |
 index_enrollment_attemptables_by_attempt_and_last_in_group    | 4045 MB | public |
 index_attempts_on_student_id                                  | 2611 MB | public |
 enrollment_activity_attemptables_pkey                         | 2513 MB | custom |
 index_attempts_on_student_id_final_attemptable_type           | 2466 MB | custom |
 attempts_pkey                                                 | 2466 MB | custom |
 index_attempts_on_response_id                                 | 2404 MB | public |
 index_attempts_on_enrollment_id                               | 1957 MB | public |
 index_enrollment_attemptables_by_enrollment_activity_id       | 1789 MB | public |
 enrollment_activities_pkey                                    |  458 MB | public |
(truncated results for brevity)

This command displays the size of each each index in the database, in MB. It is calculated by taking the number of pages (reported in relpages) and multiplying it by the page size (8192 bytes).

table_size

RailsPgExtras.table_size

$ rake pg_extras:table_size

                             name                              |  size   | schema |
---------------------------------------------------------------+-------------------
 learning_coaches                                              |  196 MB | public |
 states                                                        |  145 MB | public |
 grade_levels                                                  |  111 MB | custom |
 charities_customers                                           |   73 MB | custom |
 charities                                                     |   66 MB | public |
(truncated results for brevity)

This command displays the size of each table and materialized view in the database, in MB. It is calculated by using the system administration function pg_table_size(), which includes the size of the main data fork, free space map, visibility map and TOAST data.

table_indexes_size

RailsPgExtras.table_indexes_size

$ rake pg_extras:table_indexes_size

                             table                             | indexes_size
---------------------------------------------------------------+--------------
 learning_coaches                                              |    153 MB
 states                                                        |    125 MB
 charities_customers                                           |     93 MB
 charities                                                     |     16 MB
 grade_levels                                                  |     11 MB
(truncated results for brevity)

This command displays the total size of indexes for each table and materialized view, in MB. It is calculated by using the system administration function pg_indexes_size().

total_table_size

RailsPgExtras.total_table_size

$ rake pg_extras:total_table_size

                             name                              |  size
---------------------------------------------------------------+---------
 learning_coaches                                              |  349 MB
 states                                                        |  270 MB
 charities_customers                                           |  166 MB
 grade_levels                                                  |  122 MB
 charities                                                     |   82 MB
(truncated results for brevity)

This command displays the total size of each table and materialized view in the database, in MB. It is calculated by using the system administration function pg_total_relation_size(), which includes table size, total index size and TOAST data.

unused_indexes

RailsPgExtras.unused_indexes(args: { max_scans: 20 })

$ rake pg_extras:unused_indexes

          table      |                       index                | index_size | index_scans
---------------------+--------------------------------------------+------------+-------------
 public.grade_levels | index_placement_attempts_on_grade_level_id | 97 MB      |           0
 public.observations | observations_attrs_grade_resources         | 33 MB      |           0
 public.messages     | user_resource_id_idx                       | 12 MB      |           0
(3 rows)

This command displays indexes that have < 50 scans recorded against them, and are greater than 5 pages in size, ordered by size relative to the number of index scans. This command is generally useful for eliminating indexes that are unused, which can impact write performance, as well as read performance should they occupy space in memory.

More info

duplicate_indexes

RailsPgExtras.duplicate_indexes

| size       |  idx1        |  idx2          |  idx3    |  idx4     |
+------------+--------------+----------------+----------+-----------+
| 128 k      | users_pkey   | index_users_id |          |           |

This command displays multiple indexes that have the same set of columns, same opclass, expression and predicate - which make them equivalent. Usually it's safe to drop one of them.

null_indexes

RailsPgExtras.null_indexes(args: { min_relation_size_mb: 10 })

$ rake pg_extras:null_indexes

   oid   |         index      | index_size | unique | indexed_column | null_frac | expected_saving
---------+--------------------+------------+--------+----------------+-----------+-----------------
  183764 | users_reset_token  | 1445 MB    | t      | reset_token    |   97.00%  | 1401 MB
   88732 | plan_cancelled_at  | 539 MB     | f      | cancelled_at   |    8.30%  | 44 MB
 9827345 | users_email        | 18 MB      | t      | email          |   28.67%  | 5160 kB

This command displays indexes that contain NULL values. A high ratio of NULL values means that using a partial index excluding them will be beneficial in case they are not used for searching.

More info

seq_scans

RailsPgExtras.seq_scans

$ rake pg_extras:seq_scans

               name                |  count
-----------------------------------+----------
 learning_coaches                  | 44820063
 states                            | 36794975
 grade_levels                      | 13972293
 charities_customers               |  8615277
 charities                         |  4316276
 messages                          |  3922247
 contests_customers                |  2915972
 classroom_goals                   |  2142014
(truncated results for brevity)

This command displays the number of sequential scans recorded against all tables, descending by count of sequential scans. Tables that have very high numbers of sequential scans may be under-indexed, and it may be worth investigating queries that read from these tables.

More info

long_running_queries

RailsPgExtras.long_running_queries(args: { threshold: "200 milliseconds" })

$ rake pg_extras:long_running_queries

  pid  |    duration     |                                      query
-------+-----------------+---------------------------------------------------------------------------------------
 19578 | 02:29:11.200129 | EXPLAIN SELECT  "students".* FROM "students"  WHERE "students"."id" = 1450645 LIMIT 1
 19465 | 02:26:05.542653 | EXPLAIN SELECT  "students".* FROM "students"  WHERE "students"."id" = 1889881 LIMIT 1
 19632 | 02:24:46.962818 | EXPLAIN SELECT  "students".* FROM "students"  WHERE "students"."id" = 1581884 LIMIT 1
(truncated results for brevity)

This command displays currently running queries, that have been running for longer than 5 minutes, descending by duration. Very long running queries can be a source of multiple issues, such as preventing DDL statements completing or vacuum being unable to update relfrozenxid.

records_rank

RailsPgExtras.records_rank

$ rake pg_extras:records_rank

               name                | estimated_count
-----------------------------------+-----------------
 tastypie_apiaccess                |          568891
 notifications_event               |          381227
 core_todo                         |          178614
 core_comment                      |          123969
 notifications_notification        |          102101
 django_session                    |           68078
 (truncated results for brevity)

This command displays an estimated count of rows per table, descending by estimated count. The estimated count is derived from n_live_tup, which is updated by vacuum operations. Due to the way n_live_tup is populated, sparse vs. dense pages can result in estimations that are significantly out from the real count of rows.

bloat

RailsPgExtras.bloat

$ rake pg_extras:bloat

 type  | schemaname |           object_name         | bloat |   waste
-------+------------+-------------------------------+-------+----------
 table | public     | bloated_table                 |   1.1 | 98 MB
 table | public     | other_bloated_table           |   1.1 | 58 MB
 index | public     | bloated_table::bloated_index  |   3.7 | 34 MB
 table | public     | clean_table                   |   0.2 | 3808 kB
 table | public     | other_clean_table             |   0.3 | 1576 kB
 (truncated results for brevity)

This command displays an estimation of table "bloat" โ€“ space allocated to a relation that is full of dead tuples, that has yet to be reclaimed. Tables that have a high bloat ratio, typically 10 or greater, should be investigated to see if vacuuming is aggressive enough, and can be a sign of high table churn.

More info

vacuum_stats

RailsPgExtras.vacuum_stats

$ rake pg_extras:vacuum_stats

 schema |         table         | last_vacuum | last_autovacuum  |    rowcount    | dead_rowcount  | autovacuum_threshold | expect_autovacuum
--------+-----------------------+-------------+------------------+----------------+----------------+----------------------+-------------------
 public | log_table             |             | 2013-04-26 17:37 |         18,030 |              0 |          3,656       |
 public | data_table            |             | 2013-04-26 13:09 |             79 |             28 |             66       |
 public | other_table           |             | 2013-04-26 11:41 |             41 |             47 |             58       |
 public | queue_table           |             | 2013-04-26 17:39 |             12 |          8,228 |             52       | yes
 (truncated results for brevity)

This command displays statistics related to vacuum operations for each table, including an estimation of dead rows, last autovacuum and the current autovacuum threshold. This command can be useful when determining if current vacuum thresholds require adjustments, and to determine when the table was last vacuumed.

kill_all

RailsPgExtras.kill_all

This commands kills all the currently active connections to the database. It can be useful as a last resort when your database is stuck in a deadlock.

kill_pid

RailsPgExtras.kill_pid(args: { pid: 4657 })

This commands kills currently active database connection by its pid number. You can use connections method to find the correct pid values.

pg_stat_statements_reset

RailsPgExtras.pg_stat_statements_reset

This command discards all statistics gathered so far by pg_stat_statements.

buffercache_stats

RailsPgExtras.buffercache_stats(args: { limit: 10 })

This command shows the relations buffered in database share buffer, ordered by percentage taken. It also shows that how much of the whole relation is buffered.

buffercache_usage

RailsPgExtras.buffercache_usage(args: { limit: 20 })

This command calculates how many blocks from which table are currently cached.

extensions

RailsPgExtras.extensions

$ rake pg_extras:extensions

| pg_stat_statements | 1.7 | 1.7 | track execution statistics of all SQL statements executed
 (truncated results for brevity)

This command lists all the currently installed and available PostgreSQL extensions.

connections

RailsPgExtras.connections

+----------------------------------------------------------------+
|      Returns the list of all active database connections       |
+------------------+--------------------------+------------------+
| username | pid   | client_address           | application_name |
+------------------+--------------------------+------------------+
| postgres | 15962 | 172.31.69.166/32         | sidekiq          |
| postgres | 16810 | 172.31.69.166/32         | bin/rails        |
+------------------+--------------------------+------------------+

This command returns the list of all active database connections.

mandelbrot

RailsPgExtras.mandelbrot

$ rake pg_extras:mandelbrot

This command outputs the Mandelbrot set, calculated through SQL.

Testing

cp docker-compose.yml.sample docker-compose.yml
docker compose up -d
rake test_all

Query sources

rails-pg-extras's People

Contributors

andyatkinson avatar chriscpo avatar defkode avatar drnic avatar ivanhuanghhh avatar josephfrazier avatar morgoth avatar nickjj avatar pawurb avatar petergoldstein avatar ppiotrowicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-pg-extras's Issues

Psych::BadAlias: Unknown alias: default

Steps to reproduce

  1. Create a blank Rails app
  2. Configure it to use PostgreSQL and this gem
  3. Create the database with bin/rails db:create
  4. Run bin/rails pg_extras:diagnose

Here it is a config/database.yml example to reproduce the issue:

default: &default
  adapter: postgresql
  host: localhost
  encoding: unicode
  password: password
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: user

development:
  <<: *default
  database: rails_development

test:
  <<: *default
  database: rails_test

production:
  <<: *default
  database: rails

Expected behavior

Expected behavior

Actual behavior

$ bin/rails pg_extras:diagnose
rails aborted!
Psych::BadAlias: Unknown alias: default
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rails-pg-extras-4.0.1/lib/rails_pg_extras/tasks/all.rake:11:in `block (2 levels) in <main>'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `block in execute'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `each'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:281:in `execute'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:243:in `block in invoke_prerequisites'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `each'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:241:in `invoke_prerequisites'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:218:in `block in invoke_with_call_chain'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `synchronize'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:199:in `invoke_with_call_chain'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/task.rb:188:in `invoke'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:160:in `invoke_task'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `each'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:116:in `block in top_level'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:125:in `run_with_threads'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:110:in `top_level'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/railties-7.0.2.3/lib/rails/commands/rake/rake_command.rb:24:in `block (2 levels) in perform'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/railties-7.0.2.3/lib/rails/commands/rake/rake_command.rb:24:in `block in perform'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:59:in `with_application'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/railties-7.0.2.3/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/railties-7.0.2.3/lib/rails/command.rb:51:in `invoke'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/railties-7.0.2.3/lib/rails/commands.rb:18:in `<main>'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/workspaces/rails-7/.bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
Tasks: TOP => pg_extras:diagnose => pg_extras:establish_connection
(See full trace by running task with --trace)

System configuration

Rails version: 7.0.2.3

Ruby version: 3.1.1

RailsPgExtras.diagnose taking too long and not finishing production.

I ran this gem locally in my system (on my local database) and did give me a lot of insights. I wanted to add it to production and share it with the team. But the RailsPgExtras.diagnose is not finsihing (and the web ui is not loading). The production DB is like a 100 GB, comapred to my local DB (for 1 or 2 GB).

Its been running for over 20 minutes and still not finishing. (Also not sure if its relavent, we have 100s of schema's as well).

ArgumentError: unknown keyword: :aliases

When running the rake task locally I get an error instead of output.

able to reproduce issue on:
ruby 2.7.6p219
Rails 6.0.4.7
gem version: 4.7.0

% rake pg_extras:diagnose
rake aborted!
ArgumentError: unknown keyword: :aliases

Tasks: TOP => pg_extras:diagnose => pg_extras:establish_connection
(See full trace by running task with --trace)

Using safe_load over load seems to address this, but I think there is a compatibility issues then.

Build in UI Dashboard

I'm trying to use the new dashboard in a api_only = true Rails app and it seems the UI needs full-fledged app in order to use Views only methods.
I'm getting an error: undefined method "flash"

Would it be possible to make this work with an API only app? would be super helpful

Thanks

PS: did some testing and it appears the problem is if flash[:notice] inside views/layouts/rails_pg_extras/web/application.html.erb if those lines are deleted than it works okay.
would it be possible to check if the flash method is available? that would allow us to use this in API mode

Multiple Databases

Thank you for this awesome gem! Can it be used to manage multiple databases (for example: master/replica)?

Why Rails?

More of a question than anything. Nothing about this gem seems to be particularly "Railsy". I'm wondering why the decision was made to couple to Rails? The only thing you need is a onnection \o a database (which you can achieve right pg on its own). You could still offer a railtie for those using rails and also make this usable for everyone else in the ruby world.
Any interest?

uninitialized constant RailsPgExtras::Web::QueriesController::ACTIONS

Hey everyone, I was using the gem and decided to mount the Web routes to use the dashboard. However, I got an uninitialized constant RailsPgExtras::Web::QueriesController::ACTIONS error when accessing the route.

I believe that is happening because my application also has a definition of ApplicationController and thus the QueriesController might be inheriting from that instead of the one in rails_pg_extras/web. Mostly because inheritance does not have the module scope in it;
https://github.com/pawurb/rails-pg-extras/blob/master/app/controllers/rails_pg_extras/web/queries_controller.rb#L2-L4

I got the route to work after a monkey-patch where I changed the inheritance to inherit explicitly from the web module.

  class QueriesController < RailsPgExtras::Web::ApplicationController

I'm wondering now what's the most appropriate way to fix it. If there would be something to do on the application side. Or if we should change the gem's code somehow given ApplicationController is a pretty common name. Happy to hear anyone else thoughts on this too.

Results for non-public schemas

I don't think I'm seeing results for tables outside of public schema.

For example, we only have one table in public:

# \dt;
            List of relations
 Schema |      Name       | Type  | Owner
--------+-----------------+-------+-------
 public | spatial_ref_sys | table | drnic

And lots of tables in other schemas:

$ \dt other_schema.*;
    Schema    |                   Name                    |       Type        | Owner
--------------+-------------------------------------------+-------------------+-------
 other_schema | account                                   | table             | drnic
 other_schema | asset                                     | table             | drnic
 other_schema | booking_holds                             | table             | drnic
 ...

But the various commands, in rake/console/web, only return results for tables in public:

image

Is there a way to configure it to look in an alternate schema, or better, all the schemas in the target database? Thanks ๐Ÿ‘

rake pg_extras:outliers fails on Postgres 9.5.21 & Ruby 2.6.6

โžฒโžฒ๐Ÿ’ป  rake pg_extras:outliers
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "pg_stat_statements" does not exist
LINE 8: FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM...
             ^
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras.rb:19:in `run_query'
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras.rb:11:in `block (2 levels) in <module:RailsPGExtras>'
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras/tasks/all.rake:9:in `public_send'
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras/tasks/all.rake:9:in `block (3 levels) in <main>'
/Users/brandon/.rbenv/versions/2.6.6/bin/bundle:23:in `load'
/Users/brandon/.rbenv/versions/2.6.6/bin/bundle:23:in `<main>'

Caused by:
PG::UndefinedTable: ERROR:  relation "pg_stat_statements" does not exist
LINE 8: FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM...
             ^
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras.rb:19:in `run_query'
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras.rb:11:in `block (2 levels) in <module:RailsPGExtras>'
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras/tasks/all.rake:9:in `public_send'
/usr/local/Cellar/tmuxinator/2.0.1/libexec/gems/rails-pg-extras-1.2.1/lib/rails-pg-extras/tasks/all.rake:9:in `block (3 levels) in <main>'
/Users/brandon/.rbenv/versions/2.6.6/bin/bundle:23:in `load'
/Users/brandon/.rbenv/versions/2.6.6/bin/bundle:23:in `<main>'
Tasks: TOP => pg_extras:outliers
(See full trace by running task with --trace)

Documentation causes error

Hi,

first of all, awesome Gem! This will help us a lot, to optimize our solution.
Thank you also for the examples, but some of them are outdated.

There is for example the Info about Deadlocks
Here you using RubyPgExtras instead of RailsPgExtras. This causes the Error:

PG::Error (invalid URI query parameter: "encoding")

Maybe you can adjust your documentation.

Best.

Diagnose does not accept args options like elsewhere

Since I could run:

bin/rails runner 'RailsPgExtras.table_cache_hit(args: { schema: "rideshare" })'

to check the table_cache_hit rate, I thought I could do something similar for diagnose, specifying a custom schema, but that's not supported:

bin/rails runner 'RailsPgExtras.diagnose(args: { schema: "rideshare" })'

....
rails-pg-extras-5.3.0/lib/rails-pg-extras.rb:53:in `diagnose': unknown keyword: :args (ArgumentError)
        from /Users/andy/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.2/lib/rails/commands/runner/runner_command.rb:46
...

Apologies if I missed this since I admittedly didn't dig into this deeply, but I know for system catalogs we might not need to set a schema, but for custom schemas used with apps, where tables are located, presumably diagnose needs to access those, and we'd need to set a schema to be included in SQL queries? I'll keep playing around with this a bit but wanted to bring it up early before I forgot.

Update:

Related: #43 (comment)

More ideas

  1. Detect invalid indexes/constraints
  2. Detect duplicate indexes - with identical structure or prefixes of each other
  3. Detect columns near integer overflow
  4. Detect empty tables (or not accessed within some threshold period). Would be useful when cleaning up/refactoring a messy db schema.
  5. Detect useless columns - columns having only one distinct value or only nulls. Caution: this check will be costly on large tables.
  6. Method to return current connections info
  7. Method to return current db settings

Allow to set "public dashboard" via configuration

d1a48cb introduced mandatory authentication

We're having our own authentication in the app, by something like:

constraints Constraints::Admin.new do
  mount RailsPgExtras::Web::Engine, at: "pg_extras"
end

We don't really want to set RAILS_PG_EXTRAS_PUBLIC_DASHBOARD in all the possible apps (using heroku). It would be much easier for us to just set this via gem configuration in the initializer.

pg_extras:calls and pg_extras:outliers don't work

I'm trying to check possible problematic queries that use sequential scans but when I try to get those queries by using calls and/or outliers method there seems to have problems with a missing table.

~ bundle exec rake pg_extras:calls
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column "total_time" does not exist
LINE 4: interval '1 millisecond' * total_time AS exec_time,
                                   ^
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `block (2 levels) in execute'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:91:in `block in execute'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract_adapter.rb:721:in `block in log'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract_adapter.rb:712:in `log'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:90:in `execute'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras.rb:27:in `run_query'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras.rb:12:in `block (2 levels) in <module:RailsPGExtras>'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras/tasks/all.rake:19:in `public_send'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras/tasks/all.rake:19:in `block (3 levels) in <main>'
/usr/local/bundle/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli.rb:476:in `exec'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli.rb:24:in `start'
/usr/local/bundle/gems/bundler-2.1.2/exe/bundle:46:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-2.1.2/exe/bundle:34:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'

Caused by:
PG::UndefinedColumn: ERROR:  column "total_time" does not exist
LINE 4: interval '1 millisecond' * total_time AS exec_time,
                                   ^
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `exec'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `block (2 levels) in execute'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:91:in `block in execute'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract_adapter.rb:721:in `block in log'
/usr/local/bundle/gems/activesupport-6.0.3.6/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/abstract_adapter.rb:712:in `log'
/usr/local/bundle/gems/activerecord-6.0.3.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:90:in `execute'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras.rb:27:in `run_query'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras.rb:12:in `block (2 levels) in <module:RailsPGExtras>'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras/tasks/all.rake:19:in `public_send'
/usr/local/bundle/gems/rails-pg-extras-1.6.0/lib/rails-pg-extras/tasks/all.rake:19:in `block (3 levels) in <main>'
/usr/local/bundle/gems/rake-13.0.3/exe/rake:27:in `<top (required)>'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli.rb:476:in `exec'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/cli.rb:24:in `start'
/usr/local/bundle/gems/bundler-2.1.2/exe/bundle:46:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-2.1.2/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-2.1.2/exe/bundle:34:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'
Tasks: TOP => pg_extras:calls
(See full trace by running task with --trace)

The rest of the methods seem to work great and pg_stat_statements seems to be properly installed.

+-----------------------------------------------------------------------------------------------------------------------------------+
|                                                Available and installed extensions                                                 |
+--------------------+-----------------+-------------------+------------------------------------------------------------------------+
| name               | default_version | installed_version | comment                                                                |
+--------------------+-----------------+-------------------+------------------------------------------------------------------------+
| plpgsql            | 1.0             | 1.0               | PL/pgSQL procedural language                                           |
| pg_stat_statements | 1.8             | 1.8               | track planning and execution statistics of all SQL statements executed |
| dict_xsyn          | 1.0             |                   | text search dictionary template for extended synonym processing        |
| adminpack          | 2.1             |                   | administrative functions for PostgreSQL                                |
| intarray           | 1.3             |                   | functions, operators, and index support for 1-D arrays of integers     |
...
...
...
...

I'm using postgres 13 and rails 6.0.3.4

"ArgumentError: Unparseable filesize: 8192 bytes"

Screen Shot 2022-01-20 at 12 33 53 AM

It's odd - I was seeing output from RailsPGExtras.diagnose with no issue, and am now seeing this message, with no change (that I know of) to anything (Gemfile, codebase, the postgresql db, etc). According to output, it couldn't parse "8192 bytes", but when I did that in console, it seems to parse fine with no issue

[2] pry(main)> Filesize.parse("8192 bytes")
=> {:prefix=>"", :size=>0.0, :type=>nil}
[3] pry(main)>

The other RailsPGExtras outputs work just fine (db_settings, total_index_size, and so forth on), just not .diagnose.

Gem version is 3.2.5. Rails version 6.1.4.1, Postgresql version 10.13. Ruby version 2.7.4.

I ran begin; RailsPGExtras.diagnose; rescue => e; puts e.backtrace; end (saw the other issue, to save you some time). It appears to be coming from this line https://github.com/pawurb/ruby-pg-extras/blob/master/lib/ruby-pg-extras/diagnose_data.rb#L121

Stack trace:

[6] pry(main)> begin; RailsPGExtras.diagnose; rescue => e; puts e.backtrace; end
   (12.8ms)  /* Available and installed extensions */

SELECT * FROM pg_available_extensions ORDER BY installed_version;


   (54.1ms)  /* Index and table hit rate */

SELECT
  'index hit rate' AS name,
  (sum(idx_blks_hit)) / nullif(sum(idx_blks_hit + idx_blks_read),0) AS ratio
FROM pg_statio_user_indexes
UNION ALL
SELECT
 'table hit rate' AS name,
  sum(heap_blks_hit) / nullif(sum(heap_blks_hit) + sum(heap_blks_read),0) AS ratio
FROM pg_statio_user_tables;

   (5.4ms)  /* Index and table hit rate */

SELECT
  'index hit rate' AS name,
  (sum(idx_blks_hit)) / nullif(sum(idx_blks_hit + idx_blks_read),0) AS ratio
FROM pg_statio_user_indexes
UNION ALL
SELECT
 'table hit rate' AS name,
  sum(heap_blks_hit) / nullif(sum(heap_blks_hit) + sum(heap_blks_read),0) AS ratio
FROM pg_statio_user_tables;

   (9.5ms)  /* Unused and almost unused indexes */
/* Ordered by their size relative to the number of index scans.
Exclude indexes of very small tables (less than 5 pages),
where the planner will almost invariably select a sequential scan,
but may not in the future as the table grows */

SELECT
  schemaname || '.' || relname AS table,
  indexrelname AS index,
  pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
  idx_scan as index_scans
FROM pg_stat_user_indexes ui
JOIN pg_index i ON ui.indexrelid = i.indexrelid
WHERE NOT indisunique AND idx_scan < 50 AND pg_relation_size(relid) > 5 * 8192
ORDER BY pg_relation_size(i.indexrelid) / nullif(idx_scan, 0) DESC NULLS FIRST,
pg_relation_size(i.indexrelid) DESC;

/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/filesize-0.2.0/lib/filesize.rb:140:in `from'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:121:in `block in unused_indexes'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:120:in `select'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:120:in `unused_indexes'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:48:in `block in call'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:47:in `map'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:47:in `call'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/ruby-pg-extras-3.2.5/lib/ruby-pg-extras/diagnose_data.rb:17:in `call'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/rails-pg-extras-3.2.5/lib/rails-pg-extras.rb:54:in `diagnose'
(pry):6:in `<main>'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in `eval'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:290:in `evaluate_ruby'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:659:in `handle_line'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:261:in `block (2 levels) in eval'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in `catch'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:260:in `block in eval'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in `catch'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_instance.rb:259:in `eval'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/repl.rb:77:in `block in repl'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/repl.rb:67:in `loop'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/repl.rb:67:in `repl'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/repl.rb:38:in `block in start'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/input_lock.rb:61:in `__with_ownership'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/input_lock.rb:78:in `with_ownership'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/repl.rb:38:in `start'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/repl.rb:15:in `start'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-0.13.1/lib/pry/pry_class.rb:191:in `start'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/pry-byebug-3.9.0/lib/pry-byebug/pry_ext.rb:13:in `start_with_pry_byebug'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/railties-6.1.4.1/lib/rails/commands/console/console_command.rb:70:in `start'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/railties-6.1.4.1/lib/rails/commands/console/console_command.rb:19:in `start'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/railties-6.1.4.1/lib/rails/commands/console/console_command.rb:102:in `perform'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/railties-6.1.4.1/lib/rails/command/base.rb:69:in `perform'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/railties-6.1.4.1/lib/rails/command.rb:48:in `invoke'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/railties-6.1.4.1/lib/rails/commands.rb:18:in `<main>'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
/Users/craig/Documents/projects/octoopi/api/bin/rails:7:in `<main>'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:59:in `load'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/fork_tracker.rb:10:in `block in fork'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/fork_tracker.rb:8:in `fork'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/fork_tracker.rb:8:in `fork'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/activesupport-6.1.4.1/lib/active_support/fork_tracker.rb:27:in `fork'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
/Users/craig/.rbenv/versions/2.7.4/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
-e:1:in `<main>'
=> nil

Thanks in advance!

New version errors out - LoadError: cannot load such file -- rails_pg_extras

4.1.0 works, but 4.4.1 fails with the following error when deploying our app

-----> Detecting rake tasks
 !
 !     Could not detect rake tasks
 !     ensure you can run `$ bundle exec rake -P` against your app
 !     and using the production group of your Gemfile.
 !     rake aborted!
 !     LoadError: cannot load such file -- rails_pg_extras
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:27:in `require'
 !     /tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:60:in `block (2 levels) in require'
 !     /tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:55:in `each'
 !     /tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:55:in `block in require'
 !     /tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:44:in `each'
 !     /tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:44:in `require'
 !     /tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler.rb:176:in `require'
 !     /tmp/build_ee9d9c8a/config/application.rb:7:in `<main>'
 !     /tmp/build_ee9d9c8a/Rakefile:4:in `require_relative'
 !     /tmp/build_ee9d9c8a/Rakefile:4:in `<main>'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:29:in `load_rakefile'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:710:in `raw_load_rakefile'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:104:in `block in load_rakefile'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:103:in `load_rakefile'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:82:in `block in run'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
 !     /tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
 !     /tmp/build_ee9d9c8a/bin/rake:4:in `<main>'
 !
/tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/helpers/rake_runner.rb:100:in `load_rake_tasks!': Could not detect rake tasks (LanguagePack::Helpers::RakeRunner::CannotLoadRakefileError)
ensure you can run `$ bundle exec rake -P` against your app
and using the production group of your Gemfile.
rake aborted!
LoadError: cannot load such file -- rails_pg_extras
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:27:in `require'
/tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:60:in `block (2 levels) in require'
/tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:55:in `each'
/tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:55:in `block in require'
/tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:44:in `each'
/tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler/runtime.rb:44:in `require'
/tmp/build_ee9d9c8a/vendor/ruby-3.1.1/lib/ruby/3.1.0/bundler.rb:176:in `require'
/tmp/build_ee9d9c8a/config/application.rb:7:in `<main>'
/tmp/build_ee9d9c8a/Rakefile:4:in `require_relative'
/tmp/build_ee9d9c8a/Rakefile:4:in `<main>'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:39:in `load'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/rake_module.rb:29:in `load_rakefile'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:710:in `raw_load_rakefile'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:104:in `block in load_rakefile'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:103:in `load_rakefile'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:82:in `block in run'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:186:in `standard_exception_handling'
/tmp/build_ee9d9c8a/vendor/bundle/ruby/3.1.0/gems/rake-13.0.6/lib/rake/application.rb:80:in `run'
/tmp/build_ee9d9c8a/bin/rake:4:in `<main>'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/ruby.rb:967:in `rake'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/rails4.rb:69:in `block in run_assets_precompile_rake_task'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/base.rb:175:in `log'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/rails4.rb:63:in `run_assets_precompile_rake_task'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/ruby.rb:103:in `block in compile'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/ruby.rb:988:in `allow_git'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/ruby.rb:96:in `compile'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/rails2.rb:55:in `compile'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/rails3.rb:37:in `compile'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/rails4.rb:30:in `compile'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/rails6.rb:17:in `compile'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/bin/support/ruby_compile:19:in `block in <main>'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/lib/language_pack/base.rb:175:in `log'
	from /tmp/codon/tmp/buildpacks/50d5eddf222a9b7326028041d4e6509f915ccf2c/bin/support/ruby_compile:18:in `<main>'
 !     Push rejected, failed to compile Ruby app.
 !     Push failed

Installing `pg_stat_statements` and rails' tasks

Hi,
first off thank you for creating and maintaining this awesome gem!

After I install pg_stat_statements with RailsPgExtras.add_extensions I get the following lines in db/schema.rb:

  enable_extension "pg_stat_statements"
  # ...
  create_view "pg_stat_statements_info", sql_definition: <<-SQL
      SELECT pg_stat_statements_info.dealloc,
      pg_stat_statements_info.stats_reset
     FROM pg_stat_statements_info() pg_stat_statements_info(dealloc, stats_reset);
  SQL

Now if I want to drop and reset a DB I get the following error with these rails commands:

  • bin/rails db:drop db:prepare
  • bin/rails db:drop db:create db:schema:load
Dropped database 'main_development'
Dropped database 'main_test'
Created database 'main_development'
rails aborted!
ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR:  relation "pg_stat_statements_info" already exists
/Users/bruno/data/linkok/main/db/schema.rb:387:in `block in <main>'
/Users/bruno/data/linkok/main/db/schema.rb:13:in `<main>'

Caused by:
PG::DuplicateTable: ERROR:  relation "pg_stat_statements_info" already exists
/Users/bruno/data/linkok/main/db/schema.rb:387:in `block in <main>'
/Users/bruno/data/linkok/main/db/schema.rb:13:in `<main>'
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)

In either case, I think the problem is:

  • Rails loads the schema from db/schema.rb
  • It executes the line enable_extension "pg_stat_statements" and adds the extension which automatically creates the pg_stat_statements_info view.
  • Now schema comes to the line with create_view "pg_stat_statements_info", sql_definition .... This fails because the view already exists.

This effectively makes db:prepare task useless. The workaround is to use db:create db:migrate db:seed.

Do you have any suggestions how to handle this more elegantly? Thanks a lot!

ActiveRecord::StatementInvalid when pg_stat_statements is enabled

pg_stat_statements is enabled

I can see the following when I run RailsPGExtras.extensions

pg_stat_statements | 1.7 | | track execution statistics of all SQL statements executed

When I run RailsPGExtras.calls

I get the following error

Traceback (most recent call last):
        1: from (irb):14
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "pg_stat_statements" does not exist)
LINE 8: FROM pg_stat_statements WHERE userid = (SELECT usesysid FROM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.