From b22b770683806db0a1c0a52a4601a3b6755891e0 Mon Sep 17 00:00:00 2001
From: Stephen Frost <sfrost@snowman.net>
Date: Thu, 14 May 2015 15:41:39 -0400
Subject: [PATCH] Make repeated 'make installcheck' runs work

In pg_audit, set client_min_messages up to warning, then reset the role
attributes, to completely reset the session while not making the
regression tests depend on being run by any particular user.
---
 contrib/pg_audit/expected/pg_audit.out | 9 ++++++---
 contrib/pg_audit/sql/pg_audit.sql      | 9 +++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/contrib/pg_audit/expected/pg_audit.out b/contrib/pg_audit/expected/pg_audit.out
index 886bd99d53..e870f060bd 100644
--- a/contrib/pg_audit/expected/pg_audit.out
+++ b/contrib/pg_audit/expected/pg_audit.out
@@ -1054,6 +1054,11 @@ GRANT user1 TO user2;
 NOTICE:  AUDIT: SESSION,59,1,ROLE,GRANT ROLE,,,GRANT user1 TO user2;,<none>
 REVOKE user1 FROM user2;
 NOTICE:  AUDIT: SESSION,60,1,ROLE,REVOKE ROLE,,,REVOKE user1 FROM user2;,<none>
+-- Cleanup
+-- Set client_min_messages up to warning to avoid noise
+SET client_min_messages = 'warning';
+ALTER ROLE :current_user RESET pg_audit.log;
+ALTER ROLE :current_user RESET pg_audit.log_level;
 DROP TABLE test.account_copy;
 DROP TABLE test.test_insert;
 DROP SCHEMA test;
@@ -1064,8 +1069,6 @@ DROP TABLE hoge;
 DROP TABLE account;
 DROP TABLE account_role_map;
 DROP USER user2;
-NOTICE:  AUDIT: SESSION,61,1,ROLE,DROP ROLE,,,DROP USER user2;,<none>
 DROP USER user1;
-NOTICE:  AUDIT: SESSION,62,1,ROLE,DROP ROLE,,,DROP USER user1;,<none>
 DROP ROLE auditor;
-NOTICE:  AUDIT: SESSION,63,1,ROLE,DROP ROLE,,,DROP ROLE auditor;,<none>
+RESET client_min_messages;
diff --git a/contrib/pg_audit/sql/pg_audit.sql b/contrib/pg_audit/sql/pg_audit.sql
index c5ddd5ff14..d4d6b6f581 100644
--- a/contrib/pg_audit/sql/pg_audit.sql
+++ b/contrib/pg_audit/sql/pg_audit.sql
@@ -646,6 +646,13 @@ SET pg_audit.log = 'role';
 GRANT user1 TO user2;
 REVOKE user1 FROM user2;
 
+-- Cleanup
+-- Set client_min_messages up to warning to avoid noise
+SET client_min_messages = 'warning';
+
+ALTER ROLE :current_user RESET pg_audit.log;
+ALTER ROLE :current_user RESET pg_audit.log_level;
+
 DROP TABLE test.account_copy;
 DROP TABLE test.test_insert;
 DROP SCHEMA test;
@@ -658,3 +665,5 @@ DROP TABLE account_role_map;
 DROP USER user2;
 DROP USER user1;
 DROP ROLE auditor;
+
+RESET client_min_messages;