Skip to content

Materialized View Operations ​

Operation: createMaterializedView ​

pgm.createMaterializedView( viewName, options, definition ) ​

IMPORTANT

Create a new materialized view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the new materialized view
optionsobjectCheck below for available options
definitionstringSQL of SELECT statement

Options ​

OptionTypeDescription
ifNotExistsbooleanadds IF NOT EXISTS clause
columnsstring or arrayuse if you want to name columns differently then inferred from definition
tablespacestringoptional
storageParametersobjectoptional key value pairs of Storage Parameters
databooleandefault undefined

Storage parameter names are quoted and folded to lowercase, without camel-case conversion. Dotted names specify a namespace and parameter, for example 'toast.autovacuum_enabled': false. Values may be booleans or finite numbers:

js
pgm.createMaterializedView(
  'account_summary',
  { storageParameters: { fillfactor: 70, 'toast.autovacuum_enabled': false } },
  'SELECT * FROM accounts'
);

NaN, Infinity, and -Infinity throw a TypeError naming the option and value during SQL generation, before SQL for that operation is queued. Finite numbers, including negatives, zero, and decimals, are rendered unchanged; PostgreSQL still validates parameter-specific ranges and supported values.

Reverse Operation: dropMaterializedView ​

pgm.dropMaterializedView( viewName, options ) ​

IMPORTANT

Drop a materialized view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the view to delete
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
ifExistsbooleandrops view only if it exists
cascadebooleandrops also dependent objects

Operation: alterMaterializedView ​

pgm.alterMaterializedView( viewName, options ) ​

IMPORTANT

Alter a materialized view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the view to alter
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
clusterstringoptional index name for clustering
extensionstringoptional name of extension view is dependent on
storageParametersobjectoptional key value (null to reset) pairs of Storage Parameters

storageParameters uses the same name escaping and finite-number validation as createMaterializedView. Use null to reset a parameter, including namespaced parameters such as { 'toast.autovacuum_enabled': null }; other entries are applied with SET.

Operation: renameMaterializedView ​

pgm.renameMaterializedView( viewName, newViewName ) ​

IMPORTANT

Rename a materialized view - postgres docs

Arguments ​

NameTypeDescription
viewNameNameold name of the view
newViewNameNamenew name of the view

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

To move a materialized view between schemas, use SQL explicitly, for example pgm.sql('ALTER MATERIALIZED VIEW "old_schema"."my_view" SET SCHEMA "new_schema"'). Provide the corresponding SQL in your down migration to reverse that move.

Operation: alterMaterializedViewColumn ​

pgm.renameMaterializedViewColumn( viewName, columnName, newColumnName ) ​

IMPORTANT

Rename a materialized view column - postgres docs

Arguments ​

NameTypeDescription
viewNamestringname of the view to alter
columnNamestringcurrent column name
newColumnNamestringnew column name

Operation: refreshMaterializedView ​

pgm.refreshMaterializedView( viewName, options ) ​

IMPORTANT

Refreshes a materialized view - postgres docs

Arguments ​

NameTypeDescription
viewNamestringName of the view to refresh
optionsobjectCheck below for available options

Options ​

OptionTypeDescription
concurrentlybooleandefault false
databooleandefault undefined