refactor RelationIsAKnownShard function

improve-drop-trigger2
Onur Tirtir 2020-03-03 15:54:00 +03:00
parent 5baea1dfcb
commit de8e0a409a
2 changed files with 42 additions and 31 deletions

View File

@ -98,23 +98,32 @@ citus_table_is_visible(PG_FUNCTION_ARGS)
/* /*
* RelationIsAKnownShard gets a relationId, check whether it's a shard of * RelationIsAKnownShard gets a shardRelationOid, checks whether it's a shard
* any distributed table. If onlySearchPath is true, then it searches * of any distributed table. If onlySearchPath is true, then it only searches
* the current search path. * the current search path.
*
* We can only do that in MX since both the metadata and tables are only
* present there.
*/ */
bool bool
RelationIsAKnownShard(Oid shardRelationId, bool onlySearchPath) RelationIsAKnownShard(Oid shardRelationOid, bool onlySearchPath)
{ {
bool missingOk = true; Oid ownerRelationOid = GetOwnerRelationOid(shardRelationOid, onlySearchPath);
char relKind = '\0';
if (!OidIsValid(shardRelationId)) return OidIsValid(ownerRelationOid);
}
/*
* GetOwnerRelationOid gets a shardRelationOid, returns OID of the relation
* that owns the shard relation with shardRelationOid if it's a valid shard
* of it. If onlySearchPath is true, then it only searches the current search
* path.
*/
Oid
GetOwnerRelationOid(Oid shardRelationOid, bool onlySearchPath)
{
if (!OidIsValid(shardRelationOid))
{ {
/* we cannot continue without a valid Oid */ /* we cannot continue without a valid Oid */
return false; return InvalidOid;
} }
if (IsCoordinator()) if (IsCoordinator())
@ -129,36 +138,37 @@ RelationIsAKnownShard(Oid shardRelationId, bool onlySearchPath)
* or non-mx worker nodes, unless the coordinator is * or non-mx worker nodes, unless the coordinator is
* in pg_dist_node. * in pg_dist_node.
*/ */
return false; return InvalidOid;
} }
} }
Relation relation = try_relation_open(shardRelationId, AccessShareLock); Relation relation = try_relation_open(shardRelationOid, AccessShareLock);
if (relation == NULL) if (relation == NULL)
{ {
return false; return InvalidOid;
} }
relation_close(relation, NoLock); relation_close(relation, NoLock);
/* we're not interested in the relations that are not in the search path */ /* we're not interested in the relations that are not in the search path */
if (!RelationIsVisible(shardRelationId) && onlySearchPath) if (!RelationIsVisible(shardRelationOid) && onlySearchPath)
{ {
return false; return InvalidOid;
} }
/* /*
* If the input relation is an index we simply replace the * If the input relation is an index we simply replace the relationOid
* relationId with the corresponding relation to hide indexes * with the corresponding relation to hide indexes as well.
* as well.
*/ */
relKind = get_rel_relkind(shardRelationId); char relKind = get_rel_relkind(shardRelationOid);
if (relKind == RELKIND_INDEX) if (relKind == RELKIND_INDEX)
{ {
shardRelationId = IndexGetRelation(shardRelationId, false); shardRelationOid = IndexGetRelation(shardRelationOid, false);
} }
/* get the shard's relation name */ /* get the shard's relation name */
char *shardRelationName = get_rel_name(shardRelationId); char *shardRelationName = get_rel_name(shardRelationOid);
bool missingOk = true;
uint64 shardId = ExtractShardIdFromTableName(shardRelationName, missingOk); uint64 shardId = ExtractShardIdFromTableName(shardRelationName, missingOk);
if (shardId == INVALID_SHARD_ID) if (shardId == INVALID_SHARD_ID)
@ -167,21 +177,21 @@ RelationIsAKnownShard(Oid shardRelationId, bool onlySearchPath)
* The format of the table name does not align with * The format of the table name does not align with
* our shard name definition. * our shard name definition.
*/ */
return false; return InvalidOid;
} }
/* try to get the relation id */ /* try to get the relation id */
Oid relationId = LookupShardRelation(shardId, true); Oid relationOid = LookupShardRelation(shardId, missingOk);
if (!OidIsValid(relationId)) if (!OidIsValid(relationOid))
{ {
/* there is no such relation */ /* there is no such relation */
return false; return InvalidOid;
} }
/* verify that their namespaces are the same */ /* verify that their namespaces are the same */
if (get_rel_namespace(shardRelationId) != get_rel_namespace(relationId)) if (get_rel_namespace(shardRelationOid) != get_rel_namespace(relationOid))
{ {
return false; return InvalidOid;
} }
/* /*
@ -189,15 +199,15 @@ RelationIsAKnownShard(Oid shardRelationId, bool onlySearchPath)
* to do that because otherwise a local table with a valid shardId * to do that because otherwise a local table with a valid shardId
* appended to its name could be misleading. * appended to its name could be misleading.
*/ */
char *generatedRelationName = get_rel_name(relationId); char *generatedRelationName = get_rel_name(relationOid);
AppendShardIdToName(&generatedRelationName, shardId); AppendShardIdToName(&generatedRelationName, shardId);
if (strncmp(shardRelationName, generatedRelationName, NAMEDATALEN) == 0) if (strncmp(shardRelationName, generatedRelationName, NAMEDATALEN) == 0)
{ {
/* we found the distributed table that the input shard belongs to */ /* we found the distributed table that the input shard belongs to */
return true; return relationOid;
} }
return false; return InvalidOid;
} }

View File

@ -17,7 +17,8 @@ extern bool OverrideTableVisibility;
extern void ReplaceTableVisibleFunction(Node *inputNode); extern void ReplaceTableVisibleFunction(Node *inputNode);
extern bool RelationIsAKnownShard(Oid shardRelationId, bool onlySearchPath); extern bool RelationIsAKnownShard(Oid shardRelationOid, bool onlySearchPath);
extern Oid GetOwnerRelationOid(Oid shardRelationId, bool onlySearchPath);
#endif /* WORKER_SHARD_VISIBILITY_H */ #endif /* WORKER_SHARD_VISIBILITY_H */