* The core logic that queries each worker and gets the * index statistics. * The logic is simply to call pg_stat_all_indexes and get * the index accesses per index. */
| 123 | * the index accesses per index. |
| 124 | */ |
| 125 | static pgbson * |
| 126 | IndexStatsWorker(void *fcinfoPointer) |
| 127 | { |
| 128 | PG_FUNCTION_ARGS = fcinfoPointer; |
| 129 | Datum databaseName = PG_GETARG_DATUM(0); |
| 130 | Datum collectionName = PG_GETARG_DATUM(1); |
| 131 | |
| 132 | MongoCollection *collection = |
| 133 | GetMongoCollectionByNameDatum(databaseName, |
| 134 | collectionName, |
| 135 | AccessShareLock); |
| 136 | |
| 137 | if (collection == NULL) |
| 138 | { |
| 139 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_INVALIDNAMESPACE), |
| 140 | errmsg("Collection '%s.%s' does not exist", |
| 141 | TextDatumGetCString(databaseName), |
| 142 | TextDatumGetCString(collectionName)))); |
| 143 | } |
| 144 | |
| 145 | /* First step, get the relevant shards on this node (We're already in the query worker) */ |
| 146 | ArrayType *shardNames = NULL; |
| 147 | ArrayType *shardOids = NULL; |
| 148 | |
| 149 | /* Next get the relation and table size */ |
| 150 | pgbson_writer writer; |
| 151 | PgbsonWriterInit(&writer); |
| 152 | |
| 153 | /* Only do work if there are shards */ |
| 154 | if (!GetMongoCollectionShardOidsAndNames(collection, &shardOids, &shardNames)) |
| 155 | { |
| 156 | return PgbsonWriterGetPgbson(&writer); |
| 157 | } |
| 158 | |
| 159 | Assert(shardNames != NULL); |
| 160 | |
| 161 | /* |
| 162 | * Walk the indexes for these shards and write out their sizes. |
| 163 | */ |
| 164 | pgbson_writer indexWriter; |
| 165 | PgbsonWriterStartDocument(&writer, IndexUsageKey, -1, &indexWriter); |
| 166 | |
| 167 | const char *query = |
| 168 | "SELECT indexrelid, idx_scan FROM pg_catalog.pg_stat_all_indexes " |
| 169 | " WHERE relid =ANY ($1)"; |
| 170 | |
| 171 | int nargs = 1; |
| 172 | Oid argTypes[1] = { OIDARRAYOID }; |
| 173 | Datum argValues[1] = { PointerGetDatum(shardOids) }; |
| 174 | |
| 175 | bool readOnly = true; |
| 176 | MemoryContext priorMemoryContext = CurrentMemoryContext; |
| 177 | |
| 178 | HTAB *indexHash = CreatePgbsonElementHashSet(); |
| 179 | SPI_connect(); |
| 180 | |
| 181 | Portal statsPortal = SPI_cursor_open_with_args("workerIndexUsageStats", query, nargs, |
| 182 | argTypes, argValues, |
nothing calls this directly
no test coverage detected