community.postgresql.postgresql_idx 模块 – 从 PostgreSQL 数据库创建或删除索引

注意

此模块是 community.postgresql 集合(版本 3.9.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install community.postgresql。您需要进一步的要求才能使用此模块,请参阅 要求 以了解详细信息。

要在 playbook 中使用它,请指定:community.postgresql.postgresql_idx

概要

  • 从 PostgreSQL 数据库创建或删除索引。

要求

在执行此模块的主机上需要以下要求。

  • psycopg2 >= 2.5.1

参数

参数

注释

ca_cert

别名:ssl_rootcert

字符串

指定包含 SSL 证书颁发机构 (CA) 证书的文件名。

如果文件存在,则将验证服务器的证书是否由这些机构之一签名。

cascade

布尔值

自动删除依赖于索引的对象,并依次删除所有依赖于这些对象的对象。

仅与 state=absent 一起使用。

concurrent=true 互斥。

选择

  • false ← (默认)

  • true

columns

别名:column

列表 / 元素=字符串

需要索引覆盖的索引列列表。

state=absent 互斥。

concurrent

布尔值

启用或禁用并发模式(CREATE / DROP INDEX CONCURRENTLY)。

请注意,如果 concurrent=false,则在构建过程中会锁定表 (ACCESS EXCLUSIVE)。有关锁级别的更多信息,请参阅 https://postgresql.ac.cn/docs/current/explicit-locking.html

如果构建过程因任何原因在 cuncurrent=true 时中断,则索引将变为无效。在这种情况下,应删除并重新创建。

cascade=true 互斥。

选择

  • false

  • true ← (默认)

cond

字符串

索引条件。

state=absent 互斥。

connect_params

字典

在 community.postgresql 2.3.0 中添加

要传递给 libpg 的任何其他参数。

这些参数优先。

默认: {}

db

别名:login_db

字符串

要连接到的数据库的名称,以及将在其中创建/删除索引的数据库的名称。

idxname

别名:name

字符串 / 必需

要创建或删除的索引的名称。

idxtype

别名:type

字符串

索引类型(如 btree、gist、gin 等)。

state=absent 互斥。

login_host

别名:host

字符串

运行数据库的主机。

如果在使用 localhost 时遇到连接问题,请尝试使用 127.0.0.1

默认: ""

login_password

字符串

此模块应用于建立 PostgreSQL 会话的密码。

默认: ""

login_unix_socket

别名:unix_socket

字符串

用于本地连接的 Unix 域套接字路径。

默认: ""

login_user

别名: login

字符串

此模块应使用的用户名,以建立其 PostgreSQL 会话。

默认值: "postgres"

port

别名: login_port

整数

要连接的数据库端口。

默认值: 5432

schema

字符串

将在其中创建索引的数据库模式的名称。

session_role

字符串

连接后切换到 session_role。指定的 session_role 必须是当前 login_user 所属的角色。

SQL 命令的权限检查将像 session_role 最初登录一样执行。

ssl_cert

路径

在 community.postgresql 2.4.0 中添加

指定客户端 SSL 证书的文件名。

ssl_key

路径

在 community.postgresql 2.4.0 中添加

指定用于客户端证书的密钥的位置。

ssl_mode

字符串

确定是否以及以何种优先级与服务器协商安全的 SSL TCP/IP 连接。

有关模式的更多信息,请参见 https://postgresql.ac.cn/docs/current/static/libpq-ssl.html

默认值 prefer 与 libpq 默认值匹配。

选择

  • "allow"

  • "disable"

  • "prefer" ← (默认值)

  • "require"

  • "verify-ca"

  • "verify-full"

state

字符串

索引状态。

present 表示如果索引不存在,则会创建该索引。

absent 表示如果索引存在,则会删除该索引。

选择

  • "absent"

  • "present" ← (默认值)

storage_params

列表 / 元素=字符串

存储参数,如 fillfactor、vacuum_cleanup_index_scale_factor 等。

state=absent 互斥。

table

字符串

在其上创建索引的表。

state=absent 互斥。

tablespace

字符串

为索引设置表空间。

state=absent 互斥。

trust_input

布尔值

在 community.postgresql 0.2.0 中添加

如果为 false,则检查参数 idxnamesession_roleschematablecolumnstablespacestorage_paramscond 的值是否可能存在危险。

仅当通过参数进行 SQL 注入时才可以使用 false

选择

  • false

  • true ← (默认)

unique

布尔值

在 community.postgresql 0.2.0 中添加

启用唯一索引。

目前只有 btree 支持唯一索引。

选择

  • false ← (默认)

  • true

属性

属性

支持

描述

check_mode

支持: 完整

可以在 check_mode 中运行并返回更改状态预测,而无需修改目标。

注释

注意

  • 索引构建过程可能会影响数据库性能。

  • 为了避免生产数据库上的表锁,请使用 concurrent=true(默认行为)。

  • 默认身份验证假设您正在以主机上的 postgres 帐户登录或 sudo 切换到该帐户。

  • 为了避免“用户 postgres 的对等身份验证失败”错误,请使用 postgres 用户作为 become_user

  • 此模块使用 psycopg,这是一个 Python PostgreSQL 数据库适配器。您必须确保在使用此模块之前,主机上已安装 psycopg2 >= 2.5.1psycopg3 >= 3.1.8

  • 如果远程主机是 PostgreSQL 服务器(默认情况下),则还必须在远程主机上安装 PostgreSQL。

  • 对于基于 Ubuntu 的系统,请在使用此模块之前在远程主机上安装 postgresqllibpq-devpython3-psycopg2 软件包。

另请参阅

另请参阅

community.postgresql.postgresql_table

创建、删除或修改 PostgreSQL 表。

community.postgresql.postgresql_tablespace

从远程主机添加或删除 PostgreSQL 表空间。

PostgreSQL 索引参考

有关 PostgreSQL 索引的常规信息。

CREATE INDEX 参考

CREATE INDEX 命令文档的完整参考。

ALTER INDEX 参考

ALTER INDEX 命令文档的完整参考。

DROP INDEX 参考

DROP INDEX 命令文档的完整参考。

示例

- name: Create btree index if not exists test_idx concurrently covering columns id and name of table products
  community.postgresql.postgresql_idx:
    db: acme
    table: products
    columns: id,name
    name: test_idx

- name: Create btree index test_idx concurrently with tablespace called ssd and storage parameter
  community.postgresql.postgresql_idx:
    db: acme
    table: products
    columns:
    - id
    - name
    idxname: test_idx
    tablespace: ssd
    storage_params:
    - fillfactor=90

- name: Create gist index test_gist_idx concurrently on column geo_data of table map
  community.postgresql.postgresql_idx:
    db: somedb
    table: map
    idxtype: gist
    columns: geo_data
    idxname: test_gist_idx

# Note: for the example below pg_trgm extension must be installed for gin_trgm_ops
- name: Create gin index gin0_idx not concurrently on column comment of table test
  community.postgresql.postgresql_idx:
    idxname: gin0_idx
    table: test
    columns: comment gin_trgm_ops
    concurrent: false
    idxtype: gin

- name: Drop btree test_idx concurrently
  community.postgresql.postgresql_idx:
    db: mydb
    idxname: test_idx
    state: absent

- name: Drop test_idx cascade
  community.postgresql.postgresql_idx:
    db: mydb
    idxname: test_idx
    state: absent
    cascade: true
    concurrent: false

- name: Create btree index test_idx concurrently on columns id,comment where column id > 1
  community.postgresql.postgresql_idx:
    db: mydb
    table: test
    columns: id,comment
    idxname: test_idx
    cond: id > 1

- name: Create unique btree index if not exists test_unique_idx on column name of table products
  community.postgresql.postgresql_idx:
    db: acme
    table: products
    columns: name
    name: test_unique_idx
    unique: true
    concurrent: false

返回值

常见返回值在此处记录 此处,以下是此模块特有的字段

描述

name

字符串

索引名称。

返回值: 成功

示例: "foo_idx"

query

字符串

尝试执行的查询。

返回值: 成功

示例: "CREATE INDEX CONCURRENTLY foo_idx ON test_table USING BTREE (id)"

schema

字符串

索引存在的模式。

返回值: 成功

示例: "public"

state

字符串

索引状态。

返回值: 成功

示例: "present"

storage_params

列表 / 元素=字符串

索引存储参数。

返回值: 成功

示例: ["fillfactor=90"]

tablespace

字符串

索引存在的表空间。

返回值: 成功

示例: "ssd"

valid

布尔值

索引有效性。

返回值: 成功

示例: true

作者

  • Andrew Klychkov (@Andersson007)

  • Thomas O’Donnell (@andytom)