community.postgresql.postgresql_idx module – Create or drop indexes from a PostgreSQL database
Note
This module is part of the community.postgresql collection (version 1.7.4).
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
.
To use it in a playbook, specify: community.postgresql.postgresql_idx
.
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=yes. 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=no, 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=yes, the index becomes invalid. In this case it should be dropped and created again. Mutually exclusive with cascade=yes. Choices:
|
|
Index conditions. Mutually exclusive with state=absent. |
|
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 |
|
The password this module should use to establish its PostgreSQL session. |
|
Path to a Unix domain socket for local connections. |
|
The username this module should use to establish its PostgreSQL session. Default: “postgres” |
|
Database port to connect to. Default: 5432 |
|
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. |
|
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:
|
Notes
Note
Supports
check_mode
.The index building process can affect database performance.
To avoid table locks on production databases, use concurrent=yes (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 psycopg2, a Python PostgreSQL database adapter. You must ensure that psycopg2 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, and python-psycopg2 packages on the remote host before using this module.
The ca_cert parameter requires at least Postgres version 8.4 and psycopg2 version 2.4.3.
See Also
See also
- community.postgresql.postgresql_table
The official documentation on the community.postgresql.postgresql_table module.
- community.postgresql.postgresql_tablespace
The official documentation on the community.postgresql.postgresql_tablespace module.
- 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: no
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: yes
concurrent: no
- 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: yes
concurrent: no
Return Values
Common return values are documented here, the following are the fields unique to this module:
Key |
Description |
---|---|
Index name. Returned: always Sample: “foo_idx” |
|
Query that was tried to be executed. Returned: always Sample: “CREATE INDEX CONCURRENTLY foo_idx ON test_table USING BTREE (id)” |
|
Schema where index exists. Returned: always Sample: “public” |
|
Index state. Returned: always Sample: “present” |
|
Index storage parameters. Returned: always Sample: [“fillfactor=90”] |
|
Tablespace where index exists. Returned: always Sample: “ssd” |
|
Index validity. Returned: always Sample: true |
Authors
Andrew Klychkov (@Andersson007)
Thomas O’Donnell (@andytom)
Collection links
Issue Tracker Repository (Sources) Report an issue Communication