/* *Thisfunctioncanonlybecalledifapre-9.1versionoftheGINoperator *classdefinitionispresentinthecatalogs(probablyasaconsequence *ofupgrade-in-place).Cope.
*/
Datum
gin_extract_trgm(PG_FUNCTION_ARGS)
{ if (PG_NARGS() == 3) return gin_extract_value_trgm(fcinfo); if (PG_NARGS() == 7) return gin_extract_query_trgm(fcinfo);
elog(ERROR, "unexpected number of arguments to gin_extract_trgm");
PG_RETURN_NULL();
}
Datum
gin_extract_value_trgm(PG_FUNCTION_ARGS)
{
text *val = (text *) PG_GETARG_TEXT_PP(0);
int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
Datum *entries = NULL;
TRGM *trg;
int32 trglen;
*nentries = 0;
trg = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val));
trglen = ARRNELEM(trg);
switch (strategy)
{ case SimilarityStrategyNumber: case WordSimilarityStrategyNumber: case StrictWordSimilarityStrategyNumber: case EqualStrategyNumber:
trg = generate_trgm(VARDATA_ANY(val), VARSIZE_ANY_EXHDR(val)); break; case ILikeStrategyNumber: #ifndef IGNORECASE
elog(ERROR, "cannot handle ~~* with case-sensitive trigrams"); #endif /* FALL THRU */ case LikeStrategyNumber:
/* *Forwildcardsearchweextractallthetrigramsthatevery *potentially-matchingstringmustinclude.
*/
trg = generate_wildcard_trgm(VARDATA_ANY(val),
VARSIZE_ANY_EXHDR(val)); break; case RegExpICaseStrategyNumber: #ifndef IGNORECASE
elog(ERROR, "cannot handle ~* with case-sensitive trigrams"); #endif /* FALL THRU */ case RegExpStrategyNumber:
trg = createTrgmNFA(val, PG_GET_COLLATION(),
&graph, CurrentMemoryContext); if (trg && ARRNELEM(trg) > 0)
{ /* *Successfulregexprocessing:storeNFA-likegraphas *extra_data.GINAPIrequiresanarrayofnentries *Pointers,butwejustputthesamevalueineachelement.
*/
trglen = ARRNELEM(trg);
*extra_data = (Pointer *) palloc(sizeof(Pointer) * trglen); for (i = 0; i < trglen; i++)
(*extra_data)[i] = (Pointer) graph;
} else
{ /* No result: have to do full index scan. */
*nentries = 0;
*searchMode = GIN_SEARCH_MODE_ALL;
PG_RETURN_POINTER(entries);
} break; default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
trg = NULL; /* keep compiler quiet */ break;
}
trglen = ARRNELEM(trg);
*nentries = trglen;
if (trglen > 0)
{
entries = (Datum *) palloc(sizeof(Datum) * trglen);
ptr = GETARR(trg); for (i = 0; i < trglen; i++)
{
int32 item = trgm2int(ptr);
entries[i] = Int32GetDatum(item);
ptr++;
}
}
/* *Ifnotrigramwasextractedthenwehavetoscanalltheindex.
*/ if (trglen == 0)
*searchMode = GIN_SEARCH_MODE_ALL;
/* All cases served by this function are inexact */
*recheck = true;
switch (strategy)
{ case SimilarityStrategyNumber: case WordSimilarityStrategyNumber: case StrictWordSimilarityStrategyNumber:
nlimit = index_strategy_get_limit(strategy);
/* Count the matches */
ntrue = 0; for (i = 0; i < nkeys; i++)
{ if (check[i])
ntrue++;
}
/*-------------------- *IfDIVUNIONisdefinedthensimilarityformulais: *c/(len1+len2-c) *wherecisnumberofcommontrigramsanditstandsasntruein *thiscode.Herewedon'tknowvalueoflen2butwecanassume *thatc(ntrue)isalowerboundoflen2,soupperboundof *similarityis: *c/(len1+c-c)=>c/len1 *IfDIVUNIONisnotdefinedthensimilarityformulais: *c/max(len1,len2) *Andagain,c(ntrue)isalowerboundoflen2,butc<=len1 *justbydefinitionand,consequently,upperboundof *similarityisjustc/len1. *So,independentlyonDIVUNIONtheupperboundformulaisthesame.
*/
res = (nkeys == 0) ? false :
(((((float4) ntrue) / ((float4) nkeys))) >= nlimit); break; case ILikeStrategyNumber: #ifndef IGNORECASE
elog(ERROR, "cannot handle ~~* with case-sensitive trigrams"); #endif /* FALL THRU */ case LikeStrategyNumber: case EqualStrategyNumber: /* Check if all extracted trigrams are presented. */
res = true; for (i = 0; i < nkeys; i++)
{ if (!check[i])
{
res = false; break;
}
} break; case RegExpICaseStrategyNumber: #ifndef IGNORECASE
elog(ERROR, "cannot handle ~* with case-sensitive trigrams"); #endif /* FALL THRU */ case RegExpStrategyNumber: if (nkeys < 1)
{ /* Regex processing gave no result: do full index scan */
res = true;
} else
res = trigramsMatchGraph((TrgmPackedGraph *) extra_data[0],
check); break; default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
res = false; /* keep compiler quiet */ break;
}
/* text *query = PG_GETARG_TEXT_PP(2); */
int32 nkeys = PG_GETARG_INT32(3);
Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
GinTernaryValue res = GIN_MAYBE;
int32 i,
ntrue; bool *boolcheck; double nlimit;
switch (strategy)
{ case SimilarityStrategyNumber: case WordSimilarityStrategyNumber: case StrictWordSimilarityStrategyNumber:
nlimit = index_strategy_get_limit(strategy);
/* Count the matches */
ntrue = 0; for (i = 0; i < nkeys; i++)
{ if (check[i] != GIN_FALSE)
ntrue++;
}
/* *Seecommentingin_trgm_consistent()about*upperbound *formula
*/
res = (nkeys == 0)
? GIN_FALSE : (((((float4) ntrue) / ((float4) nkeys)) >= nlimit)
? GIN_MAYBE : GIN_FALSE); break; case ILikeStrategyNumber: #ifndef IGNORECASE
elog(ERROR, "cannot handle ~~* with case-sensitive trigrams"); #endif /* FALL THRU */ case LikeStrategyNumber: case EqualStrategyNumber: /* Check if all extracted trigrams are presented. */
res = GIN_MAYBE; for (i = 0; i < nkeys; i++)
{ if (check[i] == GIN_FALSE)
{
res = GIN_FALSE; break;
}
} break; case RegExpICaseStrategyNumber: #ifndef IGNORECASE
elog(ERROR, "cannot handle ~* with case-sensitive trigrams"); #endif /* FALL THRU */ case RegExpStrategyNumber: if (nkeys < 1)
{ /* Regex processing gave no result: do full index scan */
res = GIN_MAYBE;
} else
{ /* *AstrigramsMatchGraphimplementsamonotonicboolean *function,promotingallGIN_MAYBEkeystoGIN_TRUEwill *giveaconservativeresult.
*/
boolcheck = (bool *) palloc(sizeof(bool) * nkeys); for (i = 0; i < nkeys; i++)
boolcheck[i] = (check[i] != GIN_FALSE); if (!trigramsMatchGraph((TrgmPackedGraph *) extra_data[0],
boolcheck))
res = GIN_FALSE;
pfree(boolcheck);
} break; default:
elog(ERROR, "unrecognized strategy number: %d", strategy);
res = GIN_FALSE; /* keep compiler quiet */ break;
}
/* All cases served by this function are inexact */
Assert(res != GIN_TRUE);
PG_RETURN_GIN_TERNARY_VALUE(res);
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.16 Sekunden
(vorverarbeitet am 2026-08-04)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.