Update distribution column for citus local tables

update-dist-col-for-undistributed-tables
Ahmet Gedemenli 2022-02-16 17:53:26 +03:00
parent f2f7497ab8
commit bcc6165113
1 changed files with 22 additions and 0 deletions

View File

@ -402,11 +402,33 @@ CreateDistributedTable(Oid relationId, Var *distributionColumn, char distributio
List *originalForeignKeyRecreationCommands = NIL; List *originalForeignKeyRecreationCommands = NIL;
if (IsCitusTableType(relationId, CITUS_LOCAL_TABLE)) if (IsCitusTableType(relationId, CITUS_LOCAL_TABLE))
{ {
/* save the column name to update the distribution column var later */
char *distributionColumnName =
ColumnToColumnName(relationId, nodeToString(distributionColumn));
/* store foreign key creation commands that relation is involved */ /* store foreign key creation commands that relation is involved */
originalForeignKeyRecreationCommands = originalForeignKeyRecreationCommands =
GetFKeyCreationCommandsRelationInvolvedWithTableType(relationId, GetFKeyCreationCommandsRelationInvolvedWithTableType(relationId,
INCLUDE_ALL_TABLE_TYPES); INCLUDE_ALL_TABLE_TYPES);
relationId = DropFKeysAndUndistributeTable(relationId); relationId = DropFKeysAndUndistributeTable(relationId);
/*
* Because we create a new table when undistributing the table, attribute numbers
* might be changed, because of the dropped columns. To make sure we have the
* correct distribution column, we need to build it again, using the original
* column name that we have saved before.
*/
Relation relation = try_relation_open(relationId, ExclusiveLock);
if (relation == NULL)
{
ereport(ERROR, (errmsg("could not open the relation %u", relationId),
errdetail("When creating the distribution column.")));
}
relation_close(relation, NoLock);
distributionColumn = BuildDistributionKeyFromColumnName(relation,
distributionColumnName);
} }
/* /*