Created
August 1, 2022 20:30
-
-
Save jdecode/7ac06a8f7f0307c734066dfdd0eb3f39 to your computer and use it in GitHub Desktop.
SQL to update the migrations table sequence, since the migrations were failing due to some DB mismatch (after importing data from different source) causing the max seq to be less than the data in the table, and having a conflicting primary key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select setval('migrations_id_seq', max(id) + 1, false) from migrations; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally posted at: https://stackoverflow.com/questions/2596670/how-do-you-find-the-row-count-for-all-your-tables-in-postgres
If you want a true count, you have to execute the SELECT statement like the one you used against each table. This is because PostgreSQL keeps row visibility information in the row itself, not anywhere else, so any accurate count can only be relative to some transaction. You're getting a count of what that transaction sees at the point in time when it executes. You could automate this to run against every table in the database, but you probably don't need that level of accuracy or want to wait that long.
The second approach notes that the statistics collector tracks roughly how many rows are "live" (not deleted or obsoleted by later updates) at any time. This value can be off by a bit under heavy activity, but is generally a good estimate:
That can also show you how many rows are dead, which is itself an interesting number to monitor.
The third way is to note that the system ANALYZE command, which is executed by the autovacuum process regularly as of PostgreSQL 8.3 to update table statistics, also computes a row estimate. You can grab that one like this: