From 2d20fa37adaffab42a7309638192832d7d58c1f3 Mon Sep 17 00:00:00 2001 From: Ahmet Gedemenli Date: Fri, 24 Sep 2021 15:28:54 +0300 Subject: [PATCH] Mark the given table as not auto-converted --- .../citus_add_local_table_to_metadata.c | 16 +++++- .../distributed/metadata/metadata_utility.c | 50 +++++++++++++++++++ .../utils/foreign_key_relationship.c | 2 +- src/include/distributed/metadata_utility.h | 1 + 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c index 06e6bd29b..882b663ed 100644 --- a/src/backend/distributed/commands/citus_add_local_table_to_metadata.c +++ b/src/backend/distributed/commands/citus_add_local_table_to_metadata.c @@ -87,6 +87,8 @@ PG_FUNCTION_INFO_V1(citus_add_local_table_to_metadata); PG_FUNCTION_INFO_V1(create_citus_local_table); PG_FUNCTION_INFO_V1(remove_local_tables_from_metadata); +bool autoConverted = false; + /* * citus_add_local_table_to_metadata creates a citus local table from the table with @@ -258,6 +260,11 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys) "cascade_via_foreign_keys=>true)", qualifiedRelationName, qualifiedRelationName))); } + /* save the relation name, to obtain the shell relation id later */ + char *relationName = get_rel_name(relationId); + Oid relationSchemaId = get_rel_namespace(relationId); + + autoConverted = true; /* * By acquiring AccessExclusiveLock, make sure that no modifications happen @@ -266,6 +273,13 @@ CreateCitusLocalTable(Oid relationId, bool cascadeViaForeignKeys) CascadeOperationForFkeyConnectedRelations(relationId, lockMode, CASCADE_ADD_LOCAL_TABLE_TO_METADATA); + autoConverted = false; + + Oid shellRelationId = get_relname_relid(relationName, relationSchemaId); + + /* mark the shell relation with autoConverted=false, as it was a user request */ + UpdatePartitionAutoConverted(shellRelationId, autoConverted); + /* * We converted every foreign key connected table in our subgraph * including itself to a citus local table, so return here. @@ -1102,7 +1116,7 @@ InsertMetadataForCitusLocalTable(Oid citusLocalTableId, uint64 shardId) Var *distributionColumn = NULL; InsertIntoPgDistPartition(citusLocalTableId, distributionMethod, distributionColumn, colocationId, - replicationModel, false); + replicationModel, autoConverted); /* set shard storage type according to relation type */ char shardStorageType = ShardStorageType(citusLocalTableId); diff --git a/src/backend/distributed/metadata/metadata_utility.c b/src/backend/distributed/metadata/metadata_utility.c index 9063deb3c..8c588d73d 100644 --- a/src/backend/distributed/metadata/metadata_utility.c +++ b/src/backend/distributed/metadata/metadata_utility.c @@ -2070,6 +2070,56 @@ UpdatePlacementGroupId(uint64 placementId, int groupId) } +/* + * UpdatePartitionAutoConverted sets the autoConverted for the partition identified + * by distributedRelationId. + */ +void +UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted) +{ + ScanKeyData scanKey[1]; + int scanKeyCount = 1; + bool indexOK = true; + Datum values[Natts_pg_dist_partition]; + bool isnull[Natts_pg_dist_partition]; + bool replace[Natts_pg_dist_partition]; + + Relation pgDistPartition = table_open(DistPartitionRelationId(), RowExclusiveLock); + TupleDesc tupleDescriptor = RelationGetDescr(pgDistPartition); + ScanKeyInit(&scanKey[0], Anum_pg_dist_partition_logicalrelid, + BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(distributedRelationId)); + + SysScanDesc scanDescriptor = systable_beginscan(pgDistPartition, + DistPartitionLogicalRelidIndexId(), + indexOK, + NULL, scanKeyCount, scanKey); + + HeapTuple heapTuple = systable_getnext(scanDescriptor); + if (!HeapTupleIsValid(heapTuple)) + { + ereport(ERROR, (errmsg("could not find valid entry for partition oid: %u", + distributedRelationId))); + } + + memset(replace, 0, sizeof(replace)); + + values[Anum_pg_dist_partition_autoconverted - 1] = BoolGetDatum(autoConverted); + isnull[Anum_pg_dist_partition_autoconverted - 1] = false; + replace[Anum_pg_dist_partition_autoconverted - 1] = true; + + heapTuple = heap_modify_tuple(heapTuple, tupleDescriptor, values, isnull, replace); + + CatalogTupleUpdate(pgDistPartition, &heapTuple->t_self, heapTuple); + + CitusInvalidateRelcacheByRelid(distributedRelationId); + + CommandCounterIncrement(); + + systable_endscan(scanDescriptor); + table_close(pgDistPartition, NoLock); +} + + /* * Check that the current user has `mode` permissions on relationId, error out * if not. Superusers always have such permissions. diff --git a/src/backend/distributed/utils/foreign_key_relationship.c b/src/backend/distributed/utils/foreign_key_relationship.c index ff823415c..55b5ae258 100644 --- a/src/backend/distributed/utils/foreign_key_relationship.c +++ b/src/backend/distributed/utils/foreign_key_relationship.c @@ -164,7 +164,7 @@ ShouldUndistributeCitusLocalTable(Oid relationId) */ return false; } - if (!AutoConvertedViaCatalog(relationId)) + if (!AutoConvertedViaCatalog(relationOid)) { /* * The relation is connected to a (or, is a) Citus Local Table created diff --git a/src/include/distributed/metadata_utility.h b/src/include/distributed/metadata_utility.h index f6e8a81a5..08f0fe8f0 100644 --- a/src/include/distributed/metadata_utility.h +++ b/src/include/distributed/metadata_utility.h @@ -235,6 +235,7 @@ extern uint64 InsertShardPlacementRow(uint64 shardId, uint64 placementId, extern void InsertIntoPgDistPartition(Oid relationId, char distributionMethod, Var *distributionColumn, uint32 colocationId, char replicationModel, bool autoConverted); +extern void UpdatePartitionAutoConverted(Oid distributedRelationId, bool autoConverted); extern void DeletePartitionRow(Oid distributedRelationId); extern void DeleteShardRow(uint64 shardId); extern void UpdateShardPlacementState(uint64 placementId, char shardState);