From 3d69ab55760a906e477a09973c8652b4e11f2579 Mon Sep 17 00:00:00 2001 From: SaitTalhaNisanci Date: Fri, 22 Jan 2021 13:28:43 +0300 Subject: [PATCH] Choose the smallest colocation id among all matches (#4559) Currently we choose an arbitrary colocation id from all the matches for a colocation id. This could mean that 2 distributed tables, which have the same scheme could go into different colocation groups. This fix makes sure that the same match will go to the same colocation group. --- src/backend/distributed/utils/colocation_utils.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/utils/colocation_utils.c b/src/backend/distributed/utils/colocation_utils.c index b1038fd5f..49a0680d5 100644 --- a/src/backend/distributed/utils/colocation_utils.c +++ b/src/backend/distributed/utils/colocation_utils.c @@ -541,12 +541,22 @@ ColocationId(int shardCount, int replicationFactor, Oid distributionColumnType, indexOK, NULL, scanKeyCount, scanKey); HeapTuple colocationTuple = systable_getnext(scanDescriptor); - if (HeapTupleIsValid(colocationTuple)) + + while (HeapTupleIsValid(colocationTuple)) { Form_pg_dist_colocation colocationForm = (Form_pg_dist_colocation) GETSTRUCT(colocationTuple); - colocationId = colocationForm->colocationid; + if (colocationId == INVALID_COLOCATION_ID || colocationId > + colocationForm->colocationid) + { + /* + * We assign the smallest colocation id among all the matches so that we + * assign the same colocation group for similar distributed tables + */ + colocationId = colocationForm->colocationid; + } + colocationTuple = systable_getnext(scanDescriptor); } systable_endscan(scanDescriptor);