CREATE INDEX v15

Name

CREATE INDEX Define a new index.

Synopsis

CREATE [ UNIQUE ] INDEX <name> ON <table>
  ( { <column> | ( <expression> ) | <constant> } )
  [ TABLESPACE <tablespace> ]
  ( { NOPARALLEL | PARALLEL [ <integer> ] } )

Description

CREATE INDEX constructs an index, name, on the specified table. Indexes are primarily used to enhance database performance. However, inappropriate use results in slower performance.

The key fields for the index are specified as column names, constants, or as expressions written in parentheses. You can specify multiple fields to create multicolumn indexes.

An index field can be an expression computed from the values of one or more columns of the table row. You can use this feature to get fast access to data based on some transformation of the basic data. For example, an index computed on UPPER(col) allows the clause WHERE UPPER(col) = 'JIM' to use an index.

EDB Postgres Advanced Server provides the B-tree index method. The B-tree index method is an implementation of Lehman-Yao high-concurrency B-trees.

By default, indexes aren't used for IS NULL clauses.

All functions and operators used in an index definition must be immutable. That is, their results must depend only on their arguments and never on any outside influence, such as the contents of another table or the current time. This restriction ensures that the behavior of the index is well-defined. To use a user-defined function in an index expression, mark the function immutable when you create it.

If you create an index on a partitioned table, the CREATE INDEX command propagates indexes to the table’s subpartitions.

The PARALLEL clause specifies the degree of parallelism used while creating an index. The NOPARALLEL clause resets the parallelism to the default value. reloptions shows the parallel_workers parameter as 0.

Note

If you use the CREATE INDEX... PARALLEL command to create an index on a table whose definition included the PARALLEL clause at creation, the server uses the PARALLEL clause provided with the CREATE INDEX command when building a parallel index.

Parameters

UNIQUE

Causes the system to check for duplicate values in the table when the index is created, if data already exists, and each time data is added. Attempts to insert or update data that would result in duplicate entries generates an error.

name

The name of the index to create. You can't create a schema name here. The index is always created in the same schema as its parent table.

table

The name (possibly schema-qualified) of the table to index.

column

The name of a column in the table.

expression

An expression based on one or more columns of the table. Usually, you must write the expression surrounded by parentheses, as shown in the syntax. However, you can omit the parentheses if the expression has the form of a function call.

constant

A constant value that you can use as an index key.

Normally, a row where all indexed columns are NULL isn't included in an index. That means that the optimizer can't use that index for certain queries. To overcome this limitation, you can add a constant to the index, which forces the index to never contain a row where all index columns are NULL.

tablespace

The tablespace in which to create the index. If not specified, default_tablespace is used, or the database’s default tablespace if default_tablespace is an empty string.

PARALLEL

Specify PARALLEL to select a degree of parallelism. Set parallel_workers parameter equal to the degree of parallelism to create a parallelized index. Alternatively, if you specify PARALLEL but don't list the degree of parallelism, an index accepts default parallelism.

NOPARALLEL

Specify NOPARALLEL for default execution.

integer

The integer indicates the degree of parallelism, which is a number of parallel_workers used in the parallel operation to perform a parallel scan on an index.

Notes

You can specify up to 32 fields in a multicolumn index.

Examples

To create a B-tree index on the column ename in the table emp:

CREATE INDEX name_idx ON emp (ename);

To create the same index but have it reside in the index_tblspc tablespace:

CREATE INDEX name_idx ON emp (ename) TABLESPACE index_tblspc;

You can include a constant value in the index definition (1) to create an index that never contains a row where all of the indexed columns are NULL:

CREATE INDEX emp_dob_idx on emp (emp_dob, 1);

To create an index on name_idx in the table emp with degree of parallelism set to 7:

CREATE UNIQUE INDEX name_idx ON emp (ename) PARALLEL 7;

See also

ALTER INDEX, DROP INDEX