From 56f1daa80068673832ae3987cf08bafca9e7fdc0 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Thu, 22 Jun 2023 18:23:55 +0300 Subject: [PATCH] Refactor the code that extends constraint/index names on shards into a func --- .../distributed/relay/relay_event_utility.c | 93 +++++++++++-------- 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/src/backend/distributed/relay/relay_event_utility.c b/src/backend/distributed/relay/relay_event_utility.c index b7629a38b..6924b8e8d 100644 --- a/src/backend/distributed/relay/relay_event_utility.c +++ b/src/backend/distributed/relay/relay_event_utility.c @@ -54,6 +54,9 @@ #include "utils/relcache.h" /* Local functions forward declarations */ +static void RelayEventExtendConstraintAndIndexNames(AlterTableStmt *alterTableStmt, + Constraint *constraint, + uint64 shardId); static bool UpdateWholeRowColumnReferencesWalker(Node *node, uint64 *shardId); /* exports for SQL callable functions */ @@ -150,44 +153,8 @@ RelayEventExtendNames(Node *parseTree, char *schemaName, uint64 shardId) if (command->subtype == AT_AddConstraint) { Constraint *constraint = (Constraint *) command->def; - char **constraintName = &(constraint->conname); - const bool missingOk = false; - relationId = RangeVarGetRelid(alterTableStmt->relation, - AccessShareLock, - missingOk); - - if (constraint->indexname) - { - char **indexName = &(constraint->indexname); - AppendShardIdToName(indexName, shardId); - } - - /* - * Append shardId to constraint names if - * - table is not partitioned or - * - constraint is not a CHECK constraint - * - * We do not want to append shardId to partitioned table shards because - * the names of constraints will be inherited, and the shardId will no - * longer be valid for the child table. - * - * See MergeConstraintsIntoExisting function in Postgres that requires - * inherited check constraints in child tables to have the same name - * with those in parent tables. - */ - if (!PartitionedTable(relationId) || - constraint->contype != CONSTR_CHECK) - { - /* - * constraint->conname could be empty in the case of - * ADD {PRIMARY KEY, UNIQUE} USING INDEX. - * In this case, already extended index name will be used by postgres. - */ - if (constraint->conname != NULL) - { - AppendShardIdToName(constraintName, shardId); - } - } + RelayEventExtendConstraintAndIndexNames(alterTableStmt, constraint, + shardId); } else if (command->subtype == AT_DropConstraint || command->subtype == AT_ValidateConstraint) @@ -622,6 +589,56 @@ RelayEventExtendNames(Node *parseTree, char *schemaName, uint64 shardId) } +/* + * RelayEventExtendConstraintAndIndexNames extends the names of constraints + * and indexes in given constraint with the shardId. + */ +static void +RelayEventExtendConstraintAndIndexNames(AlterTableStmt *alterTableStmt, + Constraint *constraint, + uint64 shardId) +{ + char **constraintName = &(constraint->conname); + const bool missingOk = false; + Oid relationId = RangeVarGetRelid(alterTableStmt->relation, + AccessShareLock, + missingOk); + + if (constraint->indexname) + { + char **indexName = &(constraint->indexname); + AppendShardIdToName(indexName, shardId); + } + + /* + * Append shardId to constraint names if + * - table is not partitioned or + * - constraint is not a CHECK constraint + * + * We do not want to append shardId to partitioned table shards because + * the names of constraints will be inherited, and the shardId will no + * longer be valid for the child table. + * + * See MergeConstraintsIntoExisting function in Postgres that requires + * inherited check constraints in child tables to have the same name + * with those in parent tables. + */ + if (!PartitionedTable(relationId) || + constraint->contype != CONSTR_CHECK) + { + /* + * constraint->conname could be empty in the case of + * ADD {PRIMARY KEY, UNIQUE} USING INDEX. + * In this case, already extended index name will be used by postgres. + */ + if (constraint->conname != NULL) + { + AppendShardIdToName(constraintName, shardId); + } + } +} + + /* * RelayEventExtendNamesForInterShardCommands extends relation names in the given parse * tree for certain utility commands. The function more specifically extends table, index