community.postgresql.postgresql_idx module – Create or drop indexes from a PostgreSQL database
Note
This module is part of the community.postgresql collection (version 3.8.0).
You might already have this collection installed if you are using the ansible
package.
It is not included in ansible-core
.
To check whether it is installed, run ansible-galaxy collection list
.
To install it, use: ansible-galaxy collection install community.postgresql
.
You need further requirements to be able to use this module,
see Requirements for details.
To use it in a playbook, specify: community.postgresql.postgresql_idx
.
Synopsis
Create or drop indexes from a PostgreSQL database.
Requirements
The below requirements are needed on the host that executes this module.
psycopg2 >= 2.5.1
Parameters
Parameter |
Comments |
---|---|
Specifies the name of a file containing SSL certificate authority (CA) certificate(s). If the file exists, the server’s certificate will be verified to be signed by one of these authorities. |
|
Automatically drop objects that depend on the index, and in turn all objects that depend on those objects. It used only with state=absent. Mutually exclusive with concurrent=true. Choices:
|
|
List of index columns that need to be covered by index. Mutually exclusive with state=absent. |
|
Enable or disable concurrent mode (CREATE / DROP INDEX CONCURRENTLY). Pay attention, if concurrent=false, the table will be locked (ACCESS EXCLUSIVE) during the building process. For more information about the lock levels see https://www.postgresql.org/docs/current/explicit-locking.html. If the building process was interrupted for any reason when cuncurrent=true, the index becomes invalid. In this case it should be dropped and created again. Mutually exclusive with cascade=true. Choices:
|
|
Index conditions. Mutually exclusive with state=absent. |
|
Any additional parameters to be passed to libpg. These parameters take precedence. Default: |
|
Name of database to connect to and where the index will be created/dropped. |
|
Name of the index to create or drop. |
|
Index type (like btree, gist, gin, etc.). Mutually exclusive with state=absent. |
|
Host running the database. If you have connection issues when using Default: |
|
The password this module should use to establish its PostgreSQL session. Default: |
|
Path to a Unix domain socket for local connections. Default: |
|
The username this module should use to establish its PostgreSQL session. Default: |
|
Database port to connect to. Default: |
|
Name of a database schema where the index will be created. |
|
Switch to session_role after connecting. The specified session_role must be a role that the current login_user is a member of. Permissions checking for SQL commands is carried out as though the session_role were the one that had logged in originally. |
|
Specifies the file name of the client SSL certificate. |
|
Specifies the location for the secret key used for the client certificate. |
|
Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. See https://www.postgresql.org/docs/current/static/libpq-ssl.html for more information on the modes. Default of Choices:
|
|
Index state.
Choices:
|
|
Storage parameters like fillfactor, vacuum_cleanup_index_scale_factor, etc. Mutually exclusive with state=absent. |
|
Table to create index on it. Mutually exclusive with state=absent. |
|
Set a tablespace for the index. Mutually exclusive with state=absent. |
|
If It makes sense to use Choices:
|
|
Enable unique index. Only btree currently supports unique indexes. Choices:
|
Attributes
Attribute |
Support |
Description |
---|---|---|
Support: full |
Can run in check_mode and return changed status prediction without modifying target. |
Notes
Note
The index building process can affect database performance.
To avoid table locks on production databases, use concurrent=true (default behavior).
The default authentication assumes that you are either logging in as or sudo’ing to the
postgres
account on the host.To avoid “Peer authentication failed for user postgres” error, use postgres user as a become_user.
This module uses
psycopg
, a Python PostgreSQL database adapter. You must ensure thatpsycopg2 >= 2.5.1
orpsycopg3 >= 3.1.8
is installed on the host before using this module.If the remote host is the PostgreSQL server (which is the default case), then PostgreSQL must also be installed on the remote host.
For Ubuntu-based systems, install the
postgresql
,libpq-dev
, andpython3-psycopg2
packages on the remote host before using this module.
See Also
See also
- community.postgresql.postgresql_table
Create, drop, or modify a PostgreSQL table.
- community.postgresql.postgresql_tablespace
Add or remove PostgreSQL tablespaces from remote hosts.
- PostgreSQL indexes reference
General information about PostgreSQL indexes.
- CREATE INDEX reference
Complete reference of the CREATE INDEX command documentation.
- ALTER INDEX reference
Complete reference of the ALTER INDEX command documentation.
- DROP INDEX reference
Complete reference of the DROP INDEX command documentation.
Examples
- 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
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Index name. Returned: success Sample: |
|
Query that was tried to be executed. Returned: success Sample: |
|
Schema where index exists. Returned: success Sample: |
|
Index state. Returned: success Sample: |
|
Index storage parameters. Returned: success Sample: |
|
Tablespace where index exists. Returned: success Sample: |
|
Index validity. Returned: success Sample: |