Postgresql 中文操作指南

ALTER INDEX

ALTER INDEX — 更改索引的定义

ALTER INDEX — change the definition of an index

Synopsis

ALTER INDEX [ IF EXISTS ] name RENAME TO new_name
ALTER INDEX [ IF EXISTS ] name SET TABLESPACE tablespace_name
ALTER INDEX name ATTACH PARTITION index_name
ALTER INDEX name [ NO ] DEPENDS ON EXTENSION extension_name
ALTER INDEX [ IF EXISTS ] name SET ( storage_parameter [= value] [, ... ] )
ALTER INDEX [ IF EXISTS ] name RESET ( storage_parameter [, ... ] )
ALTER INDEX [ IF EXISTS ] name ALTER [ COLUMN ] column_number
    SET STATISTICS integer
ALTER INDEX ALL IN TABLESPACE name [ OWNED BY role_name [, ... ] ]
    SET TABLESPACE new_tablespace [ NOWAIT ]

Description

ALTER INDEX 更改现有索引的定义。下面描述了若干子格式。请注意,每个子格式都可能需要不同的锁定级别。在明确指出之前,将保持 ACCESS EXCLUSIVE 锁。当列出多个子命令时,保持的锁将是从任何子命令所要求的锁中最严格的锁。

ALTER INDEX changes the definition of an existing index. There are several subforms described below. Note that the lock level required may differ for each subform. An ACCESS EXCLUSIVE lock is held unless explicitly noted. When multiple subcommands are listed, the lock held will be the strictest one required from any subcommand.

  • RENAME

    • The RENAME form changes the name of the index. If the index is associated with a table constraint (either UNIQUE, PRIMARY KEY, or EXCLUDE), the constraint is renamed as well. There is no effect on the stored data.

    • Renaming an index acquires a SHARE UPDATE EXCLUSIVE lock.

  • SET TABLESPACE

    • This form changes the index’s tablespace to the specified tablespace and moves the data file(s) associated with the index to the new tablespace. To change the tablespace of an index, you must own the index and have CREATE privilege on the new tablespace. All indexes in the current database in a tablespace can be moved by using the ALL IN TABLESPACE form, which will lock all indexes to be moved and then move each one. This form also supports OWNED BY, which will only move indexes owned by the roles specified. If the NOWAIT option is specified then the command will fail if it is unable to acquire all of the locks required immediately. Note that system catalogs will not be moved by this command, use ALTER DATABASE or explicit ALTER INDEX invocations instead if desired. See also CREATE TABLESPACE.

  • ATTACH PARTITION

    • Causes the named index to become attached to the altered index. The named index must be on a partition of the table containing the index being altered, and have an equivalent definition. An attached index cannot be dropped by itself, and will automatically be dropped if its parent index is dropped.

  • DEPENDS ON EXTENSION _extension_nameNO DEPENDS ON EXTENSION _extension_name

    • This form marks the index as dependent on the extension, or no longer dependent on that extension if NO is specified. An index that’s marked as dependent on an extension is automatically dropped when the extension is dropped.

  • SET ( _storage_parameter [= value] [, …​ ] )_

    • This form changes one or more index-method-specific storage parameters for the index. See CREATE INDEX for details on the available parameters. Note that the index contents will not be modified immediately by this command; depending on the parameter you might need to rebuild the index with REINDEX to get the desired effects.

  • RESET ( _storage_parameter [, …​ ] )_

    • This form resets one or more index-method-specific storage parameters to their defaults. As with SET, a REINDEX might be needed to update the index entirely.

  • ALTER [ COLUMN ] _column_number SET STATISTICS integer_

    • This form sets the per-column statistics-gathering target for subsequent ANALYZE operations, though can be used only on index columns that are defined as an expression. Since expressions lack a unique name, we refer to them using the ordinal number of the index column. The target can be set in the range 0 to 10000; alternatively, set it to -1 to revert to using the system default statistics target (default_statistics_target). For more information on the use of statistics by the PostgreSQL query planner, refer to Section 14.2.

Parameters

  • IF EXISTS

    • Do not throw an error if the index does not exist. A notice is issued in this case.

  • column_number

    • The ordinal number refers to the ordinal (left-to-right) position of the index column.

  • name

    • The name (possibly schema-qualified) of an existing index to alter.

  • new_name

    • The new name for the index.

  • tablespace_name

    • The tablespace to which the index will be moved.

  • extension_name

    • The name of the extension that the index is to depend on.

  • storage_parameter

    • The name of an index-method-specific storage parameter.

  • value

    • The new value for an index-method-specific storage parameter. This might be a number or a word depending on the parameter.

Notes

还可以使用 ALTER TABLE 来执行这些操作。实际上, ALTER INDEX 只是适用于索引的 ALTER TABLE 形式的别名。

These operations are also possible using ALTER TABLE. ALTER INDEX is in fact just an alias for the forms of ALTER TABLE that apply to indexes.

以前有一个 ALTER INDEX OWNER 变体,但现在已忽略(并带有警告)。索引的所有者不能与其表的拥有者不同。更改表的所有者也会自动更改索引。

There was formerly an ALTER INDEX OWNER variant, but this is now ignored (with a warning). An index cannot have an owner different from its table’s owner. Changing the table’s owner automatically changes the index as well.

不允许更改系统目录索引的任何部分。

Changing any part of a system catalog index is not permitted.

Examples

若要重命名现有索引:

To rename an existing index:

ALTER INDEX distributors RENAME TO suppliers;

若要将索引移动到不同的表空间:

To move an index to a different tablespace:

ALTER INDEX distributors SET TABLESPACE fasttablespace;

若要更改索引的填充因子(假设索引方法支持它):

To change an index’s fill factor (assuming that the index method supports it):

ALTER INDEX distributors SET (fillfactor = 75);
REINDEX INDEX distributors;

设置表达式索引的统计信息收集目标:

Set the statistics-gathering target for an expression index:

CREATE INDEX coord_idx ON measured (x, y, (z + t));
ALTER INDEX coord_idx ALTER COLUMN 3 SET STATISTICS 1000;

Compatibility

ALTER INDEX 是一个 PostgreSQL 扩展。

ALTER INDEX is a PostgreSQL extension.