diff --git a/contrib/passwordcheck/passwordcheck.c b/contrib/passwordcheck/passwordcheck.c
index 78c44b2..867a703 100644
*** a/contrib/passwordcheck/passwordcheck.c
--- b/contrib/passwordcheck/passwordcheck.c
***************
*** 21,28 ****
--- 21,31 ----
  #endif
  
  #include "commands/user.h"
+ #include "commands/variable.h"
  #include "fmgr.h"
+ #include "miscadmin.h"
  #include "libpq/md5.h"
+ #include "utils/memutils.h"
  
  PG_MODULE_MAGIC;
  
*************** PG_MODULE_MAGIC;
*** 31,36 ****
--- 34,41 ----
  
  extern void _PG_init(void);
  
+ char	   *save_log_statement = NULL;
+ 
  /*
   * check_password
   *
*************** check_password(const char *username,
*** 136,141 ****
--- 141,184 ----
  	/* all checks passed, password is ok */
  }
  
+ static void
+ log_role_change(Oid OldUserId, Oid NewUserId, bool NewUser_is_superuser)
+ {
+ 	char		   *olduser;
+ 	char		   *newuser;
+ 	char		   *su = "Superuser ";
+ 	char		   *nsu = "";
+ 	bool			OldUser_is_superuser = superuser_arg(OldUserId);
+ 	MemoryContext	oldcontext;
+ 
+ 	if (!OidIsValid(NewUserId))
+ 		NewUserId = GetSessionUserId();
+ 		NewUser_is_superuser = superuser_arg(NewUserId);
+ 
+ 	olduser = GetUserNameFromId(OldUserId, true);
+ 	newuser = GetUserNameFromId(NewUserId, true);
+ 
+ 	elog(LOG, "%sRole %s transitioning to %sRole %s",
+ 			  OldUser_is_superuser ? su : nsu,
+ 			  olduser,
+ 			  NewUser_is_superuser ? su : nsu,
+ 			  newuser);
+ 
+ 	oldcontext = MemoryContextSwitchTo(TopMemoryContext);
+ 	if (NewUser_is_superuser && !save_log_statement)
+ 	{
+ 		save_log_statement = GetConfigOptionByName("log_statement", NULL, false);
+ 		SetConfigOption("log_statement", "all", PGC_SUSET, PGC_S_SESSION);
+ 	}
+ 	else if (!NewUser_is_superuser && save_log_statement)
+ 	{
+ 		SetConfigOption("log_statement", save_log_statement, PGC_SUSET, PGC_S_SESSION);
+ 		pfree(save_log_statement);
+ 		save_log_statement = NULL;
+ 	}
+ 	MemoryContextSwitchTo(oldcontext);
+ }
+ 
  /*
   * Module initialization function
   */
*************** _PG_init(void)
*** 144,147 ****
--- 187,191 ----
  {
  	/* activate password checks when the module is loaded */
  	check_password_hook = check_password;
+     SetRole_hook = log_role_change;
  }
