src/backend/utils/init/miscinit.c
/*
 * GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
 * and the SecurityRestrictionContext flags.
 *
 * Currently there are two valid bits in SecurityRestrictionContext:
 *
 * SECURITY_LOCAL_USERID_CHANGE indicates that we are inside an operation
 * that is temporarily changing CurrentUserId via these functions.	This is
 * needed to indicate that the actual value of CurrentUserId is not in sync
 * with guc.c's internal state, so SET ROLE has to be disallowed.
 *
 * SECURITY_RESTRICTED_OPERATION indicates that we are inside an operation
 * that does not wish to trust called user-defined functions at all.  This
 * bit prevents not only SET ROLE, but various other changes of session state
 * that normally is unprotected but might possibly be used to subvert the
 * calling session later.  An example is replacing an existing prepared
 * statement with new code, which will then be executed with the outer
 * session's permissions when the prepared statement is next used.  Since
 * these restrictions are fairly draconian, we apply them only in contexts
 * where the called functions are really supposed to be side-effect-free
 * anyway, such as VACUUM/ANALYZE/REINDEX.
 *
 * Unlike GetUserId, GetUserIdAndSecContext does *not* Assert that the current
 * value of CurrentUserId is valid; nor does SetUserIdAndSecContext require
 * the new value to be valid.  In fact, these routines had better not
 * ever throw any kind of error.  This is because they are used by
 * StartTransaction and AbortTransaction to save/restore the settings,
 * and during the first transaction within a backend, the value to be saved
 * and perhaps restored is indeed invalid.	We have to be able to get
 * through AbortTransaction without asserting in case InitPostgres fails.
 */
GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
and the SecurityRestrictionContext flags.

Currently there are two valid bits in SecurityRestrictionContext.

SECURITY_LOCAL_USERID_CHANGE indicates that we are inside an operation
that is temporarily changing CurrentUserId via these functions.      This is
needed to indicate that the actual value of CurrentUserId is not in sync
with guc.c's internal state, so SET ROLE has to be disallowed.

SECURITY_RESTRICTED_OPERATION indicates that we are inside an operation
that does not wish to trust called user-defined functions at all.  This
bit prevents not only SET ROLE, but various other changes of session state
that normally is unprotected but might possibly be used to subvert the
calling session later.  An example is replacing an existing prepared
statement with new code, which will then be executed with the outer
session's permissions when the prepared statement is next used.  Since
these restrictions are fairly draconian, we apply them only in contexts
where the called functions are really supposed to be side-effect-free
anyway, such as VACUUM/ANALYZE/REINDEX.

Unlike GetUserId, GetUserIdAndSecContext does *not* Assert that the current
value of CurrentUserId is valid; nor does SetUserIdAndSecContext require
the new value to be valid.  In fact, these routines had better not
ever throw any kind of error.  This is because they are used by
StartTransaction and AbortTransaction to save/restore the settings,
and during the first transaction within a backend, the value to be saved
and perhaps restored is indeed invalid.      We have to be able to get
through AbortTransaction without asserting in case InitPostgres fails.
GetUserIdAndSecContext / SetUserIdAndSecContext - get / set the current user ID and the SecurityRestrictionContext flags . Currently there are two valid bits in SecurityRestrictionContext . SECURITY_LOCAL_USERID_CHANGE indicates that we are inside an operation that is temporarily changing CurrentUserId via these functions . This is needed to indicate that the actual value of CurrentUserId is not in sync with guc . c ' s internal state so SET ROLE has to be disallowed . SECURITY_RESTRICTED_OPERATION indicates that we are inside an operation that does not wish to trust called user - defined functions at all . This bit prevents not only SET ROLE but various other changes of session state that normally is unprotected but might possibly be used to subvert the calling session later . An example is replacing an existing prepared statement with new code which will then be executed with the outer session ' s permissions when the prepared statement is next used . Since these restrictions are fairly draconian we apply them only in contexts where the called functions are really supposed to be side - effect - free anyway such as VACUUM / ANALYZE / REINDEX . Unlike GetUserId GetUserIdAndSecContext does * not * Assert that the current value of CurrentUserId is valid ; nor does SetUserIdAndSecContext require the new value to be valid . In fact these routines had better not ever throw any kind of error . This is because they are used by StartTransaction and AbortTransaction to save / restore the settings and during the first transaction within a backend the value to be saved and perhaps restored is indeed invalid . We have to be able to get through AbortTransaction without asserting in case InitPostgres fails .
void
GetUserIdAndSecContext(Oid *userid, int *sec_context)
{
	*userid = CurrentUserId;
	*sec_context = SecurityRestrictionContext;
}
void GetUserIdAndSecContext ( Oid * userid , int * sec_context ) { * userid = CurrentUserId ; * sec_context = SecurityRestrictionContext ; }
GetUserIdAndSecContext/SetUserIdAndSecContext - get/set the current user ID
 and the SecurityRestrictionContext flags.
GetUserIdAndSecContext / SetUserIdAndSecContext - get / set the current user ID and the SecurityRestrictionContext flags .