diff --git a/contrib/pg_trgm/trgm_regexp.c b/contrib/pg_trgm/trgm_regexp.c new file mode 100644 index 772fc44..2632a03 *** a/contrib/pg_trgm/trgm_regexp.c --- b/contrib/pg_trgm/trgm_regexp.c *************** *** 203,210 **** */ #define MAX_EXPANDED_STATES 128 #define MAX_EXPANDED_ARCS 1024 ! #define MAX_TRGM_COUNT 256 #define COLOR_COUNT_LIMIT 256 /* Struct representing a single pg_wchar, converted back to multibyte form */ typedef struct --- 203,212 ---- */ #define MAX_EXPANDED_STATES 128 #define MAX_EXPANDED_ARCS 1024 ! #define MAX_TRGM_SIZE 256 ! #define WISH_TRGM_SIZE 16 #define COLOR_COUNT_LIMIT 256 + #define BLANK_COLOR_SIZE 32 /* Struct representing a single pg_wchar, converted back to multibyte form */ typedef struct *************** static void fillTrgm(trgm *ptrgm, trgm_m *** 460,465 **** --- 462,468 ---- static void mergeStates(TrgmState *state1, TrgmState *state2); static int colorTrgmInfoCmp(const void *p1, const void *p2); static int colorTrgmInfoCountCmp(const void *p1, const void *p2); + static int getColorTrigramSize(const ColorTrgmInfo *colorTrgm); static TrgmPackedGraph *packGraph(TrgmNFA *trgmNFA, MemoryContext rcontext); static int packArcInfoCmp(const void *a1, const void *a2); *************** selectColorTrigrams(TrgmNFA *trgmNFA) *** 1423,1429 **** i; TrgmState *state; ColorTrgmInfo *colorTrgms; ! int64 totalTrgmCount; int number; /* Collect color trigrams from all arcs */ --- 1426,1432 ---- i; TrgmState *state; ColorTrgmInfo *colorTrgms; ! int64 totalTrgmSize; int number; /* Collect color trigrams from all arcs */ *************** selectColorTrigrams(TrgmNFA *trgmNFA) *** 1489,1495 **** * 1290. However, the grand total totalTrgmCount might conceivably * overflow an int, so we use int64 for that within this routine. */ ! totalTrgmCount = 0; for (i = 0; i < trgmNFA->colorTrgmsCount; i++) { ColorTrgmInfo *trgmInfo = &colorTrgms[i]; --- 1492,1498 ---- * 1290. However, the grand total totalTrgmCount might conceivably * overflow an int, so we use int64 for that within this routine. */ ! totalTrgmSize = 0; for (i = 0; i < trgmNFA->colorTrgmsCount; i++) { ColorTrgmInfo *trgmInfo = &colorTrgms[i]; *************** selectColorTrigrams(TrgmNFA *trgmNFA) *** 1504,1510 **** count *= trgmNFA->colorInfo[c].wordCharsCount; } trgmInfo->count = count; ! totalTrgmCount += count; } /* Sort color trigrams in descending order of simple trigram counts */ --- 1507,1513 ---- count *= trgmNFA->colorInfo[c].wordCharsCount; } trgmInfo->count = count; ! totalTrgmSize += getColorTrigramSize(trgmInfo); } /* Sort color trigrams in descending order of simple trigram counts */ *************** selectColorTrigrams(TrgmNFA *trgmNFA) *** 1522,1528 **** * cannot always remove the trigram we'd prefer to. */ for (i = 0; ! (i < trgmNFA->colorTrgmsCount) && (totalTrgmCount > MAX_TRGM_COUNT); i++) { ColorTrgmInfo *trgmInfo = &colorTrgms[i]; --- 1525,1531 ---- * cannot always remove the trigram we'd prefer to. */ for (i = 0; ! (i < trgmNFA->colorTrgmsCount) && (totalTrgmSize > WISH_TRGM_SIZE); i++) { ColorTrgmInfo *trgmInfo = &colorTrgms[i]; *************** selectColorTrigrams(TrgmNFA *trgmNFA) *** 1572,1585 **** /* Mark trigram unexpanded, and update totalTrgmCount */ trgmInfo->expanded = false; ! totalTrgmCount -= trgmInfo->count; } /* Did we succeed in fitting into MAX_TRGM_COUNT? */ ! if (totalTrgmCount > MAX_TRGM_COUNT) return false; ! trgmNFA->totalTrgmCount = (int) totalTrgmCount; /* * Sort color trigrams by colors (will be useful for bsearch in packGraph) --- 1575,1588 ---- /* Mark trigram unexpanded, and update totalTrgmCount */ trgmInfo->expanded = false; ! totalTrgmSize -= getColorTrigramSize(trgmInfo); } /* Did we succeed in fitting into MAX_TRGM_COUNT? */ ! if (totalTrgmSize > MAX_TRGM_SIZE) return false; ! trgmNFA->totalTrgmCount = (int) totalTrgmSize; /* * Sort color trigrams by colors (will be useful for bsearch in packGraph) *************** colorTrgmInfoCmp(const void *p1, const v *** 1751,1767 **** static int colorTrgmInfoCountCmp(const void *p1, const void *p2) { ! const ColorTrgmInfo *c1 = (const ColorTrgmInfo *) p1; ! const ColorTrgmInfo *c2 = (const ColorTrgmInfo *) p2; ! if (c1->count < c2->count) return 1; ! else if (c1->count == c2->count) return 0; else return -1; } /*--------------------- * Subroutines for packing the graph into final representation (stage 4). --- 1754,1783 ---- static int colorTrgmInfoCountCmp(const void *p1, const void *p2) { ! int count1 = getColorTrigramSize((const ColorTrgmInfo *) p1); ! int count2 = getColorTrigramSize((const ColorTrgmInfo *) p2); ! if (count1 < count2) return 1; ! else if (count1 == count2) return 0; else return -1; } + static int + getColorTrigramSize(const ColorTrgmInfo *colorTrgm) + { + int count = colorTrgm->count, i; + + for (i = 0; i < 3; i++) + { + if (colorTrgm->ctrgm.colors[i] == COLOR_BLANK) + count *= BLANK_COLOR_SIZE; + } + return count; + } + /*--------------------- * Subroutines for packing the graph into final representation (stage 4).