Definition of PostgreSQL DROP INDEX
PostgreSQL drop index statement is used to drop or delete the existing index from a table column, we have used drop index command to drop the existing index in PostgreSQL. To use the command of drop index we need to have superuser privileges or need to have the owner of that specific index. If the object depends on the index then the index is not dropped error will be given that index will not drop, basically, cascade operations with concurrent is not supported in PostgreSQL drop index command. Drop index command is very useful and important in PostgreSQL to drop the unused index.
Syntax and Parameter
Below is the syntax of drop index statement in PostgreSQL.
- Drop index index_name; — Name of index which was we have dropping using drop index command.
- Drop index index_name1, index_name2, index_name3, …., index_nameN.
- Drop index [Concurrently (Drop index without affecting or locking other operations like select, insert, update and delete)] [if exists (Drop the index if exists in the database also do not throw the error if index in not exist)] name_of_index (Name of index which was we have dropping) [Cascade | Restrict ]
Below is the parameter description syntax of the drop index statement.
- Drop index: This command is used to drop the existing index and drop the unused index from the database table column in PostgreSQL.
- Index name: It is defined as the name of the index which was we have dropping from the database table.
- Concurrently: Drop the index without affecting the other operations like select, insert, update, and delete. Drop index in PostgreSQL will acquire the exclusive lock on the table, it will block the access of operations until the drop index was not completed successfully. Cascade operations are not supported after using concurrent options with drop index command.
- If exists: This option is used when if the index is not present on the database table, after using these options it will not show the error of the index cannot exists. If the index will not present on the database table it will issue notice.
- Cascade: This option is used when we have to drop the index object automatically which was depends on the index. But cascade options is not supported with concurrent options in PostgreSQL.
- Restrict: It is used when we have not use the drop index option forcefully on an index. It will refuse the drop index command when any other object will depend on that index. This is a default parameter of the drop index command.
How DROP INDEX Statement Work in PostgreSQL?
- Below is the working of the drop index statement in PostgreSQL.
- The index will increase the performance of select operations from the table. But it will degrade the performance of insert and update operations.
- If our database table contains the insert and update operations more than select then we need to delete the same index from the table at that time we have using the drop index statement to drop unused index from the database.
- We need to have a superuser or owner of index privileges to drop index from the database table.
- The below example shows that we need to have a superuser or owner of index privileges to drop the index.
psql -U index_test -d testing
drop index index_test;
psql -U postgres -d testing
drop index index_test;
- In the above first example, we have used the user index_test to drop the index name as index_test from the student table. But it will throw the error of “ERROR: must be the owner of relation index_test”.
- In the second example, we have used the superuser of database i.e. Postgres using superuser it is possible to drop the index in PostgreSQL.
- Using drop index command in PostgreSQL we can drop multiple indexes in a single statement.
- If the object depends on the index the same index will not dropped using the drop index operations.
- The below example shows that if any object depends on the index, it will not be dropped.
drop index student_pkey;
- In the above example, we have to try to drop the index of student_pkey but it will not be dropped due to other objects is dependent on this index.
- It will show the error of “ERROR: cannot drop index student_pkey because constraint student_pkey on table student requires it”.
Examples of PostgreSQL DROP INDEX
Below is the example of drop index command in PostgreSQL.
1. Drop single Index Using Drop Index Command
In the below example, we have dropped a single index using the drop index command. In the below example, we have drop the index name as index_test from the student table.
drop index "index_test";
2. Drop Multiple Index Using Drop Index Command
In the below example, we have dropped multiple indexes using drop index command. In the below example, we have drop the index name as p_index and n_index from the student table.
drop index p_index, n_index;
3. Drop Index Using if Exists Parameter
In the below example, we have used if exists a parameter with drop index command. If exists parameter will skip the index.
drop index IF EXISTS p_index;
4. Drop Index Using Concurrently Parameter
In the below example, we have used concurrently parameter with drop index command. We have drop c_index from the student table.
drop index concurrently "c_index";
This is a guide to PostgreSQL DROP INDEX. Here we also discuss the definition and how drop index statements work in postgresql along with its different examples and its code implementation. You may also have a look at the following articles to learn more –