{"id":13518866,"url":"https://github.com/heroku/heroku-pg-extras","last_synced_at":"2025-05-14T00:04:48.560Z","repository":{"id":4868307,"uuid":"6023225","full_name":"heroku/heroku-pg-extras","owner":"heroku","description":"A heroku plugin for awesome pg:* commands that are also great and fun and super.","archived":false,"fork":false,"pushed_at":"2025-02-26T22:20:15.000Z","size":448,"stargazers_count":1343,"open_issues_count":19,"forks_count":107,"subscribers_count":126,"default_branch":"main","last_synced_at":"2025-05-12T15:53:47.430Z","etag":null,"topics":["heroku","heroku-cli-plugin","heroku-data"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"amazeui/switch","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/heroku.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-09-30T22:51:45.000Z","updated_at":"2025-05-11T03:31:00.000Z","dependencies_parsed_at":"2023-07-06T18:46:25.114Z","dependency_job_id":"0f672c98-da9c-4f19-9e78-0a523419b6c3","html_url":"https://github.com/heroku/heroku-pg-extras","commit_stats":{"total_commits":281,"total_committers":49,"mean_commits":5.73469387755102,"dds":0.8042704626334519,"last_synced_commit":"fd933c2538730bab9ad7a8496c35763db09401b3"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroku%2Fheroku-pg-extras","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroku%2Fheroku-pg-extras/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroku%2Fheroku-pg-extras/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/heroku%2Fheroku-pg-extras/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/heroku","download_url":"https://codeload.github.com/heroku/heroku-pg-extras/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254043298,"owners_count":22004917,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["heroku","heroku-cli-plugin","heroku-data"],"created_at":"2024-08-01T05:01:50.146Z","updated_at":"2025-05-14T00:04:48.543Z","avatar_url":"https://github.com/heroku.png","language":"JavaScript","funding_links":[],"categories":["TypeScript","\u003cimg width=\"21\" height=\"21\" src=\"images/toolbelt.png\" /\u003e Toolbelt","JavaScript"],"sub_categories":[],"readme":"# `pg-extras` CLI Plugin\n\nA Heroku CLI plugin providing shortcuts to common Postgres introspection queries.\n\nThis plugin is used to obtain information about a Heroku Postgres instance,\nthat may be useful when analyzing performance issues. This includes information\nabout locks, index usage, buffer cache hit ratios and vacuum statistics.\n\n### Installation\n\n```bash\n$ heroku plugins:install heroku-pg-extras\n```\n\n### Usage\n\nEach command can display more detailed usage information, including accepted flags, with `heroku help pg:\u003ccommand\u003e`.\n\n#### `pg:cache-hit`\n\n```bash\n$ heroku pg:cache-hit\n      name      |         ratio\n----------------+------------------------\n index hit rate | 0.99957765013541945832\n table hit rate |                   1.00\n(2 rows)\n```\n\nThis 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 Heroku Postgres plan is too small for the workload.\n\n#### `pg:index-usage`\n\n```\n$ heroku pg:index-usage\n       relname       | percent_of_times_index_used | rows_in_table\n---------------------+-----------------------------+---------------\n events              |                          65 |       1217347\n app_infos           |                          74 |        314057\n app_infos_user_info |                           0 |        198848\n user_info           |                           5 |         94545\n delayed_jobs        |                          27 |             0\n(5 rows)\n```\n\nThis 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.\n\n### `pg:locks`\n\n```\n$ heroku pg:locks\n procpid | relname | transactionid | granted |     query_snippet     |       age\n---------+---------+---------------+---------+-----------------------+-----------------\n   31776 |         |               | t       | \u003cIDLE\u003e in transaction | 00:19:29.837898\n   31776 |         |          1294 | t       | \u003cIDLE\u003e in transaction | 00:19:29.837898\n   31912 |         |               | t       | select * from hello;  | 00:19:17.94259\n    3443 |         |               | t       |                      +| 00:00:00\n         |         |               |         |    select            +|\n         |         |               |         |      pg_stat_activi   |\n(4 rows)\n```\n\nThis 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.\n\n### `pg:outliers`\n\n```\n$ heroku pg:outliers\n                   qry                   |    exec_time     | prop_exec_time |   ncalls    | sync_io_time\n-----------------------------------------+------------------+----------------+-------------+--------------\n SELECT * FROM archivable_usage_events.. | 154:39:26.431466 | 72.2%          | 34,211,877  | 00:00:00\n COPY public.archivable_usage_events (.. | 50:38:33.198418  | 23.6%          | 13          | 13:34:21.00108\n COPY public.usage_events (id, reporte.. | 02:32:16.335233  | 1.2%           | 13          | 00:34:19.784318\n INSERT INTO usage_events (id, retaine.. | 01:42:59.436532  | 0.8%           | 12,328,187  | 00:00:00\n SELECT * FROM usage_events WHERE (alp.. | 01:18:10.754354  | 0.6%           | 102,114,301 | 00:00:00\n UPDATE usage_events SET reporter_id =.. | 00:52:35.683254  | 0.4%           | 23,786,348  | 00:00:00\n INSERT INTO usage_events (id, retaine.. | 00:49:24.952561  | 0.4%           | 21,988,201  | 00:00:00\n COPY public.app_ownership_events (id,.. | 00:37:14.31082   | 0.3%           | 13          | 00:12:32.584754\n INSERT INTO app_ownership_events (id,.. | 00:26:59.808212  | 0.2%           | 383,109     | 00:00:00\n SELECT * FROM app_ownership_events   .. | 00:19:06.021846  | 0.1%           | 744,879     | 00:00:00\n(10 rows)\n```\n\nThis 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 filesystem).\n\nTypically, 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.\n\n### `pg:calls`\n\n```\n$ heroku pg:calls\n                   qry                   |    exec_time     | prop_exec_time |   ncalls    | sync_io_time\n-----------------------------------------+------------------+----------------+-------------+--------------\n SELECT * FROM usage_events WHERE (alp.. | 01:18:11.073333  | 0.6%           | 102,120,780 | 00:00:00\n BEGIN                                   | 00:00:51.285988  | 0.0%           | 47,288,662  | 00:00:00\n COMMIT                                  | 00:00:52.31724   | 0.0%           | 47,288,615  | 00:00:00\n SELECT * FROM  archivable_usage_event.. | 154:39:26.431466 | 72.2%          | 34,211,877  | 00:00:00\n UPDATE usage_events SET reporter_id =.. | 00:52:35.986167  | 0.4%           | 23,788,388  | 00:00:00\n INSERT INTO usage_events (id, retaine.. | 00:49:25.260245  | 0.4%           | 21,990,326  | 00:00:00\n INSERT INTO usage_events (id, retaine.. | 01:42:59.436532  | 0.8%           | 12,328,187  | 00:00:00\n SELECT * FROM app_ownership_events   .. | 00:19:06.289521  | 0.1%           | 744,976     | 00:00:00\n INSERT INTO app_ownership_events(id, .. | 00:26:59.885631  | 0.2%           | 383,153     | 00:00:00\n UPDATE app_ownership_events SET app_i.. | 00:01:22.282337  | 0.0%           | 359,741     | 00:00:00\n(10 rows)\n```\n\nThis command is much like `pg:outliers`, but ordered by the number of times a statement has been called.\n\n### `pg:blocking`\n\n```\n$ heroku pg:blocking\n blocked_pid |    blocking_statement    | blocking_duration | blocking_pid |                                        blocked_statement                           | blocked_duration\n-------------+--------------------------+-------------------+--------------+------------------------------------------------------------------------------------+------------------\n         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\n(1 row)\n```\n\nThis 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.\n\n#### `pg:total-index-size`\n\n```\n$ heroku pg:total-index-size\n  size\n-------\n 28194 MB\n(1 row)\n```\n\nThis 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).\n\n### `pg:index-size`\n\n```\n$ heroku pg:index-size\n                             name                              |  size\n---------------------------------------------------------------+---------\n idx_activity_attemptable_and_type_lesson_enrollment           | 5196 MB\n index_enrollment_attemptables_by_attempt_and_last_in_group    | 4045 MB\n index_attempts_on_student_id                                  | 2611 MB\n enrollment_activity_attemptables_pkey                         | 2513 MB\n index_attempts_on_student_id_final_attemptable_type           | 2466 MB\n attempts_pkey                                                 | 2466 MB\n index_attempts_on_response_id                                 | 2404 MB\n index_attempts_on_enrollment_id                               | 1957 MB\n index_enrollment_attemptables_by_enrollment_activity_id       | 1789 MB\n enrollment_activities_pkey                                    |  458 MB\n index_enrollment_activities_by_lesson_enrollment_and_activity |  402 MB\n index_placement_attempts_on_response_id                       |  109 MB\n index_placement_attempts_on_placement_test_id                 |  108 MB\n index_placement_attempts_on_grade_level_id                    |   97 MB\n index_lesson_enrollments_on_lesson_id                         |   93 MB\n(truncated results for brevity)\n```\n\nThis 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).\n\n### `pg:table-size`\n\n```\n$ heroku pg:table-size\n                             name                              |  size\n---------------------------------------------------------------+---------\n learning_coaches                                              |  196 MB\n states                                                        |  145 MB\n grade_levels                                                  |  111 MB\n charities_customers                                           |   73 MB\n charities                                                     |   66 MB\n(truncated results for brevity)\n```\n\nThis command displays the size of each table 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.\n\n### `pg:table-indexes-size`\n\n```\n$ heroku pg:table-indexes-size\n                             table                             | indexes_size\n---------------------------------------------------------------+--------------\n learning_coaches                                              |    153 MB\n states                                                        |    125 MB\n charities_customers                                           |     93 MB\n charities                                                     |     16 MB\n grade_levels                                                  |     11 MB\n(truncated results for brevity)\n```\n\nThis command displays the total size of indexes for each table, in MB. It is calculates by using the system administration function `pg_indexes_size()`.\n\n### `pg:total-table-size`\n\n```\n$ heroku pg:total-table-size\n                             name                              |  size\n---------------------------------------------------------------+---------\n learning_coaches                                              |  349 MB\n states                                                        |  270 MB\n charities_customers                                           |  166 MB\n grade_levels                                                  |  122 MB\n charities                                                     |   82 MB\n(truncated results for brevity)\n```\n\nThis command displays the total size of each table 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.\n\n### `pg:unused-indexes`\n\n```\n$ heroku pg:unused-indexes\n          table      |                       index                | index_size | index_scans\n---------------------+--------------------------------------------+------------+-------------\n public.grade_levels | index_placement_attempts_on_grade_level_id | 97 MB      |           0\n public.observations | observations_attrs_grade_resources         | 33 MB      |           0\n public.messages     | user_resource_id_idx                       | 12 MB      |           0\n(3 rows)\n```\n\nThis command displays indexes that have \u003c 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.\n\n### `pg:seq-scans`\n\n```\n$ heroku pg:seq-scans\n\n               name                |  count\n-----------------------------------+----------\n learning_coaches                  | 44820063\n states                            | 36794975\n grade_levels                      | 13972293\n charities_customers               |  8615277\n charities                         |  4316276\n messages                          |  3922247\n contests_customers                |  2915972\n classroom_goals                   |  2142014\n contests                          |  1370267\n goals                             |  1112659\n districts                         |   158995\n rollup_reports                    |   115942\n customers                         |    93847\n schools                           |    92984\n classrooms                        |    92982\n customer_settings                 |    91226\n(truncated results for brevity)\n```\n\nThis 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 underindexed, and it may be worth investigating queries that read from these tables.\n\n### pg:long-running-queries\n\n```\n$ heroku pg:long-running-queries\n\n  pid  |    duration     |                                      query\n-------+-----------------+---------------------------------------------------------------------------------------\n 19578 | 02:29:11.200129 | EXPLAIN SELECT  \"students\".* FROM \"students\"  WHERE \"students\".\"id\" = 1450645 LIMIT 1\n 19465 | 02:26:05.542653 | EXPLAIN SELECT  \"students\".* FROM \"students\"  WHERE \"students\".\"id\" = 1889881 LIMIT 1\n 19632 | 02:24:46.962818 | EXPLAIN SELECT  \"students\".* FROM \"students\"  WHERE \"students\".\"id\" = 1581884 LIMIT 1\n(truncated results for brevity)\n```\n\nThis 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`.\n\n### pg:records-rank\n\n```\n$ heroku pg:records_rank\n               name                | estimated_count\n-----------------------------------+-----------------\n tastypie_apiaccess                |          568891\n notifications_event               |          381227\n core_todo                         |          178614\n core_comment                      |          123969\n notifications_notification        |          102101\n django_session                    |           68078\n (truncated results for brevity)\n```\n\nThis 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.\n\n### pg:bloat\n\n```\n$ heroku pg:bloat\n\n type  | schemaname |           object_name         | bloat |   waste\n-------+------------+-------------------------------+-------+----------\n table | public     | bloated_table                 |   1.1 | 98 MB\n table | public     | other_bloated_table           |   1.1 | 58 MB\n index | public     | bloated_table::bloated_index  |   3.7 | 34 MB\n table | public     | clean_table                   |   0.2 | 3808 kB\n table | public     | other_clean_table             |   0.3 | 1576 kB\n```\n\nThis 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.\n\n### pg:vacuum-stats\n\n```\n$ heroku pg:vacuum-stats\n schema |         table         | last_vacuum | last_autovacuum  |    rowcount    | dead_rowcount  | autovacuum_threshold | expect_autovacuum\n--------+-----------------------+-------------+------------------+----------------+----------------+----------------------+-------------------\n public | log_table             |             | 2013-04-26 17:37 |         18,030 |              0 |          3,656       |\n public | data_table            |             | 2013-04-26 13:09 |             79 |             28 |             66       |\n public | other_table           |             | 2013-04-26 11:41 |             41 |             47 |             58       |\n public | queue_table           |             | 2013-04-26 17:39 |             12 |          8,228 |             52       | yes\n public | picnic_table          |             |                  |             13 |              0 |             53       |\n```\n\nThis 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.\n\n### pg:user-connections\n\n```\n$ heroku pg:user-connections\nCredential      Connections\n──────────────  ───────────\nua7almfsv0d8tq  24\n```\n\nThis command displays the number of open connections for each role. This is primarily useful for determining if a specific role is consuming many more connections than expected.\n\n### pg:mandelbrot\n\n```\n$ heroku pg:mandelbrot\n```\n\nThis command outputs the Mandelbrot set, calculated through SQL.\n\n## Publishing\n\nTo publish new versions, see\n[the data plugin documentation](https://github.com/heroku/dod-ops/blob/master/playbooks/cli-plugins.md).\n\n## THIS IS BETA SOFTWARE\n\nThanks for trying it out. If you find any issues, please [open an issue](https://github.com/heroku/heroku-pg-extras/issues).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheroku%2Fheroku-pg-extras","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheroku%2Fheroku-pg-extras","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheroku%2Fheroku-pg-extras/lists"}