Skip to content

Commit

Permalink
Do not use cached catalog in is_user
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharu Goel committed Sep 25, 2024
1 parent 909acd4 commit 70db0de
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions contrib/babelfishpg_tsql/src/catalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,35 +851,59 @@ get_authid_login_ext_idx_oid(void)
bool
is_user(Oid role_oid)
{
Relation relation;
bool is_user = true;
ScanKeyData scanKey;
SysScanDesc scan;
HeapTuple tuple;
HeapTuple authtuple;
NameData rolname;
char *type_str = "";

authtuple = SearchSysCache1(AUTHOID, ObjectIdGetDatum(role_oid));
if (!HeapTupleIsValid(authtuple))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("role with OID %u does not exist", role_oid)));
rolname = ((Form_pg_authid) GETSTRUCT(authtuple))->rolname;
tuple = SearchSysCache1(AUTHIDUSEREXTROLENAME, NameGetDatum(&rolname));

relation = table_open(get_authid_user_ext_oid(), AccessShareLock);

ScanKeyInit(&scanKey,
Anum_bbf_authid_user_ext_rolname,
BTEqualStrategyNumber, F_NAMEEQ,
NameGetDatum(&rolname));

scan = systable_beginscan(relation,
get_authid_user_ext_idx_oid(),
true, NULL, 1, &scanKey);

tuple = systable_getnext(scan);

if (!HeapTupleIsValid(tuple))
is_user = false;
else
{
BpChar type = ((Form_authid_user_ext) GETSTRUCT(tuple))->type;
char *type_str = bpchar_to_cstring(&type);
Datum datum;
bool isnull;
TupleDesc dsc;

/*
* Only sysadmin can not be dropped. For the rest of the cases i.e., type
* is "S" or "U" etc, we should drop the user
*/
if (strcmp(type_str, "R") == 0)
is_user = false;
ReleaseSysCache(tuple);
dsc = RelationGetDescr(relation);
datum = heap_getattr(tuple, USER_EXT_TYPE + 1, dsc, &isnull);
if (!isnull)
type_str = pstrdup(TextDatumGetCString(datum));
}

/*
* Only sysadmin can not be dropped. For the rest of the cases i.e., type
* is "S" or "U" etc, we should drop the user
*/
if (strcmp(type_str, "R") == 0)
is_user = false;

systable_endscan(scan);
table_close(relation, AccessShareLock);

ReleaseSysCache(authtuple);

return is_user;
Expand Down

0 comments on commit 70db0de

Please sign in to comment.