How to Use PostgreSQL List Index
Introduction
If you’re using indexes on your PostgreSQL tables, you’ll probably want to know how to obtain a list of these indexes. There’s no such thing as a PostgreSQL LIST INDEX command; instead, we can use the pg_indexes
view to accomplish this task. This tutorial will show you how to list indexes in PostgreSQL using both the pg_indexes
view and the psql
utility.
Prerequisites
In order to follow along with the examples we discuss in this tutorial, you’ll need to have PostgreSQL installed and configured. This process should be running in the background. If you’re running either Linux or Windows, you can download PostgreSQL here.
What is a PostgreSQL Index?
PostgreSQL indexes are used to speed up database searches. You can think of a PostgreSQL index as a pointer to some particular piece of data within a PostgreSQL database table.
If you want to learn how to create an index in PostgreSQL, you can reference this article on PostgreSQL Create Index.
pg_indexes in PostgreSQL
As we mentioned earlier, PostgreSQL does not have a direct command to list information on a particular database or database table; however, it does provide the pg_indexes
view, which allows us to query for index information. The pg_indexes
view offers useful information on every index within a particular database in PostgreSQL. This view is composed of five columns:
- The
schemaname
: This column stores the schema name that contains indexes and tables. tablename
: This column stores the table name for which the index was created.indexname
: As the name implies, this column stores the index name.tablespace
: This column holds the tablespace that contains the indexes.indexdef
: This column contains the index definition in the form of aCREATE INDEX
statement.
Let’s use the pg_indexes
view to see if we can get information about our ‘public’ schema. To do this, we’ll be using a query that will list all indexes of the schema within the current database. However, we first have to log in to our server as the superuser postgres
, using the following command:
1 | psql postgres |
Then, we can use the following query:
1 2 3 4 5 6 7 8 9 10 11 | SELECT tablename, indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' ORDER BY tablename, indexname; |
In our results, we should see something like the following:
1 2 3 4 5 6 7 8 | tablename | indexname | indexdef ---------------+--------------------+------------------------------------------------------------------------------------------ client_info | client_info_pkey | CREATE UNIQUE INDEX client_info_pkey ON public.client_info USING btree (client_id) customer_info | customer_info_pkey | CREATE UNIQUE INDEX customer_info_pkey ON public.customer_info USING btree (customer_id) hardware | hardware_pkey | CREATE UNIQUE INDEX hardware_pkey ON public.hardware USING btree (hw_id) typhoon_info | typhoon_info_pkey | CREATE UNIQUE INDEX typhoon_info_pkey ON public.typhoon_info USING btree (ty_id) zoo | zoo_pkey | CREATE UNIQUE INDEX zoo_pkey ON public.zoo USING btree (animal_id) (5 rows) |
We can also show the existing indexes within a table using the following query:
1 2 3 4 5 6 7 | SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'client_info'; |
In the query shown above, we will only be retrieving the indexname
and indexdef
(index definition) columns from the table client_info
.
The results will look something like this:
1 2 3 4 | indexname | indexdef ------------------+------------------------------------------------------------------------------------ client_info_pkey | CREATE UNIQUE INDEX client_info_pkey ON public.client_info USING btree (client_id) (1 ROW) |
We can also perform a SELECT
operation using the LIKE
clause, where we retrieve any indexes within the table with a name whose first letter matches certain criteria.
Before we do this, let’s take a step back and list all the tables in our database using the PostgreSQL meta-command \dt
.
Shown below is the result of this command:
1 2 3 4 5 6 7 8 9 | List OF relations Schema | Name | TYPE | Owner --------+---------------+-------+------- public | client_info | TABLE | risa public | customer_info | TABLE | risa public | hardware | TABLE | risa public | typhoon_info | TABLE | risa public | zoo | TABLE | risa (5 ROWS) |
Now, let’s perform our query:
1 2 3 4 5 6 7 8 | SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename LIKE 'c%' |
The above query will retrieve values of tablename
, indexname
and the indexdef
from any tables having a name that starts with the character c.
Since we have two tables with a name that starts with “c”, our results will look like this:
1 2 3 4 5 | tablename | indexname | indexdef ---------------+--------------------+------------------------------------------------------------------------------------------ customer_info | customer_info_pkey | CREATE UNIQUE INDEX customer_info_pkey ON public.customer_info USING btree (customer_id) client_info | client_info_pkey | CREATE UNIQUE INDEX client_info_pkey ON public.client_info USING btree (client_id) (2 ROWS) |
psql meta-command d in PostgreSQL
Now that we’ve seen how to use the pg_indexes
view to list PosgtreSQL indexes for a table, we’ll look at another way to accomplish the same task using the PostgreSQL meta-command d.
If we connect to a PostgreSQL database using the psql
command-line utility, we can list all existing indexes within a table using the command shown below. Be sure you’ve logged into your PostgreSQL database using psql
before trying this command:
1 | \d your_table_name |
Here’s an example of this command used against the table client_info
:
1 | \d client_info; |
The output of this command should look something like the following:
1 2 3 4 5 6 7 8 9 10 | TABLE "public.client_info" COLUMN | TYPE | Collation | NULLABLE | DEFAULT ------------------+---------+-----------+----------+--------- client_id | INTEGER | | NOT NULL | client_firstname | text | | NOT NULL | client_lastname | text | | NOT NULL | client_age | INTEGER | | NOT NULL | client_email | text | | NOT NULL | Indexes: "client_info_pkey" PRIMARY KEY, btree (client_id) |
We can see in this output that the index is listed in the “Indexes” section.
Conclusion
When you’re managing various tables and indexes in PostgreSQL, it’s important to be able to obtain information on all of your indexes. Although there’s no PostgreSQL LIST INDEX command to provide a listing of indexes, two other simple methods are available to return the information you need. Using the examples in this tutorial as a guide, you’ll be able to retrieve index information for any of your PostgreSQL tables.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started