Skip to content

Commit

Permalink
Add new OID alias type regrole (#13)
Browse files Browse the repository at this point in the history
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

-- OPENGPDB additions.

I have omitted catalog changes provides within this commit for ABI compat sake.
This is not disallows use of them. One, if need, could use them with help of
extension.

Kirill Reshke

Co-authored-by: Andrew Dunstan <[email protected]>
  • Loading branch information
reshke and adunstan committed Nov 14, 2024
1 parent ec3ecff commit 5cbe6b5
Show file tree
Hide file tree
Showing 27 changed files with 233 additions and 42 deletions.
2 changes: 1 addition & 1 deletion contrib/dblink/dblink.c
Original file line number Diff line number Diff line change
Expand Up @@ -2697,7 +2697,7 @@ dblink_connstr_check(const char *connstr)
{
if (option->val == NULL || option->val[0] == '\0')
{
char *username = GetUserNameFromId(GetUserId());
char *username = GetUserNameFromId(GetUserId(), false);

/* 7 is strlen("user= ") + length of '\0' */
connstr_modified = palloc0(7 + strlen(username) + strlen(connstr));
Expand Down
2 changes: 1 addition & 1 deletion contrib/spi/insert_username.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ insert_username(PG_FUNCTION_ARGS)
args[0], relname)));

/* create fields containing name */
newval = CStringGetTextDatum(GetUserNameFromId(GetUserId()));
newval = CStringGetTextDatum(GetUserNameFromId(GetUserId(), false));

/* construct new tuple */
rettuple = SPI_modifytuple(rel, rettuple, 1, &attnum, &newval, NULL);
Expand Down
2 changes: 1 addition & 1 deletion contrib/spi/timetravel.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ timetravel(PG_FUNCTION_ARGS)
}

/* create fields containing name */
newuser = CStringGetTextDatum(GetUserNameFromId(GetUserId()));
newuser = CStringGetTextDatum(GetUserNameFromId(GetUserId(), false));

nulltext = (Datum) NULL;

Expand Down
27 changes: 23 additions & 4 deletions doc/src/sgml/datatype.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -4364,8 +4364,9 @@ SET xmloption TO { DOCUMENT | CONTENT };
an object identifier. There are also several alias types for
<type>oid</>: <type>regproc</>, <type>regprocedure</>,
<type>regoper</>, <type>regoperator</>, <type>regclass</>,
<type>regtype</>, <type>regconfig</>, and <type>regdictionary</>.
<xref linkend="datatype-oid-table"> shows an overview.
<type>regtype</>, <type>regrole</>, <type>regconfig</>, and
<type>regdictionary</>. <xref linkend="datatype-oid-table"> shows
an overview.
</para>

<para>
Expand Down Expand Up @@ -4473,6 +4474,13 @@ SELECT * FROM pg_attribute
<entry><literal>integer</></entry>
</row>

<row>
<entry><type>regrole</></entry>
<entry><structname>pg_authid</></entry>
<entry>role name</entry>
<entry><literal>smithee</></entry>
</row>

<row>
<entry><type>regconfig</></entry>
<entry><structname>pg_ts_config</></entry>
Expand All @@ -4491,7 +4499,8 @@ SELECT * FROM pg_attribute
</table>

<para>
All of the OID alias types accept schema-qualified names, and will
All of the OID alias types for objects grouped by namespace accept
schema-qualified names, and will
display schema-qualified names on output if the object would not
be found in the current search path without being qualified.
The <type>regproc</> and <type>regoper</> alias types will only
Expand All @@ -4503,7 +4512,7 @@ SELECT * FROM pg_attribute
</para>

<para>
An additional property of the OID alias types is the creation of
An additional property of most of the OID alias types is the creation of
dependencies. If a
constant of one of these types appears in a stored expression
(such as a column default expression or view), it creates a dependency
Expand All @@ -4513,7 +4522,17 @@ SELECT * FROM pg_attribute
understands that the default expression depends on the sequence
<literal>my_seq</>; the system will not let the sequence be dropped
without first removing the default expression.
<type>regrole</> is the only exception for the property. Constants of this
type are not allowed in such expressions.
</para>

<note>
<para>
The OID alias types do not completely follow transaction isolation
rules. The planner also treats them as simple constants, which may
result in sub-optimal planning.
</para>
</note>

<para>
Another identifier type used by the system is <type>xid</>, or transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ gp_get_endpoints(PG_FUNCTION_ARGS)
StrNCpy(info->cursorName, entry->cursorName, NAMEDATALEN);
info->state = entry->state;
info->sessionId = entry->sessionID;
StrNCpy(info->userName, GetUserNameFromId(entry->userID), NAMEDATALEN);
StrNCpy(info->userName, GetUserNameFromId(entry->userID, false), NAMEDATALEN);
idx++;
}
}
Expand Down Expand Up @@ -349,7 +349,7 @@ gp_get_segment_endpoints(PG_FUNCTION_ARGS)
values[4] = CStringGetTextDatum(state);
values[5] = Int32GetDatum(GpIdentity.segindex);
values[6] = Int32GetDatum(entry->sessionID);
values[7] = CStringGetTextDatum(GetUserNameFromId(entry->userID));
values[7] = CStringGetTextDatum(GetUserNameFromId(entry->userID, false));
values[8] = CStringGetTextDatum(entry->name);
values[9] = CStringGetTextDatum(entry->cursorName);

Expand Down
4 changes: 2 additions & 2 deletions gpcontrib/orafce/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,14 +1151,14 @@ dbms_pipe_create_pipe (PG_FUNCTION_ARGS)

p->uid = GetUserId();
#ifdef GP_VERSION_NUM
user = (GetUserNameFromId(p->uid));
user = (GetUserNameFromId(p->uid, false));
#elif PG_VERSION_NUM >= 90500

user = (char*)DirectFunctionCall1(namein,
CStringGetDatum(GetUserNameFromId(p->uid, false)));

#else
user = (char*)DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(p->uid)));
user = (char*)DirectFunctionCall1(namein, CStringGetDatum(GetUserNameFromId(p->uid, false)));
#endif
p->creator = ora_sstrcpy(user);
pfree(user);
Expand Down
10 changes: 10 additions & 0 deletions src/backend/catalog/dependency.c
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,16 @@ find_expr_references_walker(Node *node,
add_object_address(OCLASS_TSDICT, objoid, 0,
context->addrs);
break;

/*
* Dependencies for regrole should be shared among all
* databases, so explicitly inhibit to have dependencies.
*/
case REGROLEOID:
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("constant of the type \'regrole\' cannot be used here")));
break;
}
}
return false;
Expand Down
22 changes: 12 additions & 10 deletions src/backend/catalog/objectaddress.c
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@ getObjectDescription(const ObjectAddress *object)
case OCLASS_ROLE:
{
appendStringInfo(&buffer, _("role %s"),
GetUserNameFromId(object->objectId));
GetUserNameFromId(object->objectId, false));
break;
}

Expand Down Expand Up @@ -2196,7 +2196,7 @@ getObjectDescription(const ObjectAddress *object)
ReleaseSysCache(tup);

if (OidIsValid(useid))
usename = GetUserNameFromId(useid);
usename = GetUserNameFromId(useid, false);
else
usename = "public";

Expand Down Expand Up @@ -2236,28 +2236,28 @@ getObjectDescription(const ObjectAddress *object)
case DEFACLOBJ_RELATION:
appendStringInfo(&buffer,
_("default privileges on new relations belonging to role %s"),
GetUserNameFromId(defacl->defaclrole));
GetUserNameFromId(defacl->defaclrole, false));
break;
case DEFACLOBJ_SEQUENCE:
appendStringInfo(&buffer,
_("default privileges on new sequences belonging to role %s"),
GetUserNameFromId(defacl->defaclrole));
GetUserNameFromId(defacl->defaclrole, false));
break;
case DEFACLOBJ_FUNCTION:
appendStringInfo(&buffer,
_("default privileges on new functions belonging to role %s"),
GetUserNameFromId(defacl->defaclrole));
GetUserNameFromId(defacl->defaclrole, false));
break;
case DEFACLOBJ_TYPE:
appendStringInfo(&buffer,
_("default privileges on new types belonging to role %s"),
GetUserNameFromId(defacl->defaclrole));
GetUserNameFromId(defacl->defaclrole, false));
break;
default:
/* shouldn't get here */
appendStringInfo(&buffer,
_("default privileges belonging to role %s"),
GetUserNameFromId(defacl->defaclrole));
GetUserNameFromId(defacl->defaclrole, false));
break;
}

Expand Down Expand Up @@ -3331,7 +3331,7 @@ getObjectIdentity(const ObjectAddress *object)
{
char *username;

username = GetUserNameFromId(object->objectId);
username = GetUserNameFromId(object->objectId, false);
appendStringInfoString(&buffer,
quote_identifier(username));
break;
Expand Down Expand Up @@ -3402,7 +3402,7 @@ getObjectIdentity(const ObjectAddress *object)
ReleaseSysCache(tup);

if (OidIsValid(useid))
usename = quote_identifier(GetUserNameFromId(useid));
usename = quote_identifier(GetUserNameFromId(useid, false));
else
usename = "public";

Expand All @@ -3419,6 +3419,7 @@ getObjectIdentity(const ObjectAddress *object)

HeapTuple tup;
Form_pg_default_acl defacl;
char* username;

defaclrel = heap_open(DefaultAclRelationId, AccessShareLock);

Expand All @@ -3438,9 +3439,10 @@ getObjectIdentity(const ObjectAddress *object)

defacl = (Form_pg_default_acl) GETSTRUCT(tup);

username = GetUserNameFromId(defacl->defaclrole, false);
appendStringInfo(&buffer,
"for role %s",
quote_identifier(GetUserNameFromId(defacl->defaclrole)));
quote_identifier(username));

if (OidIsValid(defacl->defaclnamespace))
{
Expand Down
2 changes: 1 addition & 1 deletion src/backend/cdb/dispatcher/cdbconn.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ cdbconn_doConnectStart(SegmentDatabaseDescriptor *segdbDesc,
* get user name from AuthenticatedUserId which is initialized
* in InitPostgres()
*/
values[nkeywords] = GetUserNameFromId(GetAuthenticatedUserId());
values[nkeywords] = GetUserNameFromId(GetAuthenticatedUserId(), false);
}
nkeywords++;

Expand Down
2 changes: 1 addition & 1 deletion src/backend/commands/alter.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ AlterObjectOwner_internal(Relation rel, Oid objectId, Oid new_ownerId)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("cannot transfer ownership to superuser \"%s\"",
GetUserNameFromId(new_ownerId))));
GetUserNameFromId(new_ownerId, false))));

}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/commands/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ execute_extension_script(Node *stmt,
if (strstr(c_sql, "@extowner@"))
{
Oid uid = switch_to_superuser ? save_userid : GetUserId();
const char *userName = GetUserNameFromId(uid);
const char *userName = GetUserNameFromId(uid, false);
const char *qUserName = quote_identifier(userName);

t_sql = DirectFunctionCall3Coll(replace_text,
Expand Down
2 changes: 1 addition & 1 deletion src/backend/commands/exttablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ DefineExternalRelation(CreateExternalStmt *createExtStmt)
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("role \"%s\" does not exist (in DefineExternalRelation)",
GetUserNameFromId(userid))));
GetUserNameFromId(userid, false))));

if ((uri->protocol == URI_GPFDIST || uri->protocol == URI_GPFDISTS) && iswritable)
{
Expand Down
2 changes: 1 addition & 1 deletion src/backend/commands/schemacmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ AlterSchemaOwner_internal(HeapTuple tup, Relation rel, Oid newOwnerId)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("cannot transfer ownership to superuser \"%s\"",
GetUserNameFromId(newOwnerId))));
GetUserNameFromId(newOwnerId, false))));

}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/commands/tablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -12607,7 +12607,7 @@ ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, LOCKMODE lock
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("cannot transfer ownership to superuser \"%s\"",
GetUserNameFromId(newOwnerId))));
GetUserNameFromId(newOwnerId, false))));

}
}
Expand Down
2 changes: 1 addition & 1 deletion src/backend/utils/adt/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5123,7 +5123,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))));
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/backend/utils/adt/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}


Expand Down
Loading

0 comments on commit 5cbe6b5

Please sign in to comment.