Suggestions on the PR

talha_tes1
Ahmet Gedemenli 2021-10-22 18:40:09 +03:00
parent ca757b5bc5
commit cb2538a5f0
5 changed files with 39 additions and 21 deletions

View File

@ -482,6 +482,8 @@ CreateDistributedTable(Oid relationId, Var *distributionColumn, char distributio
/* we need to calculate these variables before creating distributed metadata */ /* we need to calculate these variables before creating distributed metadata */
bool localTableEmpty = TableEmpty(relationId); bool localTableEmpty = TableEmpty(relationId);
Oid colocatedTableId = ColocatedTableId(colocationId); Oid colocatedTableId = ColocatedTableId(colocationId);
/* setting to false since this flag is only valid for citus local tables */
bool autoConverted = false; bool autoConverted = false;
/* create an entry for distributed table in pg_dist_partition */ /* create an entry for distributed table in pg_dist_partition */

View File

@ -70,6 +70,8 @@ static void ErrorIfAttachCitusTableToPgLocalTable(Oid parentRelationId,
Oid partitionRelationId); Oid partitionRelationId);
static bool AlterTableDefinesFKeyBetweenPostgresAndNonDistTable( static bool AlterTableDefinesFKeyBetweenPostgresAndNonDistTable(
AlterTableStmt *alterTableStatement); AlterTableStmt *alterTableStatement);
static bool ShouldMarkConnectedRelationsNotAutoConverted(Oid leftRelationId,
Oid rightRelationId);
static void MarkConnectedRelationsNotAutoConverted(Oid leftRelationId, static void MarkConnectedRelationsNotAutoConverted(Oid leftRelationId,
Oid rightRelationId); Oid rightRelationId);
static bool RelationIdListContainsCitusTableType(List *relationIdList, static bool RelationIdListContainsCitusTableType(List *relationIdList,
@ -494,7 +496,8 @@ PreprocessAttachPartitionToCitusTable(Oid parentRelationId, Oid partitionRelatio
* cannot have non-inherited foreign keys. * cannot have non-inherited foreign keys.
*/ */
bool cascadeViaForeignKeys = false; bool cascadeViaForeignKeys = false;
bool autoConverted = true; CitusTableCacheEntry *entry = GetCitusTableCacheEntry(parentRelationId);
bool autoConverted = entry->autoConverted;
CreateCitusLocalTable(partitionRelationId, cascadeViaForeignKeys, CreateCitusLocalTable(partitionRelationId, cascadeViaForeignKeys,
autoConverted); autoConverted);
} }
@ -863,7 +866,12 @@ PreprocessAlterTableStmt(Node *node, const char *alterTableCommand,
rightRelationId = RangeVarGetRelid(constraint->pktable, lockmode, rightRelationId = RangeVarGetRelid(constraint->pktable, lockmode,
alterTableStatement->missing_ok); alterTableStatement->missing_ok);
MarkConnectedRelationsNotAutoConverted(leftRelationId, rightRelationId); if (ShouldMarkConnectedRelationsNotAutoConverted(leftRelationId,
rightRelationId))
{
MarkConnectedRelationsNotAutoConverted(leftRelationId,
rightRelationId);
}
/* /*
* Foreign constraint validations will be done in workers. If we do not * Foreign constraint validations will be done in workers. If we do not
@ -1178,36 +1186,42 @@ AlterTableDefinesFKeyBetweenPostgresAndNonDistTable(AlterTableStmt *alterTableSt
/* /*
* MarkConnectedRelationsNotAutoConverted takes two relations. If both of them are Citus Local * ShouldMarkConnectedRelationsNotAutoConverted takes two relations.
* Tables, and one of them is auto-converted while the other one is not; then it * If both of them are Citus Local Tables, and one of them is auto-converted while the
* marks both of them as not-auto-converted, as well as other connected relations. * other one is not; then it returns true. False otherwise.
*/ */
static void static bool
MarkConnectedRelationsNotAutoConverted(Oid leftRelationId, Oid rightRelationId) ShouldMarkConnectedRelationsNotAutoConverted(Oid leftRelationId, Oid rightRelationId)
{ {
if (!IsCitusTable(leftRelationId) || !IsCitusTable(rightRelationId)) if (!IsCitusTable(leftRelationId) || !IsCitusTable(rightRelationId))
{ {
return; return false;
} }
if (!IsCitusTableType(leftRelationId, CITUS_LOCAL_TABLE)) if (!IsCitusTableType(leftRelationId, CITUS_LOCAL_TABLE))
{ {
return; return false;
} }
if (!IsCitusTableType(rightRelationId, CITUS_LOCAL_TABLE)) if (!IsCitusTableType(rightRelationId, CITUS_LOCAL_TABLE))
{ {
return; return false;
} }
CitusTableCacheEntry *entryLeft = GetCitusTableCacheEntry(leftRelationId); CitusTableCacheEntry *entryLeft = GetCitusTableCacheEntry(leftRelationId);
CitusTableCacheEntry *entryRight = GetCitusTableCacheEntry(rightRelationId); CitusTableCacheEntry *entryRight = GetCitusTableCacheEntry(rightRelationId);
if (entryLeft->autoConverted == entryRight->autoConverted) return entryLeft->autoConverted != entryRight->autoConverted;
{
return;
} }
/*
* MarkConnectedRelationsNotAutoConverted takes two relations.
* Marks both of them as not-auto-converted, as well as other connected relations.
*/
static void
MarkConnectedRelationsNotAutoConverted(Oid leftRelationId, Oid rightRelationId)
{
List *leftConnectedRelIds = GetForeignKeyConnectedRelationIdList(leftRelationId); List *leftConnectedRelIds = GetForeignKeyConnectedRelationIdList(leftRelationId);
List *rightConnectedRelIds = GetForeignKeyConnectedRelationIdList(rightRelationId); List *rightConnectedRelIds = GetForeignKeyConnectedRelationIdList(rightRelationId);
List *allConnectedRelations = list_concat_unique_oid(leftConnectedRelIds, List *allConnectedRelations = list_concat_unique_oid(leftConnectedRelIds,

View File

@ -2074,6 +2074,8 @@ citus_internal_add_partition_metadata(PG_FUNCTION_ARGS)
text *distributionColumnText = NULL; text *distributionColumnText = NULL;
char *distributionColumnString = NULL; char *distributionColumnString = NULL;
Var *distributionColumnVar = NULL; Var *distributionColumnVar = NULL;
/* this flag is only valid for citus local tables, so set it to false */
bool autoConverted = false; bool autoConverted = false;
/* only owner of the table (or superuser) is allowed to add the Citus metadata */ /* only owner of the table (or superuser) is allowed to add the Citus metadata */

View File

@ -2072,10 +2072,10 @@ UpdatePlacementGroupId(uint64 placementId, int groupId)
/* /*
* UpdatePartitionAutoConverted sets the autoConverted for the partition identified * UpdatePartitionAutoConverted sets the autoConverted for the partition identified
* by distributedRelationId. * by citusTableId.
*/ */
void void
UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted) UpdatePartitionAutoConverted(Oid citusTableId, bool autoConverted)
{ {
ScanKeyData scanKey[1]; ScanKeyData scanKey[1];
int scanKeyCount = 1; int scanKeyCount = 1;
@ -2087,7 +2087,7 @@ UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted)
Relation pgDistPartition = table_open(DistPartitionRelationId(), RowExclusiveLock); Relation pgDistPartition = table_open(DistPartitionRelationId(), RowExclusiveLock);
TupleDesc tupleDescriptor = RelationGetDescr(pgDistPartition); TupleDesc tupleDescriptor = RelationGetDescr(pgDistPartition);
ScanKeyInit(&scanKey[0], Anum_pg_dist_partition_logicalrelid, ScanKeyInit(&scanKey[0], Anum_pg_dist_partition_logicalrelid,
BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(distributedRelationId)); BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(citusTableId));
SysScanDesc scanDescriptor = systable_beginscan(pgDistPartition, SysScanDesc scanDescriptor = systable_beginscan(pgDistPartition,
DistPartitionLogicalRelidIndexId(), DistPartitionLogicalRelidIndexId(),
@ -2097,8 +2097,8 @@ UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted)
HeapTuple heapTuple = systable_getnext(scanDescriptor); HeapTuple heapTuple = systable_getnext(scanDescriptor);
if (!HeapTupleIsValid(heapTuple)) if (!HeapTupleIsValid(heapTuple))
{ {
ereport(ERROR, (errmsg("could not find valid entry for partition oid: %u", ereport(ERROR, (errmsg("could not find valid entry for citus table with oid: %u",
distributedRelationId))); citusTableId)));
} }
memset(replace, 0, sizeof(replace)); memset(replace, 0, sizeof(replace));
@ -2111,7 +2111,7 @@ UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted)
CatalogTupleUpdate(pgDistPartition, &heapTuple->t_self, heapTuple); CatalogTupleUpdate(pgDistPartition, &heapTuple->t_self, heapTuple);
CitusInvalidateRelcacheByRelid(distributedRelationId); CitusInvalidateRelcacheByRelid(citusTableId);
CommandCounterIncrement(); CommandCounterIncrement();

View File

@ -235,7 +235,7 @@ extern uint64 InsertShardPlacementRow(uint64 shardId, uint64 placementId,
extern void InsertIntoPgDistPartition(Oid relationId, char distributionMethod, extern void InsertIntoPgDistPartition(Oid relationId, char distributionMethod,
Var *distributionColumn, uint32 colocationId, Var *distributionColumn, uint32 colocationId,
char replicationModel, bool autoConverted); char replicationModel, bool autoConverted);
extern void UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted); extern void UpdatePartitionAutoConverted(Oid citusTableId, bool autoConverted);
extern void DeletePartitionRow(Oid distributedRelationId); extern void DeletePartitionRow(Oid distributedRelationId);
extern void DeleteShardRow(uint64 shardId); extern void DeleteShardRow(uint64 shardId);
extern void UpdateShardPlacementState(uint64 placementId, char shardState); extern void UpdateShardPlacementState(uint64 placementId, char shardState);