0

I need to work out how many rows a database has in total in order to stay under a cap which, if exceeded, will result in a hosting providing deleting the db!.

I get the table names

ActiveRecord::Base.connection.tables
=> ["ar_internal_metadata", "listings", "metrics", "plots",
 "schema_migrations", "packages"]

and add the number of rows in each table:

irb(main):016:0> ArInternalMetadata.count
Traceback (most recent call last):
        1: from (irb):16
NameError (uninitialized constant ArInternalMetadata)

irb(main):017:0> Listing.count
=> 7940328

irb(main):018:0> Metric.count
=> 17016

irb(main):019:0> Plot.count
=> 17016

irb(main):020:0> SchemaMigration.count
Traceback (most recent call last):
        1: from (irb):20
NameError (uninitialized constant SchemaMigration)

irb(main):021:0> Package.count
=> 18798

7940328 + 17016 + 17016 + 18798 = 7993158

The hosting provider will delete the database in 7 days because it exceeds their usage cap, which is 10,000,000 rows.

Presuming ar_internal_metadata and schema_migrations aren't massive, then I think there's ~8m rows and Heroku thinks there >10m.

Question

How can I (accurately) count how many rows a rails (postgres) database has?

stevec
  • 41,291
  • 27
  • 223
  • 311
  • Does this answer your question? [Get a row count of every table in Postgres database](https://stackoverflow.com/questions/28667604/get-a-row-count-of-every-table-in-postgres-database) – dbugger Jan 26 '22 at 01:01
  • 1
    Are you able to migrate to a service that doesn't have this draconian restriction? RDS, Google Cloud where limits are more storage based ? – VynlJunkie Jan 26 '22 at 10:37
  • If anyone arrives here in the future, I documented helpful approaches [here](https://stackoverflow.com/a/70872811/5783745) (see toward the bottom), and the easiest (on heroku) is to run this: `heroku pg:vacuum-stats`. – stevec Jan 27 '22 at 14:25

0 Answers0