src/backend/utils/init/miscinit.c
/*
 * Initialize user identity during normal backend startup
 */
Initialize user identity during normal backend startup
Initialize user identity during normal backend startup
void
InitializeSessionUserId(const char *rolename)
{
	HeapTuple	roleTup;
	Form_pg_authid rform;
	Oid			roleid;
	AssertState(!IsBootstrapProcessingMode());
	AssertState(!OidIsValid(AuthenticatedUserId));
	roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
	if (!HeapTupleIsValid(roleTup))
		ereport(FATAL,
				(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
				 errmsg("role \"%s\" does not exist", rolename), errSendAlert(false)));
	rform = (Form_pg_authid) GETSTRUCT(roleTup);
	roleid = HeapTupleGetOid(roleTup);
	AuthenticatedUserId = roleid;
	AuthenticatedUserIsSuperuser = rform->rolsuper;
	SetSessionUserId(roleid, AuthenticatedUserIsSuperuser);
	MyProc->roleId = roleid;
	if (IsUnderPostmaster)
	{
		if (!rform->rolcanlogin)
			ereport(FATAL,
					(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
					 errmsg("role \"%s\" is not permitted to log in",
							rolename)));
		if (Gp_role == GP_ROLE_DISPATCH && rform->rolconnlimit >= 0 &&
			!AuthenticatedUserIsSuperuser &&
			CountUserBackends(roleid) > rform->rolconnlimit)
			ereport(FATAL,
					(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
					 errmsg("too many connections for role \"%s\"",
							rolename)));
	}
	if ((Gp_role == GP_ROLE_DISPATCH || Gp_role == GP_ROLE_EXECUTE) && IsResQueueEnabled())
	{
		SetResQueueId();
	}
	SetConfigOption("session_authorization", rolename,
					PGC_BACKEND, PGC_S_OVERRIDE);
	SetConfigOption("is_superuser",
					AuthenticatedUserIsSuperuser ? "on" : "off",
					PGC_INTERNAL, PGC_S_OVERRIDE);
	ReleaseSysCache(roleTup);
}
void InitializeSessionUserId ( const char * rolename ) { HeapTuple roleTup ; Form_pg_authid rform ; Oid roleid ; AssertState ( ! IsBootstrapProcessingMode ( ) ) ; AssertState ( ! OidIsValid ( AuthenticatedUserId ) ) ; roleTup = SearchSysCache1 ( AUTHNAME , PointerGetDatum ( rolename ) ) ; if ( ! HeapTupleIsValid ( roleTup ) ) ereport ( FATAL , ( errcode ( ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION ) , errmsg ( " \" \" " , rolename ) , errSendAlert ( false ) ) ) ; rform = ( Form_pg_authid ) GETSTRUCT ( roleTup ) ; roleid = HeapTupleGetOid ( roleTup ) ; AuthenticatedUserId = roleid ; AuthenticatedUserIsSuperuser = rform -> rolsuper ; SetSessionUserId ( roleid , AuthenticatedUserIsSuperuser ) ; MyProc -> roleId = roleid ; if ( IsUnderPostmaster ) { if ( ! rform -> rolcanlogin ) ereport ( FATAL , ( errcode ( ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION ) , errmsg ( " \" \" " , rolename ) ) ) ; if ( Gp_role == GP_ROLE_DISPATCH && rform -> rolconnlimit >= 0 && ! AuthenticatedUserIsSuperuser && CountUserBackends ( roleid ) > rform -> rolconnlimit ) ereport ( FATAL , ( errcode ( ERRCODE_TOO_MANY_CONNECTIONS ) , errmsg ( " \" \" " , rolename ) ) ) ; } if ( ( Gp_role == GP_ROLE_DISPATCH || Gp_role == GP_ROLE_EXECUTE ) && IsResQueueEnabled ( ) ) { SetResQueueId ( ) ; } SetConfigOption ( " " , rolename , PGC_BACKEND , PGC_S_OVERRIDE ) ; SetConfigOption ( " " , AuthenticatedUserIsSuperuser ? " " : " " , PGC_INTERNAL , PGC_S_OVERRIDE ) ; ReleaseSysCache ( roleTup ) ; }
Initialize user identity during normal backend startup
Initialize user identity during normal backend startup
/*
	 * Don't do scans if we're bootstrapping, none of the system catalogs
	 * exist yet, and they should be owned by postgres anyway.
	 */ /* call only once */ /* This sets OuterUserId/CurrentUserId too */ /* Also mark our PGPROC entry with the authenticated user id */ /* (We assume this is an atomic store so no lock is needed) */ /*
	 * These next checks are not enforced when in standalone mode, so that
	 * there is a way to recover from sillinesses like "UPDATE pg_authid SET
	 * rolcanlogin = false;".
	 */ /*
		 * Is role allowed to login at all?
		 */ /*
		 * Check connection limit for this role.
		 *
		 * There is a race condition here --- we create our PGPROC before
		 * checking for other PGPROCs.	If two backends did this at about the
		 * same time, they might both think they were over the limit, while
		 * ideally one should succeed and one fail.  Getting that to work
		 * exactly seems more trouble than it is worth, however; instead we
		 * just document that the connection limit is approximate.
		 *
		 * We do not want to do this for QEs since a single QD might initialise
		 * many connections to each segment to execute a non-trivial plan and
		 * the user connection limit does not map, semantically, to that idea.
		 */ /*
	 * If resource scheduling is enabled, then set cached value for the
	 * queue. Do this even in standalone backend mode, just in case someone
	 * gives the superuser a resource queue.
	 */ /* Record username and superuser status as GUC settings too */