Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the available options to identify and remove the invalid objects in Postgres (ex: corrupted indexes)

Tags:

postgresql

What are the available options to identify and remove the invalid objects in Postgres

like image 204
Hari Avatar asked Jan 20 '10 14:01

Hari


People also ask

How do I find invalid objects in PostgreSQL?

However, There is no way list up 'invalid' objects in PostgreSQL, since other objects cannot become invalid in PostgreSQL. Oracle and PostgreSQL work quite differently in that respect : In Oracle, you can always ALTER an object (for example a table) even if there are dependent objects (for example views).

What is PostgreSQL invalid index?

Detects indexes that are not recognized as valid in Postgres and creates an issue with severity "info", one for each table (or table hierarchy in case of inheritance or partitioning). These indexes typically are left over when using CREATE INDEX CONCURRENTLY and the command fails or is aborted by the user.

How to rebuild indexes in Postgres?

One way to do this is to shut down the server and start a single-user PostgreSQL server with the -P option included on its command line. Then, REINDEX DATABASE , REINDEX SYSTEM , REINDEX TABLE , or REINDEX INDEX can be issued, depending on how much you want to reconstruct.


2 Answers

If you're referring to detecting "invalid" (poorly created) indexes, apparently Postgres can "fail" in an attempt to create an index, and then the query planner won't use them, though they exist in your system. This query will detect "failed" indexes:

https://www.enterprisedb.com/blog/pgupgrade-bug-invalid-concurrently-created-indexes

SELECT n.nspname, c.relname
FROM   pg_catalog.pg_class c, pg_catalog.pg_namespace n,
       pg_catalog.pg_index i
WHERE  (i.indisvalid = false OR i.indisready = false) AND
       i.indexrelid = c.oid AND c.relnamespace = n.oid AND
       n.nspname != 'pg_catalog' AND
       n.nspname != 'information_schema' AND
       n.nspname != 'pg_toast'

though I suppose detecting TOAST table indexes wouldn't hurt, so you can remove that portion of the query :)

Related, for me sometimes just running a fresh ANALYZE on a table also makes indexes suddenly start being used in production (i.e. even if indexes aren't "invalid" they may be unused until an ANALYZE run). Weird.

like image 77
rogerdpack Avatar answered Nov 26 '22 18:11

rogerdpack


Have you tried running vacuum full pg_class as superuser?

Also, auto-vacuum should take care of it eventually. Your objects seem to be temporary tables/indexes, and the catalog is (usually) not being updated as frequently as your data.

like image 27
Denis de Bernardy Avatar answered Nov 26 '22 17:11

Denis de Bernardy