Fix copy-pasto in the patch to allow background writer to run during
[PostgreSQL.git] / contrib / pg_trgm / trgm_gin.c
blob92ece92338bd9e1d5a815ec13286724097e4ad2a
1 /*
2 * $PostgreSQL$
3 */
4 #include "trgm.h"
6 #include "access/gin.h"
7 #include "access/itup.h"
8 #include "access/tuptoaster.h"
9 #include "storage/bufpage.h"
10 #include "utils/array.h"
11 #include "utils/builtins.h"
13 PG_FUNCTION_INFO_V1(gin_extract_trgm);
14 Datum gin_extract_trgm(PG_FUNCTION_ARGS);
16 PG_FUNCTION_INFO_V1(gin_trgm_consistent);
17 Datum gin_trgm_consistent(PG_FUNCTION_ARGS);
19 Datum
20 gin_extract_trgm(PG_FUNCTION_ARGS)
22 text *val = (text *) PG_GETARG_TEXT_P(0);
23 int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
24 Datum *entries = NULL;
25 TRGM *trg;
26 int4 trglen;
28 *nentries = 0;
30 trg = generate_trgm(VARDATA(val), VARSIZE(val) - VARHDRSZ);
31 trglen = ARRNELEM(trg);
33 if (trglen > 0)
35 trgm *ptr;
36 int4 i = 0,
37 item;
39 *nentries = (int32) trglen;
40 entries = (Datum *) palloc(sizeof(Datum) * trglen);
42 ptr = GETARR(trg);
43 while (ptr - GETARR(trg) < ARRNELEM(trg))
45 item = trgm2int(ptr);
46 entries[i++] = Int32GetDatum(item);
48 ptr++;
52 PG_RETURN_POINTER(entries);
56 * Per call strage for consistent functions to
57 * cache computed value from query
59 typedef struct PerCallConsistentStorage {
60 int trglen;
61 text data[1]; /* query */
62 } PerCallConsistentStorage;
63 #define PCCSHDR_SZ offsetof(PerCallConsistentStorage, data)
65 Datum
66 gin_trgm_consistent(PG_FUNCTION_ARGS)
68 bool *check = (bool *) PG_GETARG_POINTER(0);
69 /* StrategyNumber strategy = PG_GETARG_UINT16(1); */
70 text *query = PG_GETARG_TEXT_P(2);
71 bool *recheck = (bool *) PG_GETARG_POINTER(3);
72 bool res = FALSE;
73 int4 i,
74 trglen,
75 ntrue = 0;
76 PerCallConsistentStorage *pccs = (PerCallConsistentStorage*) fcinfo->flinfo->fn_extra;
78 /* All cases served by this function are inexact */
79 *recheck = true;
81 if ( pccs == NULL || VARSIZE(pccs->data) != VARSIZE(query) || memcmp( pccs->data, query, VARSIZE(query) ) !=0 )
83 TRGM *trg = generate_trgm(VARDATA(query), VARSIZE(query) - VARHDRSZ);
85 if ( pccs )
86 pfree(pccs);
88 fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
89 VARSIZE(query) + PCCSHDR_SZ);
90 pccs = (PerCallConsistentStorage*) fcinfo->flinfo->fn_extra;
92 pccs->trglen = ARRNELEM(trg);
93 memcpy( pccs->data, query, VARSIZE(query) );
96 trglen = pccs->trglen;
98 for (i = 0; i < trglen; i++)
99 if (check[i])
100 ntrue++;
102 #ifdef DIVUNION
103 res = (trglen == ntrue) ? true : ((((((float4) ntrue) / ((float4) (trglen - ntrue)))) >= trgm_limit) ? true : false);
104 #else
105 res = (trglen == 0) ? false : ((((((float4) ntrue) / ((float4) trglen))) >= trgm_limit) ? true : false);
106 #endif
108 PG_RETURN_BOOL(res);