EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL DROP INDEX

PostgreSQL DROP INDEX

Priya Pedamkar
Article byPriya Pedamkar

Updated May 12, 2023

PostgreSQL DROP INDEX

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 are not supported in the PostgreSQL drop index command. Drop index command is very useful and important in PostgreSQL to drop the unused index.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax and Parameter

Below is the syntax of the drop index statement in PostgreSQL.

  • Drop index index_name; — Name of the index we dropped using the 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 it 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: You can use this command to drop the existing index and remove the unused index from the database table column in PostgreSQL.
  • Index name: We define the index name we want to drop 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 operations access until the drop index was not completed successfully. After using concurrent options with the drop index command, it is not possible to perform cascade operations.
  • If it exists: This option is used when the index is not present on the database table; after using these options, it will not show the error of the index cannot exist. The index will issue a notice if it is not on the database table.
  • Cascade: We use this option to drop the index object that depends on the index automatically. But cascade options are not supported with concurrent options in PostgreSQL.
  • Restrict: It is used when we do not have to use the drop index option on an index forcefully. 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 a superuser or owner of index privileges to drop the index from the database table.
  • The example below shows that we need 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;

PostgreSQL DROP INDEX-1.1

  • 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 the relation index_test”.
  • In the second example, we have used the superuser of the database, i.e., Postgres; using the superuser, it is possible to drop the index in PostgreSQL.
  • We can drop multiple indexes in a single statement using the drop index command in PostgreSQL.
  • 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;

PostgreSQL DROP INDEX-1.2

  • In the above example, we have to try to drop the index of student_pkey, but it will not be dropped because other objects depend 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

PostgreSQL DROP INDEX-1.3

2. Drop Multiple Index Using the Drop Index Command

In the below example, we have dropped multiple indexes using the drop index command. In the example below, we have dropped the index names p_index and n_index from the student table.

\d+ student
drop index p_index, n_index;
\d+ student

Output-1.4

3. Drop the Index Using the if Exists Parameter

In the below example, we have used if exists a parameter with the drop index command. If exists parameter will skip the index.

\d+ student
drop index IF EXISTS p_index;

Output-1.5

4. Drop Index Using Concurrently Parameter

In the below example, we have used the 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

Output-1.6

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL DROP INDEX” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL Materialized Views
  2. PostgreSQL UNIQUE Index
  3. Vacuum PostgreSQL
  4. REINDEX PostgreSQL
PROGRAMMING LANGUAGES Course Bundle - 54 Courses in 1 | 4 Mock Tests
338+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Course Bundle - 15 Courses in 1 | 9 Mock Tests
39+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System Course Bundle - 7 Courses in 1
43+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Course Bundle - 6 Courses in 1
15+ Hour of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Course Bundle - 5 Courses in 1 | 1 Mock Test
 15+ Hour of HD Videos
5 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more