提交 c59e3c8f 编写于 作者: P Pengzhou Tang 提交者: Tang Pengzhou

Add a syscache for pg_resgroup

This commit makes access to pg_resgroup a little bit faster
上级 a399d5b6
...@@ -380,7 +380,7 @@ AlterResourceGroup(AlterResourceGroupStmt *stmt) ...@@ -380,7 +380,7 @@ AlterResourceGroup(AlterResourceGroupStmt *stmt)
* Check the pg_resgroup relation to be certain the resource group already * Check the pg_resgroup relation to be certain the resource group already
* exists. * exists.
*/ */
groupid = GetResGroupIdForName(stmt->name, RowExclusiveLock); groupid = GetResGroupIdForName(stmt->name);
if (groupid == InvalidOid) if (groupid == InvalidOid)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT), (errcode(ERRCODE_UNDEFINED_OBJECT),
...@@ -612,32 +612,27 @@ GetResGroupIdForRole(Oid roleid) ...@@ -612,32 +612,27 @@ GetResGroupIdForRole(Oid roleid)
* to Oid. * to Oid.
*/ */
Oid Oid
GetResGroupIdForName(const char *name, LOCKMODE lockmode) GetResGroupIdForName(const char *name)
{ {
Relation rel;
ScanKeyData scankey;
SysScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
Oid rsgid; Oid rsgid;
rel = heap_open(ResGroupRelationId, lockmode); tuple = SearchSysCache1(RESGROUPNAME,
CStringGetDatum(name));
/* SELECT oid FROM pg_resgroup WHERE rsgname = :1 */
ScanKeyInit(&scankey,
Anum_pg_resgroup_rsgname,
BTEqualStrategyNumber, F_NAMEEQ,
CStringGetDatum(name));
scan = systable_beginscan(rel, ResGroupRsgnameIndexId, true,
SnapshotNow, 1, &scankey);
tuple = systable_getnext(scan);
if (HeapTupleIsValid(tuple)) if (HeapTupleIsValid(tuple))
rsgid = HeapTupleGetOid(tuple); {
bool isnull;
Datum oidDatum = SysCacheGetAttr(RESGROUPNAME, tuple,
ObjectIdAttributeNumber,
&isnull);
Assert (!isnull);
rsgid = DatumGetObjectId(oidDatum);
}
else else
rsgid = InvalidOid; return InvalidOid;
systable_endscan(scan); ReleaseSysCache(tuple);
heap_close(rel, lockmode);
return rsgid; return rsgid;
} }
...@@ -646,37 +641,28 @@ GetResGroupIdForName(const char *name, LOCKMODE lockmode) ...@@ -646,37 +641,28 @@ GetResGroupIdForName(const char *name, LOCKMODE lockmode)
* GetResGroupNameForId -- Return the resource group name for an Oid * GetResGroupNameForId -- Return the resource group name for an Oid
*/ */
char * char *
GetResGroupNameForId(Oid oid, LOCKMODE lockmode) GetResGroupNameForId(Oid oid)
{ {
Relation rel;
ScanKeyData scankey;
SysScanDesc scan;
HeapTuple tuple; HeapTuple tuple;
char *name = NULL; char *name = NULL;
rel = heap_open(ResGroupRelationId, lockmode); tuple = SearchSysCache1(RESGROUPOID,
ObjectIdGetDatum(oid));
/* SELECT rsgname FROM pg_resgroup WHERE oid = :1 */
ScanKeyInit(&scankey,
ObjectIdAttributeNumber,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(oid));
scan = systable_beginscan(rel, ResGroupOidIndexId, true,
SnapshotNow, 1, &scankey);
tuple = systable_getnext(scan);
if (HeapTupleIsValid(tuple)) if (HeapTupleIsValid(tuple))
{ {
bool isnull; bool isnull;
Datum nameDatum = heap_getattr(tuple, Anum_pg_resgroup_rsgname, Datum nameDatum = SysCacheGetAttr(RESGROUPOID, tuple,
rel->rd_att, &isnull); Anum_pg_resgroup_rsgname,
&isnull);
Assert (!isnull); Assert (!isnull);
Name resGroupName = DatumGetName(nameDatum); Name resGroupName = DatumGetName(nameDatum);
name = pstrdup(NameStr(*resGroupName)); name = pstrdup(NameStr(*resGroupName));
} }
else
return "unknow";
systable_endscan(scan); ReleaseSysCache(tuple);
heap_close(rel, lockmode);
return name; return name;
} }
......
...@@ -533,7 +533,7 @@ CreateRole(CreateRoleStmt *stmt) ...@@ -533,7 +533,7 @@ CreateRole(CreateRoleStmt *stmt)
{ {
Oid rsgid; Oid rsgid;
rsgid = GetResGroupIdForName(resgroup, ShareLock); rsgid = GetResGroupIdForName(resgroup);
if (rsgid == InvalidOid) if (rsgid == InvalidOid)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT), (errcode(ERRCODE_UNDEFINED_OBJECT),
...@@ -1195,7 +1195,7 @@ AlterRole(AlterRoleStmt *stmt) ...@@ -1195,7 +1195,7 @@ AlterRole(AlterRoleStmt *stmt)
resgroup))); resgroup)));
} }
rsgid = GetResGroupIdForName(resgroup, ShareLock); rsgid = GetResGroupIdForName(resgroup);
if (rsgid == InvalidOid) if (rsgid == InvalidOid)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT), (errcode(ERRCODE_UNDEFINED_OBJECT),
......
...@@ -633,7 +633,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS) ...@@ -633,7 +633,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
if (funcctx->tuple_desc->natts > 13) if (funcctx->tuple_desc->natts > 13)
{ {
Datum now = TimestampTzGetDatum(GetCurrentTimestamp()); Datum now = TimestampTzGetDatum(GetCurrentTimestamp());
char *groupName = GetResGroupNameForId(beentry->st_rsgid, AccessShareLock); char *groupName = GetResGroupNameForId(beentry->st_rsgid);
values[13] = ObjectIdGetDatum(beentry->st_rsgid); values[13] = ObjectIdGetDatum(beentry->st_rsgid);
......
...@@ -54,6 +54,7 @@ ...@@ -54,6 +54,7 @@
#include "catalog/pg_ts_template.h" #include "catalog/pg_ts_template.h"
#include "catalog/pg_type.h" #include "catalog/pg_type.h"
#include "catalog/pg_window.h" #include "catalog/pg_window.h"
#include "catalog/pg_resgroup.h"
#include "utils/syscache.h" #include "utils/syscache.h"
...@@ -509,6 +510,28 @@ static const struct cachedesc cacheinfo[] = { ...@@ -509,6 +510,28 @@ static const struct cachedesc cacheinfo[] = {
}, },
1024 1024
}, },
{ResGroupRelationId, /* RESGROUPOID */
ResGroupOidIndexId,
1,
{
ObjectIdAttributeNumber,
0,
0,
0
},
128
},
{ResGroupRelationId, /* RESGROUPNAME */
ResGroupRsgnameIndexId,
1,
{
Anum_pg_resgroup_rsgname,
0,
0,
0
},
128
},
{RewriteRelationId, /* RULERELNAME */ {RewriteRelationId, /* RULERELNAME */
RewriteRelRulenameIndexId, RewriteRelRulenameIndexId,
2, 2,
......
...@@ -32,8 +32,8 @@ extern void DropResourceGroup(DropResourceGroupStmt *stmt); ...@@ -32,8 +32,8 @@ extern void DropResourceGroup(DropResourceGroupStmt *stmt);
extern void AlterResourceGroup(AlterResourceGroupStmt *stmt); extern void AlterResourceGroup(AlterResourceGroupStmt *stmt);
/* catalog access function */ /* catalog access function */
extern Oid GetResGroupIdForName(const char *name, LOCKMODE lockmode); extern Oid GetResGroupIdForName(const char *name);
extern char *GetResGroupNameForId(Oid oid, LOCKMODE lockmode); extern char *GetResGroupNameForId(Oid oid);
extern Oid GetResGroupIdForRole(Oid roleid); extern Oid GetResGroupIdForRole(Oid roleid);
extern void GetResGroupCapabilities(Relation rel, extern void GetResGroupCapabilities(Relation rel,
Oid groupId, Oid groupId,
......
...@@ -66,6 +66,8 @@ enum SysCacheIdentifier ...@@ -66,6 +66,8 @@ enum SysCacheIdentifier
PROCOID, PROCOID,
RELNAMENSP, RELNAMENSP,
RELOID, RELOID,
RESGROUPOID,
RESGROUPNAME,
RULERELNAME, RULERELNAME,
STATRELATT, STATRELATT,
TSCONFIGMAP, TSCONFIGMAP,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册