PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
Creating an index on a table is essential for improving the performance of query operations, especially for tables with a significant number of rows. An index provides a faster lookup path to the rows in a table based on the values of one or more columns.
In PostgreSQL, the CREATE INDEX
command is used to create an index on one or more columns of a table.
CREATE INDEX index_name ON table_name (column_name [ASC|DESC] [NULLS {FIRST | LAST }], ...);
index_name
: Name of the index.table_name
: Name of the table on which the index is to be created.column_name
: Name of the column(s) on which the index is to be created.ASC|DESC
: Specifies the sort order for the indexed column. (Optional)NULLS FIRST|LAST
: Determines the sort order for null values. (Optional)To create an index on the email
column of a users
table:
CREATE INDEX idx_users_email ON users (email);
To create a composite index on multiple columns, you can list multiple columns separated by commas:
CREATE INDEX idx_users_firstname_lastname ON users (firstname, lastname);
You can also create an index on the result of an expression:
CREATE INDEX idx_users_lower_email ON users (LOWER(email));
This creates an index on the lowercase version of the email
column, which might be useful if you frequently search for email addresses without case sensitivity.
If you want to ensure that the values in the indexed column(s) are unique, you can create a unique index:
CREATE UNIQUE INDEX idx_users_unique_email ON users (email);
PostgreSQL supports several index types: B-tree, Hash, GiST, SP-GiST, GIN, and BRIN. Each type is suited for particular use cases. By default, the B-tree index type is used. However, you can specify a different type using the USING
clause:
CREATE INDEX idx_users_email_gin ON users USING GIN (email);
A partial index is an index built over a subset of a table; the subset is defined by a conditional expression provided in the WHERE
clause:
CREATE INDEX idx_active_users_email ON users (email) WHERE status = 'active';
Creating an index can be resource-intensive and can lock the table, making it unavailable for write operations for the duration of the index creation. For large tables, consider using the CONCURRENTLY
option, which allows writes to the table while the index is being built:
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);
Indexes can significantly speed up query performance but also come with some overhead during data modification (INSERT, UPDATE, DELETE) and use disk space. Always monitor the system's performance and storage.
Periodically, you should check for unused or duplicate indexes which can be dropped to reclaim space and reduce the overhead during data modifications.
Remember to analyze the query patterns and the table's data distribution before creating an index. The decision to index a particular column or set of columns should be based on the queries you run most frequently.
Creating indexes on single columns in PostgreSQL:
CREATE INDEX your_index_name ON your_table (your_column);
Composite indexes with CREATE INDEX in PostgreSQL:
CREATE INDEX your_composite_index ON your_table (column1, column2);
Unique indexes and constraints with CREATE INDEX in PostgreSQL:
CREATE UNIQUE INDEX your_unique_index ON your_table (your_column);
Partial indexes in PostgreSQL:
CREATE INDEX your_partial_index ON your_table (your_column) WHERE condition;
Concurrent index creation in PostgreSQL:
CREATE INDEX CONCURRENTLY your_index_name ON your_table (your_column);
Indexing expressions and functions in PostgreSQL:
CREATE INDEX your_expression_index ON your_table (LOWER(your_column));
Using DESC and ASC options with CREATE INDEX in PostgreSQL:
CREATE INDEX your_index_name ON your_table (your_column DESC);
B-tree vs Hash indexes in PostgreSQL:
CREATE INDEX your_btree_index ON your_table (your_column); CREATE INDEX your_hash_index ON your_table USING HASH (your_column);