Skip to content

Index Operations

Operation: createIndex

pgm.createIndex( tablename, columns, options )

IMPORTANT

Create a new index - postgres docs Alias: addIndex

Arguments

NameTypeDescription
tablenameNamename of the table to alter
columnsstring or array[string]columns to add to the index with optional operator class and sort
optionsobjectCheck below for available options

Options

OptionTypeDescription
namestringname for the index (one will be inferred from table/columns if undefined)
uniquebooleanset to true if this is a unique index
wherestringraw sql for where clause of index
concurrentlybooleancreate this index concurrently
ifNotExistsbooleandefault false
methodstringbtree | hash | gist | spgist | gin
includestring or array[string]columns to add to the include clause
nullsstringdistinct | not distinct (for unique indexes only)

Examples

ts
pgm.createIndex('table', 'column');
//expected output: CREATE INDEX ON "table" ("column")
ts
pgm.createIndex('table', ['col1', 'col2']);
//expected output: CREATE INDEX ON "table" ("col1", "col2")
ts
pgm.createIndex('table', [
  { name: 'col1', sort: 'ASC' },
  { name: 'col2', sort: 'DESC' },
]);
//expected output: CREATE INDEX ON "table" ("col1" ASC, "col2" DESC)
ts
pgm.createIndex('table', [
  { name: 'col1', opclass: { schema: 'schema', name: 'opclass' }, sort: 'ASC' },
]);
//expected output: CREATE INDEX ON "table" ("col1" "schema"."opclass" ASC)

Reverse Operation: dropIndex

pgm.dropIndex( tablename, columns, options )

IMPORTANT

Drop an index - postgres docs

Arguments

NameTypeDescription
tablenameNamename of the table to alter
columnsstring or array[string]column names, used only to infer an index name
optionsobjectCheck below for available options

Options

OptionTypeDescription
namestringname of the index to drop
concurrentlybooleandrop this index concurrently
ifExistsbooleandefault false
cascadebooleandefault false

Operation: renameIndex

pgm.renameIndex( name, newName )

IMPORTANT

Rename an index - postgres docs

Reverse Operation: same operation in opposite direction

Arguments

NameTypeDescription
nameNamename of the index to rename
newNameNamenew name for the index

Examples

ts
pgm.renameIndex('index_name', 'new_index_name');
//expected output: ALTER INDEX index_name RENAME TO new_index_name;