Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2d6599f

Browse files
committed
Add caching of query to GIN/GiST consistent function.
Per performance gripe from nomao.com
1 parente3afbb3 commit2d6599f

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+
typedefstructPerCallConsistentStorage {
60+
inttrglen;
61+
textdata[1];/* query */
62+
}PerCallConsistentStorage;
63+
#definePCCSHDR_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
boolres= FALSE;
63-
TRGM*trg;
6473
int4i,
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
/* Oidsubtype = 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
boolres= 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
float4tmpsml=cnt_sml(key,qtrg);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp