MCPcopy Create free account
hub / github.com/documentdb/documentdb / CheckIndisvalid

Function CheckIndisvalid

pg_documentdb/src/commands/validate.c:231–317  ·  view source on GitHub ↗

* CheckIndisvalid is the internal implementation for checking the indisvalid column in pg_index. */

Source from the content-addressed store, hash-verified

229 * CheckIndisvalid is the internal implementation for checking the indisvalid column in pg_index.
230 */
231static void
232CheckIndisvalid(uint64 collectionId, ValidateResult *result)
233{
234 StringInfo cmdStr = makeStringInfo();
235 appendStringInfo(cmdStr,
236 "WITH collection_pg_indexes AS "
237 "( SELECT idx.indrelid::REGCLASS, i.relname, idx.indisvalid, "
238 "( CASE "
239 "WHEN starts_with(i.relname, 'documents_rum_index_') THEN substring(i.relname, 21)::int "
240 "WHEN starts_with(i.relname, 'collection_pk_"UINT64_FORMAT
241 "') THEN (SELECT index_id from "
242 "%s.collection_indexes where collection_id = "UINT64_FORMAT
243 " AND (index_spec).index_name = '_id_') "
244 "END ) AS indexId "
245 "FROM pg_index idx "
246 "JOIN pg_class as i ON i.oid = idx.indexrelid "
247 "where indrelid::REGCLASS = '%s.documents_"UINT64_FORMAT
248 "'::REGCLASS ORDER BY idx.indrelid) "
249 "SELECT (index_spec).index_name, cpi.indisvalid "
250 "FROM collection_pg_indexes cpi "
251 "JOIN %s.collection_indexes ci ON ci.index_id = cpi.indexId "
252 "ORDER BY cpi.indexId;",
253 collectionId, ApiCatalogSchemaName, collectionId,
254 ApiDataSchemaName, collectionId, ApiCatalogSchemaName);
255
256 pgbson_writer indexValidityWriter;
257 PgbsonWriterInit(&indexValidityWriter);
258 MemoryContext oldContext = CurrentMemoryContext;
259
260 SPI_connect();
261 bool readOnly = true;
262 int tupleCount = 0; /* fetch all applicable rows */
263 int status = SPI_execute(cmdStr->data, readOnly, tupleCount);
264 bool isNull;
265
266 if (status == SPI_OK_SELECT && SPI_tuptable->numvals > 0)
267 {
268 SPITupleTable *tuptable = SPI_tuptable;
269 TupleDesc tupdesc = tuptable->tupdesc;
270
271 for (uint64 row = 0; row < tuptable->numvals; row++)
272 {
273 HeapTuple tuple = tuptable->vals[row];
274
275 /* There are two columns in the result
276 * 1. index_name: name of the index
277 * 2. indisvalid: if the index is valid in pg_index
278 */
279 int columnNumber = 1;
280 Datum indexNameDatum = SPI_getbinval(tuple, tupdesc, columnNumber, &isNull);
281 char *indexName = NULL;
282 bool isIndexValid = true;
283 if (!isNull)
284 {
285 bool typeByValue = false;
286 int typeLength = -1;
287 indexNameDatum = SPI_datumTransfer(indexNameDatum,
288 typeByValue, typeLength);

Callers 1

validateCollectionFunction · 0.85

Calls 5

PgbsonWriterInitFunction · 0.85
PgbsonWriterAppendBoolFunction · 0.85
PgbsonWriterEndDocumentFunction · 0.85
PgbsonWriterGetPgbsonFunction · 0.85

Tested by

no test coverage detected