set_limit
set_limit() available since pg_trgm PG 8.3+. The equivalent GUC pg_trgm.similarity_threshold was introduced in PG 9.6+ and is preferred in modern deployments.→ realSets the current similarity threshold used by the % operator (default 0.3). Returns the new threshold.
Signature
set_limit ( real ) → realParameters
| Parameter | Type | Description |
|---|---|---|
| threshold | real | Similarity threshold between 0 and 1 |
Examples
SELECT set_limit(0.5);0.5SELECT set_limit(0.1); SELECT name FROM products WHERE name % 'phone';More permissive fuzzy matchingDO $ BEGIN PERFORM set_limit(0.4); END $;
SELECT name FROM brands WHERE name % $1;Threshold set inside a PL/pgSQL block for a focused searchSELECT set_limit(0.3); -- reset to default after custom search0.3`set_limit(0.6)` sets the threshold for the entire session. In connection-pooled applications, a subsequent query from a different request may inherit this elevated threshold, silently returning fewer (or no) fuzzy matches than expected.
✓ Instead: Use the GUC instead: `SET LOCAL pg_trgm.similarity_threshold = 0.6` which automatically resets at transaction end, making it safe in pooled environments.
`set_limit` changes a session-level value. In multi-threaded applications, prefer setting it via a GUC: `SET pg_trgm.similarity_threshold = 0.5`. This is cleaner and persists correctly for the session.
SET pg_trgm.similarity_threshold = 0.4;
SELECT name FROM products WHERE name % $1;Fuzzy search with explicit threshold