Search Indexing
This page explains the indexing model OctoFHIR uses today.
If you only need query syntax, see Search Parameters. If you need internals (registry and cache lifecycle), see Search Architecture.
Quick Summary
Section titled “Quick Summary”Search runs in place on the resource JSON. There are no side tables of extracted values: each search
parameter becomes a predicate over the resource column of the type’s table. PostgreSQL indexes make those
predicates fast:
- Functional indexes for the parameters listed in
search.indexed_params, built at startup on the same expression the predicate uses. - A whole-document GIN index on
resource, which serves searches that run as JSON containment.
A parameter with neither still works; it is evaluated row by row. That is also why a new SearchParameter needs no reindexing.
How Each Search Runs
Section titled “How Each Search Runs”| Query | Predicate | Index |
|---|---|---|
Reference (subject=Patient/123) |
Overlap on the references extracted by fhir_extract_ref |
Functional GIN |
Date (ge, lt, ap, and so on) |
Range overlap, then an exact check of each occurrence | Functional GiST on the min-max range |
String, default and :contains |
Match on the extracted text | Functional trigram GIN |
| Number | In-place comparison on the JSON | None of its own |
| Quantity | Range overlap on the values; UCUM searches also use the ucum column |
Functional GiST, plus GIN on value buckets for repeating values |
Token on a top-level code (Encounter.status) |
Containment, or text equality once indexed | Functional btree, otherwise the document GIN |
| Token on a CodeableConcept or Coding | Containment on the element | Functional GIN on that element |
Identifier (identifier=...) |
Containment on the resource | Document GIN |
_lastUpdated, _id |
The updated_at and id columns |
id is the primary key; updated_at has no index by default |
_profile |
The generated profile column |
Covering and GIN indexes on that column |
Chains and _has |
Follow the reference links in place on the JSON | The indexes of the parameters involved |
_include, _revinclude |
Read references from the resource JSON | None of their own |
A reference is compared in a normal form: fhir_extract_ref drops any /_history/ version, and the server’s
base URL is not stored in it, so changing base_url does not invalidate the index.
Choosing What Is Indexed
Section titled “Choosing What Is Indexed”search.indexed_params lists parameters as ResourceType.code:
[search]indexed_params = ["Patient.birthdate", "Patient.name", "Observation.subject", "Observation.date"]The list replaces the default, which covers common date, name and reference parameters of Patient, Practitioner, Organization, Observation, Encounter, Condition, MedicationRequest and a few more. Token parameters are not in the default list. Keep the list to what you search: every write to a type maintains its indexes.
Indexes are created at startup with CREATE INDEX IF NOT EXISTS, never at runtime. An unknown parameter or a
missing table is skipped. The names show what each one is:
| Parameter type | Index |
|---|---|
| Date | idx_{table}_{code}_date (GiST) |
| String | idx_{table}_{code}_str (trigram GIN) |
| Token, top-level code | idx_{table}_{code}_code (btree) |
| Token, CodeableConcept or Coding | idx_{table}_{code}_token (GIN) |
| Reference | idx_{table}_{code}_ref (GIN) |
| Quantity | idx_{table}_{code}_qhull (GiST), and _qbucket (GIN) for repeating values |
Other token shapes get no functional index. Identical index bodies are built once, so overlapping parameters share one index.
Three more settings:
-
storage.postgres.document_gin_resource_typesnarrows the whole-document GIN index to the listed types, or drops it with[]. Left unset, every type has one. It is the largest index on a table. -
search.ucum_indexed_paramsadds a GiST index over canonical UCUM values for a quantity parameter. Unit-aware matching works without it, but a search that names the UCUM system then scans the table. -
[[search.composite_index]]adds a partial index on the quantity component of a parameter, for rows with one exact token:[[search.composite_index]]resource_type = "Observation"param = "code-value-quantity"system = "http://loinc.org"code = "8867-4"
The index advisor suggests where to look.
Unsupported
Section titled “Unsupported”:text on string and token parameters is refused with an error: the server has no index path for it and does
not fall back to walking the JSON.
Debug Plans
Section titled “Debug Plans”Search debug output is disabled by default and must be enabled explicitly:
[search]allow_debug_search_plan = trueallow_debug_search_explain_analyze = falseWhen enabled, request a redacted search plan with either query syntax or the header form:
GET /fhir/Patient?family=SmithX-OctoFHIR-Search-Debug: planAccepted header values are plan, sql, explain, and explain-analyze. In the query the parameter is
_debug, with search-plan, search-sql, search-explain or search-explain-analyze.
explain-analyze additionally requires allow_debug_search_explain_analyze = true because it executes the
query. The response is returned as a FHIR Parameters resource and redacts search values while preserving
strategy, index, and SQL-shape information.
How This Affects Query Design
Section titled “How This Affects Query Design”Prefer Typed References
Section titled “Prefer Typed References”# Better (typed)GET /fhir/Observation?subject=Patient/123
# Allowed but less explicitGET /fhir/Observation?subject=123Use Bounded Date Windows
Section titled “Use Bounded Date Windows”GET /fhir/Observation?date=ge2026-01-01&date=lt2026-02-01Bounded windows usually plan better than very broad unbounded date filters.
Use :exact Intentionally
Section titled “Use :exact Intentionally”GET /fhir/Patient?family:exact=SmithUse :exact for exact text semantics, not as a generic performance toggle.
Write Path Behavior
Section titled “Write Path Behavior”A create or update writes the resource row, with the canonical UCUM values in its ucum column. PostgreSQL
maintains the indexes as part of the write. A delete removes the row. There is nothing else to keep in step.
Operational Checks
Section titled “Operational Checks”List the indexes of a table
Section titled “List the indexes of a table”SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'observation';See which indexes are used
Section titled “See which indexes are used”SELECT indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS sizeFROM pg_stat_user_indexesWHERE relname = 'observation'ORDER BY idx_scan;An index with no scans still costs every write.
Tuning Strategy
Section titled “Tuning Strategy”Start with the defaults, then add parameters to search.indexed_params when measurements justify it:
- a search that shows a sequential scan in its plan
- very high traffic on one string field
- clear slow-query evidence and a repeatable
EXPLAINimprovement
For broader guidance, see Performance Tuning.