variable.c

来自「PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统」· C语言 代码 · 共 830 行 · 第 1/2 页

C
830
字号
/* * show_timezone: GUC show_hook for timezone */const char *show_timezone(void){	const char *tzn;	if (HasCTZSet)	{		Interval	interval;		interval.month = 0;		interval.day = 0;#ifdef HAVE_INT64_TIMESTAMP		interval.time = -(CTimeZone * USECS_PER_SEC);#else		interval.time = -CTimeZone;#endif		tzn = DatumGetCString(DirectFunctionCall1(interval_out,											  IntervalPGetDatum(&interval)));	}	else		tzn = pg_get_timezone_name(global_timezone);	if (tzn != NULL)		return tzn;	return "unknown";}/* * SET TRANSACTION ISOLATION LEVEL */const char *assign_XactIsoLevel(const char *value, bool doit, GucSource source){	if (SerializableSnapshot != NULL)	{		if (source >= PGC_S_INTERACTIVE)			ereport(ERROR,					(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),					 errmsg("SET TRANSACTION ISOLATION LEVEL must be called before any query")));		/* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */		else if (source != PGC_S_OVERRIDE)			return NULL;	}	if (IsSubTransaction())	{		if (source >= PGC_S_INTERACTIVE)			ereport(ERROR,					(errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),					 errmsg("SET TRANSACTION ISOLATION LEVEL must not be called in a subtransaction")));		/* source == PGC_S_OVERRIDE means do it anyway, eg at xact abort */		else if (source != PGC_S_OVERRIDE)			return NULL;	}	if (strcmp(value, "serializable") == 0)	{		if (doit)			XactIsoLevel = XACT_SERIALIZABLE;	}	else if (strcmp(value, "repeatable read") == 0)	{		if (doit)			XactIsoLevel = XACT_REPEATABLE_READ;	}	else if (strcmp(value, "read committed") == 0)	{		if (doit)			XactIsoLevel = XACT_READ_COMMITTED;	}	else if (strcmp(value, "read uncommitted") == 0)	{		if (doit)			XactIsoLevel = XACT_READ_UNCOMMITTED;	}	else if (strcmp(value, "default") == 0)	{		if (doit)			XactIsoLevel = DefaultXactIsoLevel;	}	else		return NULL;	return value;}const char *show_XactIsoLevel(void){	switch (XactIsoLevel)	{		case XACT_READ_UNCOMMITTED:			return "read uncommitted";		case XACT_READ_COMMITTED:			return "read committed";		case XACT_REPEATABLE_READ:			return "repeatable read";		case XACT_SERIALIZABLE:			return "serializable";		default:			return "bogus";	}}/* * Random number seed */boolassign_random_seed(double value, bool doit, GucSource source){	/* Can't really roll back on error, so ignore non-interactive setting */	if (doit && source >= PGC_S_INTERACTIVE)		DirectFunctionCall1(setseed, Float8GetDatum(value));	return true;}const char *show_random_seed(void){	return "unavailable";}/* * encoding handling functions */const char *assign_client_encoding(const char *value, bool doit, GucSource source){	int			encoding;	encoding = pg_valid_client_encoding(value);	if (encoding < 0)		return NULL;	/*	 * Note: if we are in startup phase then SetClientEncoding may not be able	 * to really set the encoding.	In this case we will assume that the	 * encoding is okay, and InitializeClientEncoding() will fix things once	 * initialization is complete.	 */	if (SetClientEncoding(encoding, doit) < 0)	{		if (source >= PGC_S_INTERACTIVE)			ereport(ERROR,					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),					 errmsg("conversion between %s and %s is not supported",							value, GetDatabaseEncodingName())));		return NULL;	}	return value;}/* * SET SESSION AUTHORIZATION * * When resetting session auth after an error, we can't expect to do catalog * lookups.  Hence, the stored form of the value must provide a numeric oid * that can be re-used directly.  We store the string in the form of * NAMEDATALEN 'x's, followed by T or F to indicate superuserness, followed * by the numeric oid, followed by a comma, followed by the role name. * This cannot be confused with a plain role name because of the NAMEDATALEN * limit on names, so we can tell whether we're being passed an initial * role name or a saved/restored value.  (NOTE: we rely on guc.c to have * properly truncated any incoming value, but not to truncate already-stored * values.  See GUC_IS_NAME processing.) */extern char *session_authorization_string;		/* in guc.c */const char *assign_session_authorization(const char *value, bool doit, GucSource source){	Oid			roleid = InvalidOid;	bool		is_superuser = false;	const char *actual_rolename = NULL;	char	   *result;	if (strspn(value, "x") == NAMEDATALEN &&		(value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'))	{		/* might be a saved userid string */		Oid			savedoid;		char	   *endptr;		savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);		if (endptr != value + NAMEDATALEN + 1 && *endptr == ',')		{			/* syntactically valid, so break out the data */			roleid = savedoid;			is_superuser = (value[NAMEDATALEN] == 'T');			actual_rolename = endptr + 1;		}	}	if (roleid == InvalidOid)	{		/* not a saved ID, so look it up */		HeapTuple	roleTup;		if (!IsTransactionState())		{			/*			 * Can't do catalog lookups, so fail.  The upshot of this is that			 * session_authorization cannot be set in postgresql.conf, which			 * seems like a good thing anyway.			 */			return NULL;		}		roleTup = SearchSysCache(AUTHNAME,								 PointerGetDatum(value),								 0, 0, 0);		if (!HeapTupleIsValid(roleTup))		{			if (source >= PGC_S_INTERACTIVE)				ereport(ERROR,						(errcode(ERRCODE_UNDEFINED_OBJECT),						 errmsg("role \"%s\" does not exist", value)));			return NULL;		}		roleid = HeapTupleGetOid(roleTup);		is_superuser = ((Form_pg_authid) GETSTRUCT(roleTup))->rolsuper;		actual_rolename = value;		ReleaseSysCache(roleTup);	}	if (doit)		SetSessionAuthorization(roleid, is_superuser);	result = (char *) malloc(NAMEDATALEN + 32 + strlen(actual_rolename));	if (!result)		return NULL;	memset(result, 'x', NAMEDATALEN);	sprintf(result + NAMEDATALEN, "%c%u,%s",			is_superuser ? 'T' : 'F',			roleid,			actual_rolename);	return result;}const char *show_session_authorization(void){	/*	 * Extract the user name from the stored string; see	 * assign_session_authorization	 */	const char *value = session_authorization_string;	Oid			savedoid;	char	   *endptr;	Assert(strspn(value, "x") == NAMEDATALEN &&		   (value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'));	savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);	Assert(endptr != value + NAMEDATALEN + 1 && *endptr == ',');	return endptr + 1;}/* * SET ROLE * * When resetting session auth after an error, we can't expect to do catalog * lookups.  Hence, the stored form of the value must provide a numeric oid * that can be re-used directly.  We implement this exactly like SET * SESSION AUTHORIZATION. * * The SQL spec requires "SET ROLE NONE" to unset the role, so we hardwire * a translation of "none" to InvalidOid. */extern char *role_string;		/* in guc.c */const char *assign_role(const char *value, bool doit, GucSource source){	Oid			roleid = InvalidOid;	bool		is_superuser = false;	const char *actual_rolename = value;	char	   *result;	if (strspn(value, "x") == NAMEDATALEN &&		(value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'))	{		/* might be a saved userid string */		Oid			savedoid;		char	   *endptr;		savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);		if (endptr != value + NAMEDATALEN + 1 && *endptr == ',')		{			/* syntactically valid, so break out the data */			roleid = savedoid;			is_superuser = (value[NAMEDATALEN] == 'T');			actual_rolename = endptr + 1;		}	}	if (roleid == InvalidOid &&		strcmp(actual_rolename, "none") != 0)	{		/* not a saved ID, so look it up */		HeapTuple	roleTup;		if (!IsTransactionState())		{			/*			 * Can't do catalog lookups, so fail.  The upshot of this is that			 * role cannot be set in postgresql.conf, which seems like a good			 * thing anyway.			 */			return NULL;		}		roleTup = SearchSysCache(AUTHNAME,								 PointerGetDatum(value),								 0, 0, 0);		if (!HeapTupleIsValid(roleTup))		{			if (source >= PGC_S_INTERACTIVE)				ereport(ERROR,						(errcode(ERRCODE_UNDEFINED_OBJECT),						 errmsg("role \"%s\" does not exist", value)));			return NULL;		}		roleid = HeapTupleGetOid(roleTup);		is_superuser = ((Form_pg_authid) GETSTRUCT(roleTup))->rolsuper;		ReleaseSysCache(roleTup);		/*		 * Verify that session user is allowed to become this role		 */		if (!is_member_of_role(GetSessionUserId(), roleid))		{			if (source >= PGC_S_INTERACTIVE)				ereport(ERROR,						(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),						 errmsg("permission denied to set role \"%s\"",								value)));			return NULL;		}	}	if (doit)		SetCurrentRoleId(roleid, is_superuser);	result = (char *) malloc(NAMEDATALEN + 32 + strlen(actual_rolename));	if (!result)		return NULL;	memset(result, 'x', NAMEDATALEN);	sprintf(result + NAMEDATALEN, "%c%u,%s",			is_superuser ? 'T' : 'F',			roleid,			actual_rolename);	return result;}const char *show_role(void){	/*	 * Extract the role name from the stored string; see assign_role	 */	const char *value = role_string;	Oid			savedoid;	char	   *endptr;	/* This special case only applies if no SET ROLE has been done */	if (value == NULL || strcmp(value, "none") == 0)		return "none";	Assert(strspn(value, "x") == NAMEDATALEN &&		   (value[NAMEDATALEN] == 'T' || value[NAMEDATALEN] == 'F'));	savedoid = (Oid) strtoul(value + NAMEDATALEN + 1, &endptr, 10);	Assert(endptr != value + NAMEDATALEN + 1 && *endptr == ',');	/*	 * Check that the stored string still matches the effective setting, else	 * return "none".  This is a kluge to deal with the fact that SET SESSION	 * AUTHORIZATION logically resets SET ROLE to NONE, but we cannot set the	 * GUC role variable from assign_session_authorization (because we haven't	 * got enough info to call set_config_option).	 */	if (savedoid != GetCurrentRoleId())		return "none";	return endptr + 1;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?