Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 25bfa7e

Browse files
committed
Improve the gin index scan performance in pg_trgm.
Previous coding assumes too pessimistic upper bound of similarity in consistent method of GIN. Author: Fornaroli Christophe with comments by me.
1 parent a9246fb commit 25bfa7e

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

contrib/pg_trgm/trgm_gin.c

+20-8
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,23 @@ gin_trgm_consistent(PG_FUNCTION_ARGS)
190190
if (check[i])
191191
ntrue++;
192192
}
193-
#ifdef DIVUNION
194-
res = (nkeys == ntrue) ? true : ((((((float4) ntrue) / ((float4) (nkeys - ntrue)))) >= trgm_limit) ? true : false);
195-
#else
193+
194+
/*--------------------
195+
* If DIVUNION is defined then similarity formula is:
196+
* c / (len1 + len2 - c)
197+
* where c is number of common trigrams and it stands as ntrue in
198+
* this code. Here we don't know value of len2 but we can assume
199+
* that c (ntrue) is a lower bound of len2, so upper bound of
200+
* similarity is:
201+
* c / (len1 + c - c) => c / len1
202+
* If DIVUNION is not defined then similarity formula is:
203+
* c / max(len1, len2)
204+
* And again, c (ntrue) is a lower bound of len2, but c <= len1
205+
* just by definition and, consequently, upper bound of
206+
* similarity is just c / len1.
207+
* So, independly on DIVUNION the upper bound formula is the same.
208+
*/
196209
res = (nkeys == 0) ? false : ((((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit) ? true : false);
197-
#endif
198210
break;
199211
case ILikeStrategyNumber:
200212
#ifndef IGNORECASE
@@ -267,11 +279,11 @@ gin_trgm_triconsistent(PG_FUNCTION_ARGS)
267279
if (check[i] != GIN_FALSE)
268280
ntrue++;
269281
}
270-
#ifdef DIVUNION
271-
res = (nkeys == ntrue) ? GIN_MAYBE : (((((float4) ntrue) / ((float4) (nkeys - ntrue))) >= trgm_limit) ? GIN_MAYBE : GIN_FALSE);
272-
#else
282+
283+
/*
284+
* See comment in gin_trgm_consistent() about * upper bound formula
285+
*/
273286
res = (nkeys == 0) ? GIN_FALSE : (((((float4) ntrue) / ((float4) nkeys)) >= trgm_limit) ? GIN_MAYBE : GIN_FALSE);
274-
#endif
275287
break;
276288
case ILikeStrategyNumber:
277289
#ifndef IGNORECASE

0 commit comments

Comments
 (0)