PostgreSQL – Multicolumn Indexes
Last Updated :
17 Jul, 2024
In PostgreSQL, multicolumn indexes, also known as composite indexes, combined indexes, or concatenated indexes, are indexes defined on more than one column of a table. These indexes can significantly improve query performance by allowing the database to quickly locate rows based on multiple column values.
Let us better understand the Multicolumn Indexes in PostgreSQL from this article.
What are Multicolumn Indexes?
A multicolumn index is an index created on multiple columns of a table. PostgreSQL supports multicolumn indexes with a maximum of 32 columns, though this limit can be changed by modifying the ‘pg_config_manual.h'
file when building PostgreSQL. Only B-tree, GIST, GIN, and BRIN index types support multicolumn indexes.
Syntax
The following syntax shows how to create a multicolumn index:
CREATE INDEX index_name
ON table_name(a, b, c, ...);
Optimizing Multicolumn Index Usage
When defining a multicolumn index, one should place the columns which are often used in the WHERE clause at the beginning of the column list and the columns that are less frequently used in the condition after.
The PostgreSQL optimizer considers using the index in the following cases:
- ‘WHERE a = v1 and b = v2 and c = v3‘;
- ‘WHERE a = v1 and b = v2‘;
- ‘WHERE a = v1‘;
However, it will not consider using the index in the following cases:
- ‘WHERE c = v3;’
- ‘WHERE b = v2 and c = v3;’
PostgreSQL Multicolumn Indexes Example
To demonstrate multicolumn indexes, we will create a new table named ‘people’ with three columns: id, ‘first_name’, and ‘last_name’:
CREATE TABLE people(
id INT GENERATED BY DEFAULT AS IDENTITY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL
);
You can add data to the people through this file.
The following statement finds people whose last name is Adams.
Query Without Index:
SELECT
*
FROM
people
WHERE
last_name = 'Adams';
This will result in the following:

As shown clearly in the output, PostgreSQL performed the sequential scan on the ‘people’ table to find the corresponding rows because there was no index defined for the ‘last_name’ column.
Let’s define a B-tree index on both ‘last_name’ and ‘first_name’ columns. Assuming that searching for people by the last name is more often than by the first name, we define the index with the following column order.
CREATE INDEX idx_people_names
ON people (last_name, first_name);
Now, if you search for people whose last name is Adams, the PostgreSQL optimizer will use the index as shown in the output of the following statement:
Query With Index:
EXPLAIN SELECT *
FROM
people
WHERE
last_name = 'Adams';
This will output the below:

The following statement finds the person whose last name is Adams and the first name is Lou.
Complex Query With Index:
SELECT
*
FROM
people
WHERE
last_name = 'Adams'
AND first_name = 'Lou';
This will lead to the following:

The PostgreSQL Optimizer used the index for this statement because both columns in the WHERE clause are all in the index.
EXPLAIN SELECT *
FROM
people
WHERE
last_name = 'Adams'
AND first_name = 'Lou';
However, if you search for people whose first name is Lou, PostgreSQL will perform sequential scan the table instead of using the index as shown in the output of the following statement.
Query Not Using the Index:
EXPLAIN SELECT *
FROM
people
WHERE
first_name = 'Lou';
Output:

Important Points About PostgreSQL Multicolumn Indexes
- The order of columns in a multicolumn index is crucial. Place the most frequently queried columns first to ensure the index is used effectively.
- Multicolumn indexes can be large and may require additional maintenance. Regularly analyze and vacuum your database to maintain performance.
- Ensure that the indexed columns have high selectivity. Columns with low selectivity (many duplicate values) may not benefit from indexing.
- While multicolumn indexes can improve query performance, they also add overhead to write operations (INSERT, UPDATE, DELETE).
- Always use the
EXPLAIN
command to analyze how PostgreSQL executes your queries.
Similar Reads
PostgreSQL - List Indexes
Indexes in PostgreSQL are crucial for optimizing query performance, helping speed up data retrieval by allowing faster access to rows in a table. PostgreSQL does not provide a direct SHOW INDEXES command like some other databases; however, you can use the pg_indexes view and the psql command line to
4 min read
PostgreSQL - ADD COLUMN
In PostgreSQL, the ADD COLUMN statement is a powerful command used to modify an existing database table by adding one or more new columns. This feature is important for adapting table structures to meet evolving data requirements, and it plays a key role in database management and optimization. In t
5 min read
PostgreSQL - Partial Index
In PostgreSQL, partial indexes are a powerful feature designed to improve query performance while minimizing index size. By allowing you to specify the rows of a table that should be indexed, partial indexes can significantly speed up queries that use common WHERE conditions with constant values. Le
2 min read
PostgreSQL - Size of Indexes
In PostgreSQL, index management is essential for optimizing query performance and ensuring efficient database storage. One important function for assessing the storage requirements of table indexes is the pg_indexes_size() function. In this article, we will explain the pg_indexes_size() function, it
4 min read
PostgreSQL - Change Column Type
Changing the column type in PostgreSQL is good for adapting to increase the data needs. Using the ALTER TABLE statement, we can modify the structure of existing tables easily. The PostgreSQL ALTER COLUMN syntax allows us to change a columns data type while maintaining data integrity and performance.
5 min read
PostgreSQL - DROP COLUMN
In PostgreSQL, there are instances where you might need to remove unnecessary or obsolete columns from your database tables. The DROP COLUMN clause in the ALTER TABLE statement allows you to do this with ease. When you drop a column from a table, PostgreSQL automatically removes any associated index
2 min read
PostgreSQL - LEFT JOIN
In PostgreSQL, the LEFT JOIN (or LEFT OUTER JOIN) is a powerful tool that allows you to merge data from two tables based on a related column. With a LEFT JOIN, you get all records from the "left" table and matching records from the "right" table. If thereâs no match in the right table, NULL values w
5 min read
PostgreSQL - INSERT
PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
5 min read
PostgreSQL - Index On Expression
When working with databases, optimizing query performance is crucial, especially when dealing with large datasets. One powerful technique in PostgreSQL is leveraging indexes on expressions. This approach allows you to optimize queries that involve expressions, ensuring faster retrieval times and eff
3 min read
PostgreSQL - DROP INDEX
In PostgreSQL, indexes are essential for improving query performance but sometimes we may need to remove them when they are no longer effective or necessary. This is where the DROP INDEX statement comes in. It allows us to delete an existing index from the database, ensuring that our PostgreSQL envi
5 min read