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;
Related
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";
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)
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.
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.
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"
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
I try to execute statement
DELETE FROM statistics WHERE statistic_id is null
and geting error:
java.sql.SQLException: ERROR 1027 (42Y86): All columns referenced in a WHERE clause must be available in every index for a table with immutable rows. tableName=STATISTICS
at org.apache.phoenix.exception.SQLExceptionCode$Factory$1.newException(SQLExceptionCode.java:386)
at org.apache.phoenix.exception.SQLExceptionInfo.buildException(SQLExceptionInfo.java:145)
at org.apache.phoenix.compile.DeleteCompiler.compile(DeleteCompiler.java:389)
at org.apache.phoenix.jdbc.PhoenixStatement$ExecutableDeleteStatement.compilePlan(PhoenixStatement.java:553)
at org.apache.phoenix.jdbc.PhoenixStatement$ExecutableDeleteStatement.compilePlan(PhoenixStatement.java:541)
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:303)
at org.apache.phoenix.jdbc.PhoenixStatement$2.call(PhoenixStatement.java:296)
at org.apache.phoenix.call.CallRunner.run(CallRunner.java:53)
at org.apache.phoenix.jdbc.PhoenixStatement.executeMutation(PhoenixStatement.java:294)
at org.apache.phoenix.jdbc.PhoenixStatement.execute(PhoenixStatement.java:1254)
at sqlline.Commands.execute(Commands.java:822)
at sqlline.Commands.sql(Commands.java:732)
at sqlline.SqlLine.dispatch(SqlLine.java:808)
at sqlline.SqlLine.begin(SqlLine.java:681)
at sqlline.SqlLine.start(SqlLine.java:398)
at sqlline.SqlLine.main(SqlLine.java:292)
my primary key is on field ID and I've secondary key on STATISTIC_ID
Phoenix require [1] that when you delete something from table with immutable rows, rows to delete should be filtered by all indexed columns. The one way to do so is to disable offending indices by
ALTER INDEX index_name ON table_name DISABLE;
DELETE FROM table_name WHERE condition;
And afterwards rebuild disabled indices:
ALTER INDEX index_name ON table_name REBUILD;
However, keep in mind that this operation takes significant amount of time and resources.
[1] https://phoenix.apache.org/secondary_indexing.html#Immutable_Tables