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 the drop index command to drop the existing index in PostgreSQL. To use the drop index command, 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 the index will not drop; basically, cascade operations with concurrent is not supported in the 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 the drop index statement in PostgreSQL.
- Drop index index_name; — Name of the index which was we have dropped 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 the index in not exist)] name_of_index (Name of the index which was we have dropping) [Cascade | Restrict ]
Parameter:
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 index’s name, 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 the drop index command.
- If it 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 exist. If the index does not present on the database table, it will issue notice.
- Cascade: This option is used when we have to automatically drop the index object, which depends on the index. But cascade options are not supported with concurrent options in PostgreSQL.
- Restrict: It is used when we have not to use the drop index option forcefully on an index. It will refuse the drop index command when any other object depends on that index. This is a default parameter of the drop index command.
How to 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 the 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 the database, i.e. Postgres; using superuser, it is possible to drop the index in PostgreSQL.
- Using the 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 an example of the 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.
\d+ student
drop index "index_test";
\d+ student
2. Drop Multiple Index Using Drop Index Command
In the below example, we have dropped multiple indexes using the drop index command. In the below example, we have drop the index name as p_index and n_index from the student table.
\d+ student
drop index p_index, n_index;
\d+ student
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.
\d+ student
drop index IF EXISTS p_index;
4. Drop Index Using Concurrently Parameter
In the below example, we have used parameter concurrently with the drop index command. We have drop the c_index from the student table.
\d+ student
drop index concurrently "c_index";
\d+ student
Recommended Articles
This is a guide to PostgreSQL DROP INDEX. Here we also discuss the definition and how drop index statements work in postgresql and its different examples and code implementation. You may also have a look at the following articles to learn more –