aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt
diff options
context:
space:
mode:
authorAndrew Dunstan <andrew@dunslane.net>2015-05-09 13:06:49 -0400
committerAndrew Dunstan <andrew@dunslane.net>2015-05-09 13:06:49 -0400
commit0c90f6769de6a60f842c916d49b404d03bcc503a (patch)
tree519db4b7b0d468bd6df426169fcea5eb0688697b /src/backend/utils/adt
parent0cf56f14dd15532fec930b502cb6457023b01ef8 (diff)
downloadpostgresql-0c90f6769de6a60f842c916d49b404d03bcc503a.tar.gz
postgresql-0c90f6769de6a60f842c916d49b404d03bcc503a.zip
Add new OID alias type regrole
The new type has the scope of whole the database cluster so it doesn't behave the same as the existing OID alias types which have database scope, concerning object dependency. To avoid confusion constants of the new type are prohibited from appearing where dependencies are made involving it. Also, add a note to the docs about possible MVCC violation and optimization issues, which are general over the all reg* types. Kyotaro Horiguchi
Diffstat (limited to 'src/backend/utils/adt')
-rw-r--r--src/backend/utils/adt/acl.c2
-rw-r--r--src/backend/utils/adt/name.c4
-rw-r--r--src/backend/utils/adt/regproc.c104
-rw-r--r--src/backend/utils/adt/selfuncs.c2
4 files changed, 109 insertions, 3 deletions
diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c
index 7701fc5ac07..e7aecc95c97 100644
--- a/src/backend/utils/adt/acl.c
+++ b/src/backend/utils/adt/acl.c
@@ -4878,7 +4878,7 @@ check_is_member_of_role(Oid member, Oid role)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be member of role \"%s\"",
- GetUserNameFromId(role))));
+ GetUserNameFromId(role, false))));
}
/*
diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c
index b6c6e393358..58261275614 100644
--- a/src/backend/utils/adt/name.c
+++ b/src/backend/utils/adt/name.c
@@ -263,13 +263,13 @@ namestrcmp(Name name, const char *str)
Datum
current_user(PG_FUNCTION_ARGS)
{
- PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(GetUserId()))));
+ PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(GetUserId(), false))));
}
Datum
session_user(PG_FUNCTION_ARGS)
{
- PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(GetSessionUserId()))));
+ PG_RETURN_DATUM(DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(GetSessionUserId(), false))));
}
diff --git a/src/backend/utils/adt/regproc.c b/src/backend/utils/adt/regproc.c
index 11d663b295d..8b5672875c2 100644
--- a/src/backend/utils/adt/regproc.c
+++ b/src/backend/utils/adt/regproc.c
@@ -40,6 +40,7 @@
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
+#include "utils/acl.h"
static char *format_operator_internal(Oid operator_oid, bool force_qualify);
static char *format_procedure_internal(Oid procedure_oid, bool force_qualify);
@@ -1553,6 +1554,109 @@ regdictionarysend(PG_FUNCTION_ARGS)
return oidsend(fcinfo);
}
+/*
+ * regrolein - converts "rolename" to role OID
+ *
+ * We also accept a numeric OID, for symmetry with the output routine.
+ *
+ * '-' signifies unknown (OID 0). In all other cases, the input must
+ * match an existing pg_authid entry.
+ *
+ * This function is not needed in bootstrap mode, so we don't worry about
+ * making it work then.
+ */
+Datum
+regrolein(PG_FUNCTION_ARGS)
+{
+ char *role_name_or_oid = PG_GETARG_CSTRING(0);
+ Oid result;
+
+ /* '-' ? */
+ if (strcmp(role_name_or_oid, "-") == 0)
+ PG_RETURN_OID(InvalidOid);
+
+ /* Numeric OID? */
+ if (role_name_or_oid[0] >= '0' &&
+ role_name_or_oid[0] <= '9' &&
+ strspn(role_name_or_oid, "0123456789") == strlen(role_name_or_oid))
+ {
+ result = DatumGetObjectId(DirectFunctionCall1(oidin,
+ CStringGetDatum(role_name_or_oid)));
+ PG_RETURN_OID(result);
+ }
+
+ /* Normal case: see if the name matches any pg_authid entry. */
+ result = get_role_oid(role_name_or_oid, false);
+
+ PG_RETURN_OID(result);
+}
+
+/*
+ * to_regrole - converts "rolename" to role OID
+ *
+ * If the name is not found, we return NULL.
+ */
+Datum
+to_regrole(PG_FUNCTION_ARGS)
+{
+ char *role_name = PG_GETARG_CSTRING(0);
+ Oid result;
+
+ result = get_role_oid(role_name, true);
+
+ if (OidIsValid(result))
+ PG_RETURN_OID(result);
+ else
+ PG_RETURN_NULL();
+}
+
+/*
+ * regroleout - converts role OID to "role_name"
+ */
+Datum
+regroleout(PG_FUNCTION_ARGS)
+{
+ Oid roleoid = PG_GETARG_OID(0);
+ char *result;
+
+
+ if (roleoid == InvalidOid)
+ {
+ result = pstrdup("-");
+ PG_RETURN_CSTRING(result);
+ }
+
+ result = GetUserNameFromId(roleoid, true);
+ if (!result)
+ {
+ /* If OID doesn't match any role, return it numerically */
+ result = (char *) palloc(NAMEDATALEN);
+ snprintf(result, NAMEDATALEN, "%u", roleoid);
+ }
+ PG_RETURN_CSTRING(result);
+}
+
+/*
+ * regrolerecv - converts external binary format to regrole
+ */
+Datum
+regrolerecv(PG_FUNCTION_ARGS)
+{
+ /* Exactly the same as oidrecv, so share code */
+ return oidrecv(fcinfo);
+}
+
+/*
+ * regrolesend - converts regrole to binary format
+ */
+Datum
+regrolesend(PG_FUNCTION_ARGS)
+{
+ /* Exactly the same as oidsend, so share code */
+ return oidsend(fcinfo);
+}
+
+
/*
* text_regclass: convert text to regclass
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 4dd3f9fbce1..a28868c3130 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -3619,6 +3619,7 @@ convert_to_scalar(Datum value, Oid valuetypid, double *scaledvalue,
case REGTYPEOID:
case REGCONFIGOID:
case REGDICTIONARYOID:
+ case REGROLEOID:
*scaledvalue = convert_numeric_to_scalar(value, valuetypid);
*scaledlobound = convert_numeric_to_scalar(lobound, boundstypid);
*scaledhibound = convert_numeric_to_scalar(hibound, boundstypid);
@@ -3724,6 +3725,7 @@ convert_numeric_to_scalar(Datum value, Oid typid)
case REGTYPEOID:
case REGCONFIGOID:
case REGDICTIONARYOID:
+ case REGROLEOID:
/* we can treat OIDs as integers... */
return (double) DatumGetObjectId(value);
}