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

NOTE

Bare names containing only ASCII letters, digits, non-ASCII characters, _, . and - are quoted as column identifiers; $ is also allowed after the first character. This includes Straße, 日本語, größe-id and [{ name: 'größe-id' }]. Case is preserved unless decamelize is enabled. For compatibility, names containing " are also quoted as identifiers when their other characters are only ASCII letters, digits, _ or .; the quotes are treated as part of the column name. For example, a"b names the column "a""b", while größe"id follows the SQL expression path. To reference the latter column, use explicitly quoted SQL such as ("größe""id"). Other unquoted inputs are treated as SQL expressions, with parentheses added when needed: a+b, a*b, a/b and a - b remain expressions. To make subtraction explicit, use a - b or (a-b); an explicit index name is recommended for string expressions, but is not required. Function calls, JSON expressions and dollar-quoted SQL strings retain their existing behavior.

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 | brin
includestring or array[string]columns to add to the include clause
nullsstringdistinct | not distinct (for unique indexes only)
storageParametersobjectindex storage parameters for the WITH clause; values can be strings, numbers, or booleans

Storage parameter names and string values are escaped and used as supplied, without decamelization. An empty storageParameters object omits the WITH clause. PostgreSQL validates which parameters and values the selected index method supports; see Index Storage Parameters.

The exported IndexStorageParameters type can be used for reusable parameter maps.

When using concurrently: true, call pgm.noTransaction() in the migration. Regular index creation supports transactions, including when storage parameters are specified.

Examples ​

ts
pgm.createIndex('table', 'column');
//expected output: CREATE INDEX ON "table" ("column")
ts
pgm.createIndex('measurements', 'a-b', {
  name: 'unique_measurement',
  unique: true,
});
//expected output: CREATE UNIQUE INDEX "unique_measurement" ON "measurements" ("a-b")
ts
pgm.createIndex('measurements', '(a-b)', { name: 'measurement_difference' });
//expected output: CREATE INDEX "measurement_difference" ON "measurements" ((a-b))
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)
ts
pgm.createIndex('films', 'title', {
  name: 'title_idx',
  storageParameters: { fillfactor: 70, deduplicate_items: false },
});
//expected output: CREATE INDEX "title_idx" ON "films" ("title") WITH ("fillfactor" = 70, "deduplicate_items" = false);
ts
pgm.createIndex('locations', 'position', {
  name: 'position_idx',
  method: 'gist',
  storageParameters: { buffering: 'off' },
});
//expected output: CREATE INDEX "position_idx" ON "locations" USING gist ("position") WITH ("buffering" = $pga$off$pga$);
ts
pgm.createIndex('events', 'created_at', {
  method: 'brin',
  storageParameters: { pages_per_range: 32, autosummarize: true },
});
//expected output: CREATE INDEX "events_created_at_index" ON "events" USING brin ("created_at") WITH ("pages_per_range" = 32, "autosummarize" = true);

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;

Renaming preserves the source schema, including during automatic reversal. See Renaming and schemas for destination-schema validation and supported PgLiteral identifiers.

Renaming an index does not move it to another schema. PostgreSQL does not provide ALTER INDEX ... SET SCHEMA; indexes follow their table when it changes schema.