Drop index postgres, what if duplicate index name exists - postgresql

below query works perfectly in postgre, because duplicate index within table is not allowed but with in DB it is allowed.
sandbox=# create schema test;
CREATE SCHEMA
sandbox=# create table public.a (a_id integer not null);
CREATE TABLE
sandbox=# create table test.a (a_id integer not null);
CREATE TABLE
sandbox=# create index a_idx on public.a (a_id);
CREATE INDEX
sandbox=# create index a_idx on test.a (a_id);
CREATE INDEX
what happens when I do
DROP INDEX a_idx;
will both the indexes get deleted ?
can I write DROP INDEX test.a.a_idx ?
how the index look up works while deleting ?

What happens depends on the setting of search_path. PostgreSQL searches the existing schemas on search_path in turn, and as soon as it finds an index of that name, it drops the index and is done.

can I write DROP INDEX test.a.a_idx ?
The index is in the same schema as its table, so it would just be DROP INDEX test.a_idx (if you want/need to override search_path)

Related

Can't drop indexes with schema name

Created index on partition table movies.actors_2010(name) with schemaname movies.actors_2010_name_idx
CREATE INDEX CONCURRENTLY "movies.actors_2010_name_idx" ON movies.actors_2010(name);
Now when I try to drop the index, output shows index doesn't exist and index remain same, can't drop it.
drop index IF EXISTS movies.actors_2010_name_idx;
Any suggestions are helpful.
The problem appears to be that you quoted the identifier "movies.actors_2010_name_idx". That created an index with a . in the name, not an index named "actors_2010_name_idx" in the movies schema. To do that, you should have used
CREATE INDEX CONCURRENTLY "movies"."actors_2010_name_idx" ON "movies"."actors_2010"(name);
or
CREATE INDEX CONCURRENTLY movies.actors_2010_name_idx ON movies.actors_2010(name);
Now to do delete the index with the broken name, you'd use
DROP INDEX IF EXISTS "movies.actors_2010_name_idx";
or possibly
DROP INDEX IF EXISTS "movies"."movies.actors_2010_name_idx";

Unable to drop index on a db2 table

I have a table MAIN_SCHEMA.TEST in which I created a Index on a column CHECK_ID.
CHECK_ID is also a FOREIGN_KEY constraint in TEST table.
This table contains only 50 records.
By Mistake the index got created in Default schema DEFAULT_SCHEMA.CHECK_ID_IDX.
CREATE INDEX DEFAULT_SCHEMA.CHECK_ID_IDX(CHECK_ID ASC);
So I am trying to drop this index but the drop query gets stuck for long time.
DROP INDEX DEFAULT_SCHEMA.CHECK_ID_IDX.
there are no locks on this table when I checked.
Instead of dropping and recreating the index with the right schema, could you just try to RENAME the index? It requires the existing SCHEMA.NAME pair together with the new as input. It will not move any data, but just update the metadata.

Will postgresql generate index automatically?

is there automatic index in Postgresql or need users to create index explicitly? if there is automatic index, how can I view it? thanks.
An index on the primary key and unique constraints will be made automatically. Use CREATE INDEX to make more indexes. To view existing database structure including the indexes, use \d table.
A quick example of generating an index would be:
CREATE INDEX unique_index_name ON table (column);
You can create an index on multiple columns:
CREATE INDEX unique_index_name ON table (column1, column2, column3);
Or a partial index which will only exist when conditions are met:
CREATE INDEX unique_index_name ON table (column) WHERE column > 0;
There is a lot more you can do with them, but that is for the documentation (linked above) to tell you. Also, if you create an index on a production database, use CREATE INDEX CONCURRENTLY (it will take longer, but not lock out new writes to the table). Let me know if you have any other questions.
Update:
If you want to view indexes with pure SQL, look at the pg_catalog.pg_indexes table:
SELECT *
FROM pg_catalog.pg_indexes
WHERE schemaname='public'
AND tablename='table';

PostgreSQL cannot drop index on partition

I have several partition tables with indexes on them. All indexes can be seen in response of
SELECT indexname FROM pg_catalog.pg_indexes;
But when I'm trying to make DROP INDEX my_index_name; it returns error declaring that there is no index my_index_name.
How can I drop those indexes?
Could be related to your search_path. Try dropping the index prefixed by the schema.
Eg.
SELECT schemaname,tablename,indexname FROM pg_indexes WHERE indexname = 'my_index_name'
Using the results of that query, drop the index:
DROP INDEX some_schema.your_index_name;

Are indexes on temporary tables deleted when the table is deleted?

Would the following SQL remove also the index - or does it have to be removed separately?
CREATE TABLE #Tbl (field int)
CREATE NONCLUSTERED INDEX idx ON #Tbl (field)
DROP TABLE #Tbl
Yes they are. You can search in MSSQL help for CREATE INDEX article it is said there:
"Indexes can be created on a temporary
table. When the table is dropped or
the session ends, all indexes and
triggers are dropped."
It will be removed automatically, as there is nothing left to index. Think of it as a child object in this respect.
The drop table will remove the index. Drop Index takes the index name and the table name.
In this case would be DROP INDEX idc ON #tbl
which can be called if you want to drop the index but leave the table.