简体   繁体   中英

Rails & postgres - reindex after restore

Recently I ran into a situation where I had to restore a backup database from Heroku to my local setup because of a configuration error I had made.

This was very straight-forward from a Heroku post:

https://devcenter.heroku.com/articles/heroku-postgres-import-export :

$ heroku pgbackups:capture
$ curl -o latest.dump `heroku pgbackups:url`
$ pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump

The issue became that after the restore, some of the old (pre-restore) index data was still being served with the newly restored data. ie it seemed that there was still some remnant index data after the restore and a db migration. Note that before the restore, I did not clear or delete or do anything to my old db ... so obviously there was some data still there pre-restore ... the tables were gone, but the indexes may have still been around.

I suspected that if I made some changes to the table, that would cause a partial reindex of whatever I changed, and would be some type of partial fix to the problem. And sure enough, when I made a change to the record via the console, the remnant data was updated (and was now correct).

So my question is multi-part ...

Why did the restore not clear everything in the db and "start from scratch"? (Is there another way?)

And is there a better programatic way to reindex a table via rake or console?

For searchkick I use:

rake searchkick:reindex CLASS=Blog

And within models I use something like:

blog.reindex

But I have not found a command/function way to do a basic table reindex (without some type of hack to touch every record).

Based on OP's response to my comment, I think this is the result of applying a full DB backup onto an existing DB, resulting in some cross-pollination between the restored data and the existing data, including stale indexes.

What I would recommend is, if the backup was the entire DB:

  1. On your local db, drop the whole thing using the dropdb command
  2. Re-create a blank db using createdb
  3. Then run your pg_restore command against the empty DB. That will restore all your data from the backup, and, providing indexes were part of that backup (which they should be in a backup that's the entire db), those should be populated with the newly restored data as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM