Skip to content

Commit 5871b88

Browse files
committed
GUC variable pg_trgm.similarity_threshold insead of set_limit()
Use GUC variable pg_trgm.similarity_threshold insead of set_limit()/show_limit() which was introduced when defining GUC varuables by modules was absent. Author: Artur Zakirov
1 parent f9e5ed6 commit 5871b88

File tree

6 files changed

+78
-15
lines changed

6 files changed

+78
-15
lines changed

contrib/pg_trgm/pg_trgm--1.2.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
44
\echo Use "CREATE EXTENSION pg_trgm" to load this file. \quit
55

6+
-- Deprecated function
67
CREATE FUNCTION set_limit(float4)
78
RETURNS float4
89
AS 'MODULE_PATHNAME'
910
LANGUAGE C STRICT VOLATILE;
1011

12+
-- Deprecated function
1113
CREATE FUNCTION show_limit()
1214
RETURNS float4
1315
AS 'MODULE_PATHNAME'
@@ -26,7 +28,7 @@ LANGUAGE C STRICT IMMUTABLE;
2628
CREATE FUNCTION similarity_op(text,text)
2729
RETURNS bool
2830
AS 'MODULE_PATHNAME'
29-
LANGUAGE C STRICT STABLE; -- stable because depends on trgm_limit
31+
LANGUAGE C STRICT STABLE; -- stable because depends on pg_trgm.similarity_threshold
3032

3133
CREATE OPERATOR % (
3234
LEFTARG = text,

contrib/pg_trgm/trgm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ typedef char *BITVECP;
105105

106106
typedef struct TrgmPackedGraph TrgmPackedGraph;
107107

108-
extern float4 trgm_limit;
108+
extern double similarity_threshold;
109109

110110
extern uint32 trgm2int(trgm *ptr);
111111
extern void compact_trigram(trgm *tptr, char *str, int bytelen);

contrib/pg_trgm/trgm_gin.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ gin_trgm_consistent(PG_FUNCTION_ARGS)
206206
* similarity is just c / len1.
207207
* So, independly on DIVUNION the upper bound formula is the same.
208208
*/
209-
res = (nkeys == 0) ? false : ((((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit) ? true : false);
209+
res = (nkeys == 0) ? false :
210+
((((((float4) ntrue) / ((float4) nkeys))) >= similarity_threshold)
211+
? true : false);
210212
break;
211213
case ILikeStrategyNumber:
212214
#ifndef IGNORECASE
@@ -283,7 +285,9 @@ gin_trgm_triconsistent(PG_FUNCTION_ARGS)
283285
/*
284286
* See comment in gin_trgm_consistent() about * upper bound formula
285287
*/
286-
res = (nkeys == 0) ? GIN_FALSE : (((((float4) ntrue) / ((float4) nkeys)) >= trgm_limit) ? GIN_MAYBE : GIN_FALSE);
288+
res = (nkeys == 0) ? GIN_FALSE :
289+
(((((float4) ntrue) / ((float4) nkeys)) >= similarity_threshold)
290+
? GIN_MAYBE : GIN_FALSE);
287291
break;
288292
case ILikeStrategyNumber:
289293
#ifndef IGNORECASE

contrib/pg_trgm/trgm_gist.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
294294
float4 tmpsml = cnt_sml(key, qtrg);
295295

296296
/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
297-
res = (*(int *) &tmpsml == *(int *) &trgm_limit || tmpsml > trgm_limit) ? true : false;
297+
res = (*(int *) &tmpsml == *(int *) &similarity_threshold
298+
|| tmpsml > similarity_threshold) ? true : false;
298299
}
299300
else if (ISALLTRUE(key))
300301
{ /* non-leaf contains signature */
@@ -308,7 +309,8 @@ gtrgm_consistent(PG_FUNCTION_ARGS)
308309
if (len == 0)
309310
res = false;
310311
else
311-
res = (((((float8) count) / ((float8) len))) >= trgm_limit) ? true : false;
312+
res = (((((float8) count) / ((float8) len))) >= similarity_threshold)
313+
? true : false;
312314
}
313315
break;
314316
case ILikeStrategyNumber:

contrib/pg_trgm/trgm_op.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
PG_MODULE_MAGIC;
1616

17-
float4 trgm_limit = 0.3f;
17+
/* GUC variables */
18+
double similarity_threshold = 0.3f;
19+
20+
void _PG_init(void);
1821

1922
PG_FUNCTION_INFO_V1(set_limit);
2023
PG_FUNCTION_INFO_V1(show_limit);
@@ -23,22 +26,52 @@ PG_FUNCTION_INFO_V1(similarity);
2326
PG_FUNCTION_INFO_V1(similarity_dist);
2427
PG_FUNCTION_INFO_V1(similarity_op);
2528

29+
/*
30+
* Module load callback
31+
*/
32+
void
33+
_PG_init(void)
34+
{
35+
/* Define custom GUC variables. */
36+
DefineCustomRealVariable("pg_trgm.similarity_threshold",
37+
"Sets the threshold used by the %% operator.",
38+
"Valid range is 0.0 .. 1.0.",
39+
&similarity_threshold,
40+
0.3,
41+
0.0,
42+
1.0,
43+
PGC_USERSET,
44+
0,
45+
NULL,
46+
NULL,
47+
NULL);
48+
}
2649

50+
/*
51+
* Deprecated function.
52+
* Use "pg_trgm.similarity_threshold" GUC variable instead of this function
53+
*/
2754
Datum
2855
set_limit(PG_FUNCTION_ARGS)
2956
{
3057
float4 nlimit = PG_GETARG_FLOAT4(0);
3158

3259
if (nlimit < 0 || nlimit > 1.0)
33-
elog(ERROR, "wrong limit, should be between 0 and 1");
34-
trgm_limit = nlimit;
35-
PG_RETURN_FLOAT4(trgm_limit);
60+
ereport(ERROR,
61+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
62+
errmsg("wrong limit, should be between 0 and 1")));
63+
similarity_threshold = nlimit;
64+
PG_RETURN_FLOAT4(similarity_threshold);
3665
}
3766

67+
/*
68+
* Deprecated function.
69+
* Use "pg_trgm.similarity_threshold" GUC variable instead of this function
70+
*/
3871
Datum
3972
show_limit(PG_FUNCTION_ARGS)
4073
{
41-
PG_RETURN_FLOAT4(trgm_limit);
74+
PG_RETURN_FLOAT4(similarity_threshold);
4275
}
4376

4477
static int
@@ -720,5 +753,5 @@ similarity_op(PG_FUNCTION_ARGS)
720753
PG_GETARG_DATUM(0),
721754
PG_GETARG_DATUM(1)));
722755

723-
PG_RETURN_BOOL(res >= trgm_limit);
756+
PG_RETURN_BOOL(res >= similarity_threshold);
724757
}

doc/src/sgml/pgtrgm.sgml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
Returns the current similarity threshold used by the <literal>%</>
100100
operator. This sets the minimum similarity between
101101
two words for them to be considered similar enough to
102-
be misspellings of each other, for example.
102+
be misspellings of each other, for example
103+
(<emphasis>deprecated</emphasis>).
103104
</entry>
104105
</row>
105106
<row>
@@ -108,7 +109,7 @@
108109
<entry>
109110
Sets the current similarity threshold that is used by the <literal>%</>
110111
operator. The threshold must be between 0 and 1 (default is 0.3).
111-
Returns the same value passed in.
112+
Returns the same value passed in (<emphasis>deprecated</emphasis>).
112113
</entry>
113114
</row>
114115
</tbody>
@@ -133,7 +134,7 @@
133134
<entry>
134135
Returns <literal>true</> if its arguments have a similarity that is
135136
greater than the current similarity threshold set by
136-
<function>set_limit</>.
137+
<varname>pg_trgm.similarity_threshold</>.
137138
</entry>
138139
</row>
139140
<row>
@@ -149,6 +150,27 @@
149150
</table>
150151
</sect2>
151152

153+
<sect2>
154+
<title>GUC Parameters</title>
155+
156+
<variablelist>
157+
<varlistentry id="guc-pgtrgm-similarity-threshold" xreflabel="pg_trgm.similarity_threshold">
158+
<term>
159+
<varname>pg_trgm.similarity_threshold</> (<type>real</type>)
160+
<indexterm>
161+
<primary><varname>pg_trgm.similarity_threshold</> configuration parameter</primary>
162+
</indexterm>
163+
</term>
164+
<listitem>
165+
<para>
166+
Sets the current similarity threshold that is used by the <literal>%</>
167+
operator. The threshold must be between 0 and 1 (default is 0.3).
168+
</para>
169+
</listitem>
170+
</varlistentry>
171+
</variablelist>
172+
</sect2>
173+
152174
<sect2>
153175
<title>Index Support</title>
154176

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