Why Does Searching for an Underscore Return Unexpected Records?

A customer searches for the project code AC_7 and receives records named ACB7 and ACX7. The search is not necessarily losing characters. It may be interpreting the underscore as a wildcard rather than as literal text.
Search behavior should match the promise made by the interface. A field labeled “project code” suggests a different contract from a field explicitly offering pattern matching.
Define what the input means
PostgreSQL's pattern-matching documentation explains that LIKE uses an underscore for one character and a percent sign for a sequence of characters. It describes escaping these characters when they must be matched literally.
That behavior is specific to pattern matching. Treating a value safely as a query parameter does not, by itself, change the meaning of wildcard characters inside that value.
Example: a literal project identifier
For an illustrative exact-code search, AC_7 should identify the record whose code contains that underscore. ACB7 is a different code.
Write the expected result set before implementing the query. If the product actually intends a pattern search, explain the syntax to users instead of silently exposing it through an ordinary text field.
Separate query safety from search semantics
Use the database interface's supported parameter binding for values. Separately decide how literal text becomes a pattern, if pattern matching is required at all.
For an exact identifier lookup, an equality comparison may fit the requirement more directly. For a literal contains search, the implementation needs an explicit treatment of wildcard and escape characters.
Review the escape character itself
A transformation that handles percent signs and underscores but ignores its own escape character can still produce incorrect patterns. Review the full encoding path, including application strings, database parameters and the chosen database's escape rules.
Avoid copying an escaping recipe from a different database without checking the contract. The PostgreSQL documentation notes differences from standard SQL behavior, which is a reason to verify the actual system.
Build a small result-set fixture
Create synthetic records with a plain code, a code containing an underscore, one containing a percent sign and one containing the chosen escape character. Add nearby codes that should not match.
For each search input, write the exact set of expected identifiers. Check both false matches and missing matches. A test that verifies only that one desired record appears can miss unwanted additional results.
Include an empty search and decide whether it should show no records, a default list or a validation message. Do not let an implementation detail accidentally turn an empty value into an unrestricted query.
Review case and accent behavior separately. They are additional search decisions, not automatically solved by handling wildcard punctuation.
Verify the user-facing contract
Test through the same interface users operate, so the check includes input transformations and backend behavior. Compare what the user typed with the value the search layer received using safe synthetic data.
Document whether users can deliberately enter patterns. If advanced pattern search is offered, keep it distinguishable from literal search and explain any limits.
For acceptance, have a reviewer predict results for the fixture without seeing the implementation. If their interpretation differs from the system, either the interface or the query contract needs clarification.
Keep a regression case for the original AC_7 search alongside its false matches. After a query-library upgrade, rerun that case through the interface and inspect every returned identifier. This prevents a superficially successful search demonstration from overlooking a broadened result set that could lead a user to choose the wrong business record.
The goal is not to remove useful search capabilities. It is to make punctuation behave predictably for the task at hand, especially when business identifiers contain characters that also have a technical meaning.
Continue with the related FutureOS guide for the related planning decision.
The published FutureOS introduction describes the project as being in prelaunch. Use this guide to prepare a concrete project discussion; it does not announce an available FutureOS feature.
Prepared with AI assistance. Primary sources checked on October 24, 2026. Examples and procedures are illustrative editorial guidance, not FutureOS product announcements or an independently reviewed assessment.


Comments