in src/backend/utils/adt/agtype_gin.c [309:388]
Datum gin_consistent_agtype(PG_FUNCTION_ARGS)
{
bool *check;
StrategyNumber strategy;
int32 nkeys;
bool *recheck;
bool res = true;
int32 i;
if (PG_ARGISNULL(0) || PG_ARGISNULL(1) ||
PG_ARGISNULL(3) || PG_ARGISNULL(5))
{
PG_RETURN_NULL();
}
check = (bool *) PG_GETARG_POINTER(0);
strategy = PG_GETARG_UINT16(1);
nkeys = PG_GETARG_INT32(3);
recheck = (bool *) PG_GETARG_POINTER(5);
if (strategy == AGTYPE_CONTAINS_STRATEGY_NUMBER ||
strategy == AGTYPE_CONTAINS_TOP_LEVEL_STRATEGY_NUMBER)
{
/*
* We must always recheck, since we can't tell from the index whether
* the positions of the matched items match the structure of the query
* object. (Even if we could, we'd also have to worry about hashed
* keys and the index's failure to distinguish keys from string array
* elements.) However, the tuple certainly doesn't match unless it
* contains all the query keys.
*/
*recheck = true;
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else if (strategy == AGTYPE_EXISTS_STRATEGY_NUMBER)
{
/*
* Although the key is certainly present in the index, we must recheck
* because (1) the key might be hashed, and (2) the index match might
* be for a key that's not at top level of the JSON object. For (1),
* we could look at the query key to see if it's hashed and not
* recheck if not, but the index lacks enough info to tell about (2).
*/
*recheck = true;
res = true;
}
else if (strategy == AGTYPE_EXISTS_ANY_STRATEGY_NUMBER)
{
/* As for plain exists, we must recheck */
*recheck = true;
res = true;
}
else if (strategy == AGTYPE_EXISTS_ALL_STRATEGY_NUMBER)
{
/* As for plain exists, we must recheck */
*recheck = true;
/* ... but unless all the keys are present, we can say "false" */
for (i = 0; i < nkeys; i++)
{
if (!check[i])
{
res = false;
break;
}
}
}
else
{
elog(ERROR, "unrecognized strategy number: %d", strategy);
}
PG_RETURN_BOOL(res);
}