Skip to content

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.

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:

  1. Functional indexes for the parameters listed in search.indexed_params, built at startup on the same expression the predicate uses.
  2. 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.

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.

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_types narrows 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_params adds 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.

: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.

Search debug output is disabled by default and must be enabled explicitly:

[search]
allow_debug_search_plan = true
allow_debug_search_explain_analyze = false

When enabled, request a redacted search plan with either query syntax or the header form:

GET /fhir/Patient?family=Smith
X-OctoFHIR-Search-Debug: plan

Accepted 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.

Terminal window
# Better (typed)
GET /fhir/Observation?subject=Patient/123
# Allowed but less explicit
GET /fhir/Observation?subject=123
Terminal window
GET /fhir/Observation?date=ge2026-01-01&date=lt2026-02-01

Bounded windows usually plan better than very broad unbounded date filters.

Terminal window
GET /fhir/Patient?family:exact=Smith

Use :exact for exact text semantics, not as a generic performance toggle.

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.

SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 'observation';
SELECT indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes
WHERE relname = 'observation'
ORDER BY idx_scan;

An index with no scans still costs every write.

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 EXPLAIN improvement

For broader guidance, see Performance Tuning.