| 3144 | |
| 3145 | |
| 3146 | static void |
| 3147 | UpdateIdIndexToSetStatsState(uint64 collectionId, bool enableStats) |
| 3148 | { |
| 3149 | StringInfoData queryStringInfo; |
| 3150 | initStringInfo(&queryStringInfo); |
| 3151 | |
| 3152 | appendStringInfo(&queryStringInfo, |
| 3153 | "SELECT index_id, index_spec FROM %s.collection_indexes" |
| 3154 | " WHERE collection_id = %lu AND (index_spec).index_name = '_id_' LIMIT 1 FOR UPDATE", |
| 3155 | ApiCatalogSchemaName, collectionId); |
| 3156 | |
| 3157 | /* all args are non-null */ |
| 3158 | bool readOnly = false; |
| 3159 | int numValues = 2; |
| 3160 | bool isNull[2]; |
| 3161 | Datum results[2]; |
| 3162 | ExtensionExecuteMultiValueQueryViaSPI(queryStringInfo.data, readOnly, |
| 3163 | SPI_OK_SELECT, results, isNull, |
| 3164 | numValues); |
| 3165 | |
| 3166 | if (isNull[0] || isNull[1]) |
| 3167 | { |
| 3168 | ereport(ERROR, (errmsg( |
| 3169 | "Could not find primary key index for collection. This is unexpected"))); |
| 3170 | } |
| 3171 | |
| 3172 | IndexDetails indexDetails = { 0 }; |
| 3173 | indexDetails.indexId = DatumGetInt32(results[0]); |
| 3174 | indexDetails.indexSpec = *DatumGetIndexSpec(results[1]); |
| 3175 | indexDetails.collectionId = collectionId; |
| 3176 | |
| 3177 | pgbson *indexOptions = indexDetails.indexSpec.indexOptions; |
| 3178 | if (indexOptions == NULL) |
| 3179 | { |
| 3180 | indexOptions = PgbsonInitEmpty(); |
| 3181 | } |
| 3182 | |
| 3183 | bson_iter_t iter; |
| 3184 | pgbson_writer optionsWriter; |
| 3185 | PgbsonWriterInit(&optionsWriter); |
| 3186 | PgbsonInitIterator(indexOptions, &iter); |
| 3187 | bool foundStatsEnabled = false; |
| 3188 | while (bson_iter_next(&iter)) |
| 3189 | { |
| 3190 | const char *key = bson_iter_key(&iter); |
| 3191 | const bson_value_t *value = bson_iter_value(&iter); |
| 3192 | if (strcmp(key, "statsEnabled") == 0) |
| 3193 | { |
| 3194 | foundStatsEnabled = true; |
| 3195 | |
| 3196 | /* Skip writing the field if it's false (disabled) */ |
| 3197 | if (enableStats) |
| 3198 | { |
| 3199 | PgbsonWriterAppendBool(&optionsWriter, key, -1, true); |
| 3200 | } |
| 3201 | } |
| 3202 | else |
| 3203 | { |
no test coverage detected