*** a/contrib/pg_trgm/pg_trgm--1.2.sql
--- b/contrib/pg_trgm/pg_trgm--1.2.sql
***************
*** 3,13 ****
--- 3,15 ----
  -- complain if script is sourced in psql, rather than via CREATE EXTENSION
  \echo Use "CREATE EXTENSION pg_trgm" to load this file. \quit
  
+ -- Deprecated function
  CREATE FUNCTION set_limit(float4)
  RETURNS float4
  AS 'MODULE_PATHNAME'
  LANGUAGE C STRICT VOLATILE;
  
+ -- Deprecated function
  CREATE FUNCTION show_limit()
  RETURNS float4
  AS 'MODULE_PATHNAME'
***************
*** 26,32 **** LANGUAGE C STRICT IMMUTABLE;
  CREATE FUNCTION similarity_op(text,text)
  RETURNS bool
  AS 'MODULE_PATHNAME'
! LANGUAGE C STRICT STABLE;  -- stable because depends on trgm_limit
  
  CREATE OPERATOR % (
          LEFTARG = text,
--- 28,34 ----
  CREATE FUNCTION similarity_op(text,text)
  RETURNS bool
  AS 'MODULE_PATHNAME'
! LANGUAGE C STRICT STABLE;  -- stable because depends on pg_trgm.sml_limit
  
  CREATE OPERATOR % (
          LEFTARG = text,
*** a/contrib/pg_trgm/trgm.h
--- b/contrib/pg_trgm/trgm.h
***************
*** 105,111 **** typedef char *BITVECP;
  
  typedef struct TrgmPackedGraph TrgmPackedGraph;
  
! extern float4 trgm_limit;
  
  extern uint32 trgm2int(trgm *ptr);
  extern void compact_trigram(trgm *tptr, char *str, int bytelen);
--- 105,111 ----
  
  typedef struct TrgmPackedGraph TrgmPackedGraph;
  
! extern double trgm_sml_limit;
  
  extern uint32 trgm2int(trgm *ptr);
  extern void compact_trigram(trgm *tptr, char *str, int bytelen);
*** a/contrib/pg_trgm/trgm_gin.c
--- b/contrib/pg_trgm/trgm_gin.c
***************
*** 206,212 **** gin_trgm_consistent(PG_FUNCTION_ARGS)
  			 * similarity is just c / len1.
  			 * So, independly on DIVUNION the upper bound formula is the same.
  			 */
! 			res = (nkeys == 0) ? false : ((((((float4) ntrue) / ((float4) nkeys))) >= trgm_limit) ? true : false);
  			break;
  		case ILikeStrategyNumber:
  #ifndef IGNORECASE
--- 206,213 ----
  			 * similarity is just c / len1.
  			 * So, independly on DIVUNION the upper bound formula is the same.
  			 */
! 			res = (nkeys == 0) ? false :
! 				((((((float4) ntrue) / ((float4) nkeys))) >= trgm_sml_limit) ? true : false);
  			break;
  		case ILikeStrategyNumber:
  #ifndef IGNORECASE
***************
*** 283,289 **** gin_trgm_triconsistent(PG_FUNCTION_ARGS)
  			/*
  			 * See comment in gin_trgm_consistent() about * upper bound formula
  			 */
! 			res = (nkeys == 0) ? GIN_FALSE : (((((float4) ntrue) / ((float4) nkeys)) >= trgm_limit) ? GIN_MAYBE : GIN_FALSE);
  			break;
  		case ILikeStrategyNumber:
  #ifndef IGNORECASE
--- 284,291 ----
  			/*
  			 * See comment in gin_trgm_consistent() about * upper bound formula
  			 */
! 			res = (nkeys == 0) ? GIN_FALSE :
! 				(((((float4) ntrue) / ((float4) nkeys)) >= trgm_sml_limit) ? GIN_MAYBE : GIN_FALSE);
  			break;
  		case ILikeStrategyNumber:
  #ifndef IGNORECASE
*** a/contrib/pg_trgm/trgm_gist.c
--- b/contrib/pg_trgm/trgm_gist.c
***************
*** 294,300 **** gtrgm_consistent(PG_FUNCTION_ARGS)
  				float4		tmpsml = cnt_sml(key, qtrg);
  
  				/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
! 				res = (*(int *) &tmpsml == *(int *) &trgm_limit || tmpsml > trgm_limit) ? true : false;
  			}
  			else if (ISALLTRUE(key))
  			{					/* non-leaf contains signature */
--- 294,301 ----
  				float4		tmpsml = cnt_sml(key, qtrg);
  
  				/* strange bug at freebsd 5.2.1 and gcc 3.3.3 */
! 				res = (*(int *) &tmpsml == *(int *) &trgm_sml_limit
! 						|| tmpsml > trgm_sml_limit) ? true : false;
  			}
  			else if (ISALLTRUE(key))
  			{					/* non-leaf contains signature */
***************
*** 308,314 **** gtrgm_consistent(PG_FUNCTION_ARGS)
  				if (len == 0)
  					res = false;
  				else
! 					res = (((((float8) count) / ((float8) len))) >= trgm_limit) ? true : false;
  			}
  			break;
  		case ILikeStrategyNumber:
--- 309,315 ----
  				if (len == 0)
  					res = false;
  				else
! 					res = (((((float8) count) / ((float8) len))) >= trgm_sml_limit) ? true : false;
  			}
  			break;
  		case ILikeStrategyNumber:
*** a/contrib/pg_trgm/trgm_op.c
--- b/contrib/pg_trgm/trgm_op.c
***************
*** 14,20 ****
  
  PG_MODULE_MAGIC;
  
! float4		trgm_limit = 0.3f;
  
  PG_FUNCTION_INFO_V1(set_limit);
  PG_FUNCTION_INFO_V1(show_limit);
--- 14,23 ----
  
  PG_MODULE_MAGIC;
  
! /* GUC variables */
! double		trgm_sml_limit = 0.3f;
! 
! void		_PG_init(void);
  
  PG_FUNCTION_INFO_V1(set_limit);
  PG_FUNCTION_INFO_V1(show_limit);
***************
*** 23,44 **** PG_FUNCTION_INFO_V1(similarity);
  PG_FUNCTION_INFO_V1(similarity_dist);
  PG_FUNCTION_INFO_V1(similarity_op);
  
  
  Datum
  set_limit(PG_FUNCTION_ARGS)
  {
  	float4		nlimit = PG_GETARG_FLOAT4(0);
  
  	if (nlimit < 0 || nlimit > 1.0)
! 		elog(ERROR, "wrong limit, should be between 0 and 1");
! 	trgm_limit = nlimit;
! 	PG_RETURN_FLOAT4(trgm_limit);
  }
  
  Datum
  show_limit(PG_FUNCTION_ARGS)
  {
! 	PG_RETURN_FLOAT4(trgm_limit);
  }
  
  static int
--- 26,77 ----
  PG_FUNCTION_INFO_V1(similarity_dist);
  PG_FUNCTION_INFO_V1(similarity_op);
  
+ /*
+  * Module load callback
+  */
+ void
+ _PG_init(void)
+ {
+ 	/* Define custom GUC variables. */
+ 	DefineCustomRealVariable("pg_trgm.sml_limit",
+ 							"Sets the threshold used by the %% operator.",
+ 							"Valid range is 0.0 .. 1.0.",
+ 							&trgm_sml_limit,
+ 							0.3,
+ 							0.0,
+ 							1.0,
+ 							PGC_USERSET,
+ 							0,
+ 							NULL,
+ 							NULL,
+ 							NULL);
+ }
  
+ /*
+  * Deprecated function.
+  * Use "pg_trgm.sml_limit" GUC variable instead of this function
+  */
  Datum
  set_limit(PG_FUNCTION_ARGS)
  {
  	float4		nlimit = PG_GETARG_FLOAT4(0);
  
  	if (nlimit < 0 || nlimit > 1.0)
! 		ereport(ERROR,
! 				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
! 				 errmsg("wrong limit, should be between 0 and 1")));
! 	trgm_sml_limit = nlimit;
! 	PG_RETURN_FLOAT4(trgm_sml_limit);
  }
  
+ /*
+  * Deprecated function.
+  * Use "pg_trgm.sml_limit" GUC variable instead of this function
+  */
  Datum
  show_limit(PG_FUNCTION_ARGS)
  {
! 	PG_RETURN_FLOAT4(trgm_sml_limit);
  }
  
  static int
***************
*** 720,724 **** similarity_op(PG_FUNCTION_ARGS)
  														 PG_GETARG_DATUM(0),
  														 PG_GETARG_DATUM(1)));
  
! 	PG_RETURN_BOOL(res >= trgm_limit);
  }
--- 753,757 ----
  														 PG_GETARG_DATUM(0),
  														 PG_GETARG_DATUM(1)));
  
! 	PG_RETURN_BOOL(res >= trgm_sml_limit);
  }
*** a/doc/src/sgml/pgtrgm.sgml
--- b/doc/src/sgml/pgtrgm.sgml
***************
*** 99,105 ****
         Returns the current similarity threshold used by the <literal>%</>
         operator.  This sets the minimum similarity between
         two words for them to be considered similar enough to
!        be misspellings of each other, for example.
        </entry>
       </row>
       <row>
--- 99,106 ----
         Returns the current similarity threshold used by the <literal>%</>
         operator.  This sets the minimum similarity between
         two words for them to be considered similar enough to
!        be misspellings of each other, for example
!        (<emphasis>deprecated</emphasis>).
        </entry>
       </row>
       <row>
***************
*** 108,114 ****
        <entry>
         Sets the current similarity threshold that is used by the <literal>%</>
         operator.  The threshold must be between 0 and 1 (default is 0.3).
!        Returns the same value passed in.
        </entry>
       </row>
      </tbody>
--- 109,115 ----
        <entry>
         Sets the current similarity threshold that is used by the <literal>%</>
         operator.  The threshold must be between 0 and 1 (default is 0.3).
!        Returns the same value passed in (<emphasis>deprecated</emphasis>).
        </entry>
       </row>
      </tbody>
***************
*** 133,139 ****
        <entry>
         Returns <literal>true</> if its arguments have a similarity that is
         greater than the current similarity threshold set by
!        <function>set_limit</>.
        </entry>
       </row>
       <row>
--- 134,140 ----
        <entry>
         Returns <literal>true</> if its arguments have a similarity that is
         greater than the current similarity threshold set by
!        <varname>pg_trgm.sml_limit</>.
        </entry>
       </row>
       <row>
***************
*** 150,155 ****
--- 151,177 ----
   </sect2>
  
   <sect2>
+   <title>GUC Parameters</title>
+ 
+   <variablelist>
+    <varlistentry id="guc-pgtrgm-sml-limit" xreflabel="pg_trgm.sml_limit">
+     <term>
+      <varname>pg_trgm.sml_limit</> (<type>real</type>)
+      <indexterm>
+       <primary><varname>pg_trgm.sml_limit</> configuration parameter</primary>
+      </indexterm>
+     </term>
+     <listitem>
+      <para>
+       Sets the current similarity threshold that is used by the <literal>%</>
+       operator.  The threshold must be between 0 and 1 (default is 0.3).
+      </para>
+     </listitem>
+    </varlistentry>
+   </variablelist>
+  </sect2>
+ 
+  <sect2>
    <title>Index Support</title>
  
    <para>
