After 9.6->12.3 pg_upgrade we marked some serious select give missing results! REINDEX or drop / create healthed the problem.
Upgrade bulletpoints
- Stop 9.6
- rsync 9.6 data and bin files from Centos7 to Centos8 (pre installed 12)
- pg_upgrade
- ./analyze_new_cluster.sh
- ./delete_old_cluster.sh
Per database we found 1-3 UNIQUE corrupt indexes. Missed circa 20 values per index.
We found a very useful tool amcheck! https://www.postgresql.org/docs/10/amcheck.html
SELECT bt_index_check(c.oid), c.relname, c.relpages
FROM pg_index i
JOIN pg_opclass op ON i.indclass[0] = op.oid
JOIN pg_am am ON op.opcmethod = am.oid
JOIN pg_class c ON i.indexrelid = c.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE am.amname = 'btree' AND n.nspname = 'pg_catalog'
-- Don't check temp tables, which may be from another session:
AND c.relpersistence != 't'
-- Function may throw an error when this is omitted:
AND i.indisready AND i.indisvalid
ORDER BY c.relpages DESC LIMIT 10;
VERY IMPORTANT: Comment out (AND n.nspname = 'pg_catalog' + LIMIT 10)restictions by validation, to run bt_index_check function to your index too!
And yes the function throw an exception if find a corrupt index.
Why does index go wrong? How can we be sure our new db is consistent and upgrade is succesful?