* GetCollectionIdsForIndexBuild returns the collectionIds for Index build. * This is to make sure that we process only one index build request for a collection at a time. */
| 1788 | * This is to make sure that we process only one index build request for a collection at a time. |
| 1789 | */ |
| 1790 | uint64 * |
| 1791 | GetCollectionIdsForIndexBuild(List *excludeCollectionIds) |
| 1792 | { |
| 1793 | /* Allocate one more collectionId than MaxNumActiveUsersIndexBuilds so that the last one is always 0 */ |
| 1794 | uint64 *collectionIds = palloc0(sizeof(uint64) * (MaxNumActiveUsersIndexBuilds + 1)); |
| 1795 | |
| 1796 | /* For a collectionId also, if there are multiple requests, we first try to get a request which is in Queued state over Failed requests. |
| 1797 | * |
| 1798 | * SELECT array_agg(a.collection_id) FROM |
| 1799 | * (SELECT collection_id |
| 1800 | * FROM ApiCatalogSchemaName.{ExtensionObjectPrefix}_index_queue pq |
| 1801 | * WHERE collection_id <> ALL($2) |
| 1802 | * ORDER BY min(pq.index_cmd_status) LIMIT MaxNumActiveUsersIndexBuilds |
| 1803 | * ) a; |
| 1804 | */ |
| 1805 | |
| 1806 | StringInfo cmdStr = makeStringInfo(); |
| 1807 | appendStringInfo(cmdStr, |
| 1808 | "SELECT array_agg(a.collection_id) FROM ("); |
| 1809 | appendStringInfo(cmdStr, |
| 1810 | "SELECT collection_id FROM %s iq ", |
| 1811 | GetIndexQueueName()); |
| 1812 | |
| 1813 | if (excludeCollectionIds != NIL) |
| 1814 | { |
| 1815 | appendStringInfo(cmdStr, " WHERE collection_id <> ALL($2) "); |
| 1816 | } |
| 1817 | appendStringInfo(cmdStr, |
| 1818 | " ORDER BY cmd_type ASC, index_cmd_status ASC LIMIT $1"); |
| 1819 | appendStringInfo(cmdStr, ") a"); |
| 1820 | |
| 1821 | int argCount = 1; |
| 1822 | Oid argTypes[2]; |
| 1823 | Datum argValues[2]; |
| 1824 | char argNulls[2]; |
| 1825 | |
| 1826 | argTypes[0] = INT4OID; |
| 1827 | argValues[0] = Int32GetDatum(MaxNumActiveUsersIndexBuilds); |
| 1828 | argNulls[0] = ' '; |
| 1829 | |
| 1830 | if (excludeCollectionIds != NIL) |
| 1831 | { |
| 1832 | ArrayType *collectionIdArray = ConvertUint64ListToArray(excludeCollectionIds); |
| 1833 | argTypes[1] = INT8ARRAYOID; |
| 1834 | argValues[1] = PointerGetDatum(collectionIdArray); |
| 1835 | argNulls[1] = ' '; |
| 1836 | argCount++; |
| 1837 | } |
| 1838 | |
| 1839 | bool isNull = true; |
| 1840 | bool readOnly = true; |
| 1841 | Datum resultDatum = ExtensionExecuteQueryWithArgsViaSPI(cmdStr->data, argCount, |
| 1842 | argTypes, |
| 1843 | argValues, argNulls, |
| 1844 | readOnly, |
| 1845 | SPI_OK_SELECT, &isNull); |
| 1846 | |
| 1847 | if (isNull) |
no test coverage detected