1

I was wondering how to drop an index in postgresql. I know to use DROP INDEX, but many of the examples I see online show creating an index and dropping the known index such as this site: https://www.geeksforgeeks.org/postgresql-drop-index/.

However, what I am looking for is going into a table and just dropping already existing indexes. I found this site that tells me how to show the different index names and index defs: https://www.postgresqltutorial.com/postgresql-indexes/postgresql-list-indexes/, but not sure how to drop the indexes from here. Or in other words, how would I get/query an index name from the database, and then drop that index?

I'm quite new to sql and querying in general so I was wondering if the way to do this is to make a new table based on the SELECT/FROM/WHERE and this way I would have access to the index names and defs in a table which I could use to drop them? Could I just replace the SELECT with a DROP INDEX? Is there a better way to do this?

1 Answer 1

3

Use \d and \di to find the name of the index, then drop it.

\d <tableName>

lists (among other things) the indexes of the relation.

\di 

lists the names of all the indexes in the schema.

once you have the name, simply use:

drop index <name>;

As you can see below, some indexes cannot be dropped, since they are created by constraints (primary key, foreign key constraint, come to my mind).

[local]:dmg@rip=# \d r
                 Table "public.r"
 Column |  Type   | Collation | Nullable | Default 
--------+---------+-----------+----------+---------
 a      | integer |           | not null | 
 b      | integer |           |          | 
Indexes:
    "r_pkey" PRIMARY KEY, btree (a)
    "rindx" btree (a)
Referenced by:
    TABLE "s" CONSTRAINT "s_a_fkey" FOREIGN KEY (a) REFERENCES r(a) ON UPDATE CASCADE

[local]:dmg@rip=# \di
            List of relations
 Schema |  Name  | Type  | Owner | Table 
--------+--------+-------+-------+-------
 public | r_pkey | index | dmg   | r
 public | rindx  | index | dmg   | r
 public | s_pkey | index | dmg   | s
(3 rows)

[local]:dmg@rip=# drop index r_pkey;
ERROR:  cannot drop index r_pkey because constraint r_pkey on table r requires it
HINT:  You can drop constraint r_pkey on table r instead.

[local]:dmg@rip=# drop index rindx;
Sign up to request clarification or add additional context in comments.

4 Comments

Is the use of /d and /di only for the command prompt? I'm trying to write a script that uses groovy and PostgreSQL, so I was hoping I could do this without the use of the command prompt.
You can use this query to find the indexes on tab: SELECT indexrelid::regclass FROM pg_index WHERE indrelid = 'tab'::regclass;
Thanks, I think I can get/find the indexes, but I'm not sure how to drop those indexes? Do I have to save those index values somewhere first and then drop them or can I just straight out drop them?
you can just drop the index (drop index <name>;). It you needed it again, simple create it. No need to do anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.