From 2b3c8f4582bcc7f2426ddade21dde25b34f288ea Mon Sep 17 00:00:00 2001
From: Georg Sauthoff <mail@georg.so>
Date: Tue, 15 Sep 2020 15:21:34 +0200
Subject: [PATCH] Rollback if nothing changed (#887)

Since the module unconditionally issues ALTER statements in order to
observe their effect on the postgres catalog - to determine whether the
privileges have changes - a rollback is thus advisable when in fact
nothing has changed.

fix #885
---
 changelogs/fragments/887-rollback-if-nothing-changed.yml | 2 ++
 plugins/modules/database/postgresql/postgresql_privs.py  | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/fragments/887-rollback-if-nothing-changed.yml

diff --git a/changelogs/fragments/887-rollback-if-nothing-changed.yml b/changelogs/fragments/887-rollback-if-nothing-changed.yml
new file mode 100644
index 0000000000..7ae40ebb5a
--- /dev/null
+++ b/changelogs/fragments/887-rollback-if-nothing-changed.yml
@@ -0,0 +1,2 @@
+bugfixes:
+- postgresql_privs - rollback if nothing changed (https://github.com/ansible-collections/community.general/issues/885).
diff --git a/plugins/modules/database/postgresql/postgresql_privs.py b/plugins/modules/database/postgresql/postgresql_privs.py
index 30f2de7988..155bd87e94 100644
--- a/plugins/modules/database/postgresql/postgresql_privs.py
+++ b/plugins/modules/database/postgresql/postgresql_privs.py
@@ -1102,7 +1102,7 @@ def main():
         conn.rollback()
         module.fail_json(msg=to_native(e.message))
 
-    if module.check_mode:
+    if module.check_mode or not changed:
         conn.rollback()
     else:
         conn.commit()