Skip to content

Commit c01cd9b

Browse files
visridhashuaitian-git
authored andcommitted
Merged PR 1768176: [Index Policies] Add a few more metadata lines in index policy logging
### Does this PR have any customer impact? No ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) Logging ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) No ### Are you introducing any new config? If yes, do you have tests with and without them being set? ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description 1) Track whether or not the index is TTL 2) Track numPaths different from numKeys (needed for composite) 3) Track whether it's a composite index or not Also add a check that checks that the PG index is valid as part of updating the stats before marking the index as valid in the documentdb Catalog ---- #### AI description (iteration 1) #### PR Classification Enhancement: The pull request updates index policy logging by adding new metadata fields and refining index property validations. #### PR Summary This pull request enriches index policy logging by incorporating new utility functions and metadata details to better capture index characteristics and ensure index integrity. - **`src/metadata/index.c`**: Added `IndexSpecIsWildcardIndex` and `IndexSpecIsOrderedIndex` functions to determine index types. - **`src/utils/index_policies_utils.c`**: Enhanced `BuildIndexMetricLog` by logging additional fields such as `numPaths`, `hasTtl`, `hasPfe`, and computing the wildcard and ordered index flags. - **`include/metadata/index.h`**: Introduced new inline functions for boolean conversion with default behaviors. - **Commands modules** (`create_indexes.c`, `coll_mod.c`, and `drop_indexes.c`): Updated validations (e.g., checking index liveliness) and adjusted boolean handling to align with the new metadata scheme. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
1 parent 9791909 commit c01cd9b

5 files changed

Lines changed: 84 additions & 6 deletions

File tree

pg_documentdb/include/metadata/index.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ List * CollectionIdGetIndexNames(uint64 collectionId, bool excludeIdIndex, bool
240240
inProgressOnly);
241241
int CollectionIdGetIndexCount(uint64 collectionId);
242242
int CollectionIdsGetIndexCount(ArrayType *collectionIdsArray);
243+
bool IndexSpecIsWildcardIndex(const IndexSpec *indexSpec);
244+
bool IndexSpecIsOrderedIndex(const IndexSpec *indexSpec);
243245

244246

245247
/* modify/write index metadata */
@@ -320,11 +322,18 @@ const char * GetIndexTypeFromKeyDocument(pgbson *keyDocument);
320322
/* Static utilities */
321323

322324
static inline bool
323-
GetBoolFromBoolIndexOption(BoolIndexOption option)
325+
GetBoolFromBoolIndexOptionDefaultTrue(BoolIndexOption option)
324326
{
325327
return option == BoolIndexOption_Undefined ||
326328
option == BoolIndexOption_False ? false : true;
327329
}
328330

329331

332+
static inline bool
333+
GetBoolFromBoolIndexOptionDefaultFalse(BoolIndexOption option)
334+
{
335+
return option == BoolIndexOption_True;
336+
}
337+
338+
330339
#endif

pg_documentdb/src/commands/coll_mod.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,9 @@ ModifyIndexSpecsInCollection(const MongoCollection *collection,
574574
if ((*specFlags & HAS_INDEX_OPTION_HIDDEN) == HAS_INDEX_OPTION_HIDDEN)
575575
{
576576
PgbsonWriterAppendBool(writer, "hidden_old",
577-
10, GetBoolFromBoolIndexOption(oldHidden));
577+
10, GetBoolFromBoolIndexOptionDefaultTrue(oldHidden));
578578
PgbsonWriterAppendBool(writer, "hidden_new",
579-
10, GetBoolFromBoolIndexOption(newHidden));
579+
10, GetBoolFromBoolIndexOptionDefaultTrue(newHidden));
580580
}
581581

582582
if ((*specFlags & HAS_INDEX_OPTION_EXPIRE_AFTER_SECONDS) ==

pg_documentdb/src/commands/create_indexes.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6372,10 +6372,21 @@ UpdateIndexStatsForPostgresIndex(uint64 collectionId, List *indexIdList)
63726372
List *columnNumbers = NIL;
63736373
if (indexOid != InvalidOid)
63746374
{
6375+
bool indexIsLive = false;
63756376
Relation indexRel = index_open(indexOid, AccessShareLock);
6377+
indexIsLive = indexRel->rd_index->indislive;
63766378
IndexInfo *indexInfo = BuildIndexInfo(indexRel);
63776379
RelationClose(indexRel);
63786380

6381+
/* Use this chance to validate that the index is live*/
6382+
if (!indexIsLive)
6383+
{
6384+
ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INTERNALERROR),
6385+
errmsg(
6386+
"Index %s is not live, but index build was marked as completed",
6387+
indexName)));
6388+
}
6389+
63796390
/* Only do this for RUM style indexes. Vector and Geospatial indexes do need statistics. */
63806391
if (!IsBsonRegularIndexAm(indexInfo->ii_Am))
63816392
{

pg_documentdb/src/commands/drop_indexes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ DropPostgresIndexWithSuffix(uint64 collectionId, IndexDetails *index, bool concu
697697
Assert(suffix != NULL);
698698

699699
StringInfo cmdStr = makeStringInfo();
700-
bool isUnique = GetBoolFromBoolIndexOption(index->indexSpec.indexUnique);
700+
bool isUnique = GetBoolFromBoolIndexOptionDefaultFalse(index->indexSpec.indexUnique);
701701
if (isUnique || (strncmp(index->indexSpec.indexName,
702702
ID_INDEX_NAME, strlen(ID_INDEX_NAME)) == 0))
703703
{

pg_documentdb/src/metadata/index.c

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,6 +2107,65 @@ GetMongoIndexKind(char *indexKindName, bool *isSupported)
21072107
}
21082108

21092109

2110+
bool
2111+
IndexSpecIsWildcardIndex(const IndexSpec *indexSpec)
2112+
{
2113+
if (indexSpec->indexWPDocument != NULL)
2114+
{
2115+
return true;
2116+
}
2117+
2118+
if (indexSpec->indexKeyDocument == NULL)
2119+
{
2120+
return false;
2121+
}
2122+
2123+
bson_iter_t keyDocument;
2124+
PgbsonInitIterator(indexSpec->indexKeyDocument, &keyDocument);
2125+
while (bson_iter_next(&keyDocument))
2126+
{
2127+
const StringView keyView = bson_iter_key_string_view(&keyDocument);
2128+
if (StringViewEndsWithString(&keyView, "$**"))
2129+
{
2130+
return true;
2131+
}
2132+
}
2133+
2134+
return false;
2135+
}
2136+
2137+
2138+
inline static bool
2139+
IsOptionsKeyOrderedIndex(const char *key)
2140+
{
2141+
return strcmp(key, "enableCompositeTerm") == 0 ||
2142+
strcmp(key, "enableOrderedIndex") == 0;
2143+
}
2144+
2145+
2146+
bool
2147+
IndexSpecIsOrderedIndex(const IndexSpec *indexSpec)
2148+
{
2149+
if (indexSpec->indexOptions == NULL)
2150+
{
2151+
return false;
2152+
}
2153+
2154+
bson_iter_t iter;
2155+
PgbsonInitIterator(indexSpec->indexOptions, &iter);
2156+
while (bson_iter_next(&iter))
2157+
{
2158+
const char *key = bson_iter_key(&iter);
2159+
if (IsOptionsKeyOrderedIndex(key))
2160+
{
2161+
return BsonValueAsBool(bson_iter_value(&iter));
2162+
}
2163+
}
2164+
2165+
return false;
2166+
}
2167+
2168+
21102169
/*
21112170
* Serializes the IndexKey to a GetIndexes friendly manner.
21122171
* Returns any additional Text indexes that were captured from processing
@@ -2413,8 +2472,7 @@ static bool
24132472
AreIndexOptionsStillEquivalent(const char *path, const bson_value_t *left,
24142473
const bson_value_t *right)
24152474
{
2416-
if (strcmp(path, "enableCompositeTerm") == 0 ||
2417-
strcmp(path, "enableOrderedIndex") == 0)
2475+
if (IsOptionsKeyOrderedIndex(path))
24182476
{
24192477
if (left == NULL && right == NULL)
24202478
{

0 commit comments

Comments
 (0)