PostgreSQL Partial Index

In this section, we are going to understand the working of the PostgreSQL Partial Index, which is used to enhance the performance of the command by reducing the index size.

What is PostgreSQL partial index?

Till now, we have understood how to add values of various columns to an index. The Partial index is the next command, which is used in the PostgreSQL index.

It allows us to define the rows of a table, which could be indexed. Therefore, we will use the partial index to speed up the query while dropping the index's size.

In other words, we can say that the PostgreSQL partial index is an index, which is constructed over a subsection of a table. The predicate of the partial index is well- defined subset by a conditional expression.

And the index is accessed for only those table rows, which satisfy the predicate.

Suppose we have used the WHERE conditions that have a constant value, then in such scenarios, we can go for the PostgreSQL partial index as it is very beneficial.

Note: If the column name contains the constant_value, then we can use the following illustration:

Syntax of PostgreSQL Partial Index

The syntax for specifying a partial index is as follows:

In the above illustration, the WHERE clause is used to define those rows, which are added to the index.

Example of PostgreSQL Partial index

Let us see a sample example to understand the working of the PostgreSQL Partial Index.

We are creating one new table as Buyer with the CREATE command's help and inserting some values using the INSERT command.

To create Clients into an Organization database, we use the CREATE command:

Output

After implementing the above command, we will get the following message window, which displays that the Buyer table has been created successfully into the Organization table.

PoatgreSQL Partial Index

After creating the Buyer table successfully, we will enter some values into it with the INSERT command's help.

Output

After implementing the above command, we will get the following message window, which displays that the particular values have been inserted successfully into the Buyer table.

PoatgreSQL Partial Index

Typically, if we are interested in inactive buyers and frequently do some follow-ups to get them back to purchase more items.

In the below command, we are trying to identify all those buyers who are inactive:

Output

After successfully implementing the above command, we will get the below result, which displays all the buyer information who are inactive:

PoatgreSQL Partial Index

In the below command, we are using the EXPLAIN keyword to implement the above command as the query planner needs to scan the Buyer table:

Output

After implementing the above command, we will get the following output, which shows the Buyer table's Query plan.

PoatgreSQL Partial Index

Here, we can also enhance the command by creating an index for the active column, as shown in the following command:

Output

We will get the following message on executing the above command: the index has idex_buyer_active been created successfully

PoatgreSQL Partial Index

The above-created index satisfies its purpose, but it also contains several rows, which are never searched, specifically all the active buyers.

In the below command, we are specifying the index, which includes only inactive buyers:

Output

After implementing the above command, we will get the following message: the idex_buyer_inactive index has been created successfully.

PoatgreSQL Partial Index

Hence, PostgreSQL will use partial index when the WHERE clause appears in a command, as shown in the following command:

Output

We will get the following message on executing the above command, which shows the Buyer table's Query plan as the PostgreSQL optimizer is using the idex_buyer_inactive index.

PoatgreSQL Partial Index

Overview

In the PostgreSQL Partial Index section, we have understood the working of PostgreSQL partial index and how to define the rows added to the index.


Next TopicPostgreSQL REINDEX




Contact US

Email:[email protected]

PostgreSQL Partial Index
10/30