Prefix
Prefix search functionality for PostgreSQL
The prefix
extension provides efficient prefix (starts-with) search functionality in PostgreSQL.
While it’s useful for implementing autocomplete features and prefix-based filtering, it’s particularly
important in telephony applications where call routing and costs depend on matching phone numbers
to operator prefixes.
Your Nile database arrives with the prefix
extension already enabled.
Understanding Prefix Search
A prefix search finds strings that begin with a specific pattern. In telephony applications, this is crucial for:
- Matching phone numbers to carrier prefixes
- Determining call routing paths
- Calculating call costs based on destination
- Identifying geographic regions from area codes
For example:
- “1212” is a prefix for all London phone numbers starting with “1212”
- “+1” is a prefix for North American numbers
- “91” is a prefix for calls to India
Quick Start
Let’s walk through some common prefix search operations using telephony examples.
Creating a Table for Phone Prefixes
Inserting Sample Data
Basic Phone Number Queries
Find carrier and rate for a specific phone number:
Call Cost Calculations
Call Router Implementation
Operators
The prefix
extension introduces operators on prefix ranges:
@>
: Checks if the prefix range contains the given number or range<@
: Checks if the prefix range or numberis contained within another prefix range&&
: Checks for overlapping prefix ranges|
: Is union of two prefix ranges&
: Is intersection of two prefix ranges
In addition, operators <=
, >=
, <
, >
, =
, <>
are supported for prefix ranges.
Common Use Cases
- Telephone number routing
- Call cost calculation
- Carrier and geographic prefix matching
- Product SKU searches (e.g., finding all products starting with ‘ELEC-’)
- URL path routing and matching
- File system path lookups