Skip to content

Commit 2d6599f

Browse files
committed
Add caching of query to GIN/GiST consistent function.
Per performance gripe from nomao.com
1 parent e3afbb3 commit 2d6599f

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

contrib/pg_trgm/trgm_gin.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/pg_trgm/trgm_gin.c,v 1.4 2008/05/17 01:28:21 adunstan Exp $
2+
* $PostgreSQL: pgsql/contrib/pg_trgm/trgm_gin.c,v 1.5 2008/07/11 11:56:48 teodor Exp $
33
*/
44
#include "trgm.h"
55

@@ -52,6 +52,16 @@ gin_extract_trgm(PG_FUNCTION_ARGS)
5252
PG_RETURN_POINTER(entries);
5353
}
5454

55+
/*
56+
* Per call strage for consistent functions to
57+
* cache computed value from query
58+
*/
59+
typedef struct PerCallConsistentStorage {
60+
int trglen;
61+
text data[1]; /* query */
62+
} PerCallConsistentStorage;
63+
#define PCCSHDR_SZ offsetof(PerCallConsistentStorage, data)
64+
5565
Datum
5666
gin_trgm_consistent(PG_FUNCTION_ARGS)
5767
{
@@ -60,16 +70,30 @@ gin_trgm_consistent(PG_FUNCTION_ARGS)
6070
text *query = PG_GETARG_TEXT_P(2);
6171
bool *recheck = (bool *) PG_GETARG_POINTER(3);
6272
bool res = FALSE;
63-
TRGM *trg;
6473
int4 i,
6574
trglen,
6675
ntrue = 0;
76+
PerCallConsistentStorage *pccs = (PerCallConsistentStorage*) fcinfo->flinfo->fn_extra;
6777

6878
/* All cases served by this function are inexact */
6979
*recheck = true;
7080

71-
trg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
72-
trglen = ARRNELEM(trg);
81+
if ( pccs == NULL || VARSIZE(pccs->data) != VARSIZE(query) || memcmp( pccs->data, query, VARSIZE(query) ) !=0 )
82+
{
83+
TRGM *trg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
84+
85+
if ( pccs )
86+
pfree(pccs);
87+
88+
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
89+
VARSIZE(query) + PCCSHDR_SZ);
90+
pccs = (PerCallConsistentStorage*) fcinfo->flinfo->fn_extra;
91+
92+
pccs->trglen = ARRNELEM(trg);
93+
memcpy( pccs->data, query, VARSIZE(query) );
94+
}
95+
96+
trglen = pccs->trglen;
7397

7498
for (i = 0; i < trglen; i++)
7599
if (check[i])

contrib/pg_trgm/trgm_gist.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/pg_trgm/trgm_gist.c,v 1.14 2008/05/17 01:28:21 adunstan Exp $
2+
* $PostgreSQL: pgsql/contrib/pg_trgm/trgm_gist.c,v 1.15 2008/07/11 11:56:48 teodor Exp $
33
*/
44
#include "trgm.h"
55

@@ -168,12 +168,30 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
168168
/* Oid subtype = PG_GETARG_OID(3); */
169169
bool *recheck = (bool *) PG_GETARG_POINTER(4);
170170
TRGM *key = (TRGM *) DatumGetPointer(entry->key);
171-
TRGM *qtrg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
171+
TRGM *qtrg;
172172
bool res = false;
173+
char *cache = (char*) fcinfo->flinfo->fn_extra;
173174

174175
/* All cases served by this function are exact */
175176
*recheck = false;
176177

178+
if ( cache == NULL || VARSIZE(cache) != VARSIZE(query) || memcmp( cache, query, VARSIZE(query) ) !=0 )
179+
{
180+
qtrg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
181+
182+
if (cache)
183+
pfree(cache);
184+
185+
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
186+
MAXALIGN(VARSIZE(query)) + VARSIZE(qtrg) );
187+
cache = (char*) fcinfo->flinfo->fn_extra;
188+
189+
memcpy( cache, query, VARSIZE(query) );
190+
memcpy( cache + MAXALIGN(VARSIZE(query)), qtrg, VARSIZE(qtrg) );
191+
}
192+
193+
qtrg = (TRGM*)( cache + MAXALIGN(VARSIZE(query)) );
194+
177195
if (GIST_LEAF(entry))
178196
{ /* all leafs contains orig trgm */
179197
float4 tmpsml = cnt_sml(key, qtrg);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy