From f3f711e0975d5e373c78d48c9b6df7a6af63d3e5 Mon Sep 17 00:00:00 2001 From: Onur Tirtir Date: Thu, 4 Jun 2020 17:28:43 +0300 Subject: [PATCH] Implement IndexIsImpliedByAConstraint --- .../distributed/master/master_node_protocol.c | 57 ++++++++++++------- src/include/distributed/master_protocol.h | 1 + 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/backend/distributed/master/master_node_protocol.c b/src/backend/distributed/master/master_node_protocol.c index 1d03aab34..585a4be3f 100644 --- a/src/backend/distributed/master/master_node_protocol.c +++ b/src/backend/distributed/master/master_node_protocol.c @@ -674,31 +674,12 @@ GetTableIndexAndConstraintCommands(Oid relationId) { Form_pg_index indexForm = (Form_pg_index) GETSTRUCT(heapTuple); Oid indexId = indexForm->indexrelid; - bool isConstraint = false; char *statementDef = NULL; - /* - * A primary key index is always created by a constraint statement. - * A unique key index or exclusion index is created by a constraint - * if and only if the index has a corresponding constraint entry in pg_depend. - * Any other index form is never associated with a constraint. - */ - if (indexForm->indisprimary) - { - isConstraint = true; - } - else if (indexForm->indisunique || indexForm->indisexclusion) - { - Oid constraintId = get_index_constraint(indexId); - isConstraint = OidIsValid(constraintId); - } - else - { - isConstraint = false; - } + bool indexImpliedByConstraint = IndexImpliedByAConstraint(indexForm); /* get the corresponding constraint or index statement */ - if (isConstraint) + if (indexImpliedByConstraint) { Oid constraintId = get_index_constraint(indexId); Assert(constraintId != InvalidOid); @@ -736,6 +717,40 @@ GetTableIndexAndConstraintCommands(Oid relationId) } +/* + * IndexImpliedByAConstraint is an helper function to be used while scanning + * pg_index. It returns true if the index identified by the given indexForm is + * implied by a constraint. Note that caller is responsible for passing a valid + * indexFrom, which means an alive heap tuple which is of form Form_pg_index. + */ +bool +IndexImpliedByAConstraint(Form_pg_index indexForm) +{ + Assert(indexForm != NULL); + + bool indexImpliedByConstraint = false; + + /* + * A primary key index is always created by a constraint statement. + * A unique key index or exclusion index is created by a constraint + * if and only if the index has a corresponding constraint entry in + * pg_depend. Any other index form is never associated with a constraint. + */ + if (indexForm->indisprimary) + { + indexImpliedByConstraint = true; + } + else if (indexForm->indisunique || indexForm->indisexclusion) + { + Oid constraintId = get_index_constraint(indexForm->indexrelid); + + indexImpliedByConstraint = OidIsValid(constraintId); + } + + return indexImpliedByConstraint; +} + + /* * ShardStorageType returns the shard storage type according to relation type. */ diff --git a/src/include/distributed/master_protocol.h b/src/include/distributed/master_protocol.h index 1755a101d..14743a5c3 100644 --- a/src/include/distributed/master_protocol.h +++ b/src/include/distributed/master_protocol.h @@ -105,6 +105,7 @@ extern Oid ResolveRelationId(text *relationName, bool missingOk); extern List * GetTableDDLEvents(Oid relationId, bool forShardCreation); extern List * GetTableCreationCommands(Oid relationId, bool forShardCreation); extern List * GetTableIndexAndConstraintCommands(Oid relationId); +extern bool IndexImpliedByAConstraint(Form_pg_index indexForm); extern char ShardStorageType(Oid relationId); extern void CheckDistributedTable(Oid relationId); extern void CreateAppendDistributedShardPlacements(Oid relationId, int64 shardId,