0

I created a unique index for a materialized view as :

create unique index if not exists matview_key on
      matview (some_group_id, some_description);

I can't tell if it has been created
How do I see the index?

Thank you!

Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
aerijman
  • 2,522
  • 1
  • 22
  • 32

3 Answers3

1

Like has been commented, if the command finishes successfully and you don't get an error message, the index was created. Possible caveat: while the transaction is not committed, nobody else can see it (except the unique name is reserved now), and it still might get rolled back. Check in a separate transaction to be sure.

To be absolutely sure:

SELECT pg_get_indexdef(oid)
FROM   pg_catalog.pg_class
WHERE  relname = 'matview_key'
AND    relkind = 'i'
-- AND    relnamespace = 'public'::regnamespace  -- optional, to make sure of the schema, too

This way you see whether an index of the given name exists, and also its exact definition to rule out a different index with the same name. Pure SQL, works from any client. (There is nothing special about an index on materialized views.)

Also filter for the schema to be absolutely sure. Would be the "default" schema (a.k.a. "current" schema) in your case, since you did not specify in the creation. See:

Related:

In psql:

\di public.matview_key

To only find indexes. Again, the schema is optional to narrow down.

Progress Reporting

If creating an index takes a long time, you can look up progress in pg_stat_progress_create_index since Postgres 12:

SELECT * FROM pg_stat_progress_create_index
-- WHERE relid = 'public.matview'::regclass  -- optionally narrow down
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • Hello Erwin Brandstetter, Can you help me with this question. I believe you can solve it....... https://stackoverflow.com/questions/70256034/psql-df-display-function-not-contain-certain-string-pattern?noredirect=1#comment124196464_70256034 – jian Dec 07 '21 at 10:15
1

Two ways to verify index creation:

--In psql
\d matview

--Using SQL

select 
  * 
from 
   pg_indexes 
where 
   indexname = 'matview_key' 
and 
   tablename = 'matview';

More information on pg_indexes.

Adrian Klaver
  • 15,886
  • 2
  • 17
  • 28
0

Un alternative to looking into pg_indexes is pg_matviews (for a materialized view only)

select *
from pg_matviews
where matviewname = 'my_matview_name'; 
aerijman
  • 2,522
  • 1
  • 22
  • 32