7

In a postgres database, I have a unique constraint and two unique indexes created for the same. I deleted the constraint using the query below:

alter table my_schema.users drop constraint users_dept_uk

It has dropped the constraint and one index but there was a second index which still exists.

The follwoing query is still telling me the index exists:

SELECT r.relname, r.relkind, n.nspname
FROM pg_class r INNER JOIN pg_namespace n ON r.relnamespace = n.oid
WHERE r.relname = 'users_dept_idx';

It gives the following output:

users_dept_idx, i, my_schema

When I execute the query below:

drop index my_schema.users_dept_idx

I am getting the error:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedObject) index "users_dept_idx" does not exist

What am I missing here? Not able to delete it and not able to insert data because of this index which I no longer want.

6
  • Are you sure you are connected to the same database throughout all of these operations? Why are you using python rather than psql for what seems to be an interactive use case? Commented Mar 21, 2020 at 15:57
  • Is all your input and output copied verbatim, or have you tried to manually anonymize them? Commented Mar 21, 2020 at 16:00
  • you've got to make sure you are on the right DB context, it looks like your alchemy run is happening on a db which doesn't hold the index, ,mostly I have seen this when folks do it again the default, i.e. postgres db. Commented Mar 21, 2020 at 16:27
  • I'm using python module sqlachemy and the above queries are executed using 'engine.execute()'. I am sure that I'm connecting to the same db as there are no other dbs Commented Mar 21, 2020 at 16:55
  • I have the same. When deleting it does not exists, when listing or creating -> already existing. Commented Apr 1, 2021 at 12:55

2 Answers 2

10

It is weird that the index name needs quotation around it. Below command worked absolutely fine and dropped the index as well

drop index my_schema."users_dept_idx"
Sign up to request clarification or add additional context in comments.

Comments

2

I had a similar problem. It turns out that when you create an index it is created in the same schema as the underlying table. In this case, you don't have to use the "schema" part of the name. When you drop it you have to use the full name of the index:

create index if not exists ah_login_minute on api.acc_history(login,updated_minute);
drop index if exists ah_login_minute; -- this fails, you have to use full name
drop index if exists api.ah_login_minute; -- this works

Comments

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.