Querying a HAPI FHIR repository with SQL using HFQL

HFQL lets a HAPI FHIR JPA server answer SQL-like analytical queries through a JDBC driver, but it is experimental and slow on large data. This page explains when it fits and when a separate analytics pipeline is the better choice.

On this page

How HFQL lets analysts query HAPI FHIR with SQL-like syntax

A team that needs record counts or a flat table of FHIR data usually has to write REST searches, page through bundles and stitch the results together by hand. HFQL is an experimental HAPI FHIR module that lets the JPA server answer SQL-like analytical queries directly, and it is reached through a JDBC-compatible driver rather than through the FHIR REST API. Analysts can therefore point a familiar database tool at the server and ask questions such as how many Observations of a given code exist or which Patients meet a set of conditions, without learning the search parameter syntax first.

The HAPI FHIR documentation is direct about the limits. HFQL is not optimized for performance and may take a long time to produce results, so it is best suited to exploration, data profiling and one-off extracts that help a team understand what its data contains. It is not a substitute for scheduled reporting or for queries that must return within a fixed time for an application or a dashboard. Before relying on it, the team should confirm with the HAPI FHIR version it runs which HFQL features are available, how the module is enabled in its configuration, and how the server behaves under concurrent analyst queries.

The practical first step is a small pilot. Enable HFQL on a non-production copy of the JPA server, download the matching driver from the HAPI FHIR GitHub releases, connect a client such as DBeaver, and time a few representative queries against the data volume you expect. The results will show whether ad hoc access is enough for your analysts or whether the workload belongs in a separate analytics pipeline fed from the server's data.

How an analyst query reaches the FHIR data

The query starts in a SQL client such as DBeaver, which connects through the HFQL JDBC driver downloaded from the HAPI FHIR GitHub releases. The request passes an access control point, where authentication and the AuthorizationInterceptor decide whether the caller may run the query at all. The JPA server then reads from the relational database behind it, and results return to the client as rows. Check the access control point first, because HFQL queries run against the same data the server exposes through its REST API.

Path of an HFQL query from the analyst's SQL client to the database and back.

Choosing between HFQL, FHIR search and a separate analytics pipeline

This table compares three ways to answer questions about FHIR data: HFQL on the JPA server, standard FHIR REST search for application needs, and a copied analytics store. Read each row as a trade-off, and treat the load and governance columns as the questions to settle before any analyst gets a database login.

OptionBest fitLoad on the live serverGovernance it needs
Direct HFQL on the JPA serverAd hoc analytical questions over the stored FHIR data, run by a small group of trusted analysts during testing or investigationHigh and unpredictable. HFQL is experimental, is not optimized for performance and may take a long time to return results, so it competes with clinical trafficDedicated accounts, the authorization model applied to the HFQL path, query logging, and a decision on whether HFQL is enabled in production at all
Standard FHIR REST searchApplication features such as worklists, patient lookups and clinical views that need predictable, resource-scoped resultsBounded by the search parameters and indexes the server supports. Heavy unindexed or chained searches still need load testingAccess through the authorization and consent interceptors, audit logging of who searched for what, and documented search limitations for each application
Separate analytics pipelineRecurring reporting, cohort analysis, dashboards and wide joins across large volumes of dataLow on the live server, because data is copied on a schedule or stream. The cost moves to the pipeline and its refresh jobsDefined data extracts, minimization of identifiers, pipeline access reviews, retention rules for copies, and documented lineage back to the source
Typical data freshnessCurrent to the moment of the queryCurrent to the moment of the query for read-through paths, subject to server loadDepends on the pipeline schedule, so reporting users need to know how stale a figure may be
Schema stabilityResource structure as stored, which requires knowing FHIR paths and HFQL syntaxResource structure as stored, with the same FHIR knowledge needed for searchAnalysts work against a model the pipeline team designs, which can be stable across FHIR versions if the mapping is maintained
Operational ownershipThe team that runs the JPA server, with HFQL treated as a feature under testThe application team that owns the search callsA separate data team that owns the copy, its tooling and its failure handling
Question to ask before choosingCan the server absorb a long-running query during clinical hours, and who approves each analyst account?Which searches run per user session, and do they stay within the supported parameters?Who is accountable for the copied data, and what is the approved refresh and retention schedule?

Where HFQL performance breaks down

HFQL is an experimental analytics layer that runs inside the JPA server, so its limits are also limits on the clinical workload sharing that server. The cards below cover the performance constraints to design around before any analyst connects a query tool.

Not optimized for performance

The HAPI FHIR documentation states that HFQL is not optimized for performance. Analytical queries are not tuned the way a dedicated analytics engine is tuned, so a query that looks simple in SQL can translate into expensive searches against the FHIR store. Treat HFQL as a convenience for exploration, not a reporting engine, and test each planned query shape before analysts rely on it.

HFQL docs

Queries may run for a long time

The documentation warns that HFQL may take a long time to produce results. Long-running queries hold database connections and server threads for their full duration. Ask the team to measure elapsed time on representative data and decide in advance what runtime is acceptable, and which queries should be stopped or rewritten.

Shared server with clinical traffic

HFQL runs on the same JPA server that serves clinical reads and writes. A heavy analytical query competes for the database, CPU and memory that clinicians depend on. Ask whether the module is enabled on production nodes at all, or whether analysis runs against a separate instance that can be sized and restarted without affecting care workflows.

Large volumes and wide queries

Cost grows with the number of resources scanned and the number of columns and joins requested. A query that selects many fields across a large Observation or Encounter set can multiply the work. Limit the selected fields, filter on indexed search parameters where possible, and confirm the result size before running exports or unbounded queries.

Experimental status and version drift

HFQL is marked experimental, so its syntax, behavior and driver may change between HAPI FHIR releases. Pin the server version and the matching driver version together, and retest saved queries after every upgrade. Confirm with your vendor or the release notes which HFQL behavior is covered by compatibility commitments.

Test with representative data first

Performance results from an empty or sample database say little about production. Load a copy of realistic record counts and shapes, run the query set analysts expect, and record runtime, memory and database load. Agree on acceptable limits with clinical operations and analytics owners before rollout, and document which questions should move to a separate analytics pipeline.

How to enable HFQL and connect a first query

This sequence takes a team from a running HAPI FHIR JPA server to a working HFQL connection and a first query that can be checked against a plain FHIR search. It is meant for a non-production instance so the experimental module can be tested without risk to live data.

  1. Confirm the server version and JDK requirement

    Identify the exact HAPI FHIR release and JPA server starter version your instance runs, then check its Java requirement in the HAPI FHIR versions documentation. For example, HAPI FHIR 8.2.0 requires JDK 17, but your own release may differ, so confirm it against your build before continuing. Done means you have written down the release number and the matching JDK version, and the JVM on the test host reports that version.

  2. Enable HFQL in a non-production instance

    Turn on the HFQL module in the JPA server configuration of a test or staging instance, not in production. Restart the server and watch the startup log for errors related to the module. Done means the server starts cleanly and HFQL responds to a simple request, which confirms the feature is active before you go further. Your platform team should record the exact configuration key used, since it depends on your release.

  3. Download the matching JDBC driver

    From the HAPI FHIR GitHub releases page, download the JDBC driver that matches the exact server release you are running. Mismatched driver and server versions are a common cause of confusing failures, so do not reuse a driver from another version. Store the file in a location your SQL client can reach and note its checksum. Done means the JAR file name carries the same release number as the server.

  4. Configure the SQL client connection

    In DBeaver or another JDBC-capable client, create a new driver entry that points to the downloaded JAR and set the connection URL to the HFQL endpoint of your test server. Use a test account with read-only access, since the client will send queries as that user. Done means the client shows a successful connection test, and the server log records the connection attempt without authentication errors.

  5. Run a count query against a known resource type

    Start with a simple SELECT over one resource type that holds a small, known number of records, such as Patient in a seeded test dataset. Keep the query narrow so you can judge the result quickly. Done means the query returns rows and the row count matches the number you expect from your seed data.

  6. Compare the result with a FHIR search

    Run a standard FHIR search on the same resource type, for example a search with _summary=count or a plain search with the same filter, and compare the total with the HFQL row count. Repeat this for one filtered query. Done means both numbers agree for each test case. If they differ, record the query and do not use HFQL results for reporting until you have found the cause.

  7. Record the baseline and the limits you observed

    Write down the server release, driver version, query text, runtime and row counts from the test runs. Note any queries that were slow or returned errors, since HFQL is experimental and is not optimized for performance. Done means you have a baseline document your team can review before deciding whether HFQL fits the analytics use case or a separate pipeline is the better option.

Securing analytical access to FHIR data

An HFQL endpoint turns the FHIR server into a queryable database, so the access controls that protect REST reads must be checked for SQL reads too. The cards below cover who can connect, how credentials are handled, whether authorization and consent apply, how analytical reads are logged, and how much data each query can extract. HAPI FHIR provides building blocks for these controls rather than a complete security layer, so the operator must verify coverage.

Who can open a connection

Any tool that can reach the HFQL endpoint and present a valid login can run queries, including desktop database clients such as DBeaver. Treat the driver as a new entry point, not a convenience feature of the existing API. Restrict network paths to the analytics workstations or service accounts that need them, and confirm which endpoints the starter exposes once HFQL is enabled in your build.

HFQL driver docs

Issuing and rotating driver credentials

Decide whether analysts use personal identities from your identity provider or shared service accounts. Personal identities give per-user audit trails and immediate revocation, while shared accounts make both harder. Ask how the driver authenticates in your configuration, where the secret is stored on analyst machines, and how you rotate it without breaking scheduled reports.

Whether authorization rules apply

Confirm with a test account whether your AuthorizationInterceptor and consent rules are evaluated for HFQL queries, not only for REST searches. A query that returns the same rows as an allowed REST search is not proof of coverage. Run a denied-user test and a restricted-compartment test before any analyst receives access.

HAPI security docs

Audit logging of analytical reads

Analytical queries can read large volumes of patient data in a single statement, so they need the same audit trail as clinical access. Check whether BALP audit logging records HFQL requests, which user and query text are captured, and where logs are retained. Without this, a bulk extraction may leave no record that identifies the person who ran it.

Minimizing extracted fields

Select only the columns an analysis needs, and avoid direct identifiers such as names, addresses and identifiers unless they are required. Use pseudonymous keys where the analysis allows it, and document which fields each analytical role can select. A narrow field list also limits the damage from a leaked credential.

Verifying coverage before go-live

Because HAPI FHIR does not provide one security layer for every path, write a coverage matrix that lists each access route to the data, including REST, HFQL, bulk export and any MCP endpoint, and the control that applies to each. Test every row with a restricted account and keep the results as evidence for your security and compliance reviewers.

When a separate analytics pipeline is the better option

HFQL is a reasonable fit when an analyst or engineer wants to explore the shape of FHIR data, test a question, or check whether a field is populated as expected. Run these queries against a small test or staging copy of the server, never against the production JPA database, because HFQL is experimental and is not optimized for performance. Queries can run for a long time, so a result that takes minutes on a sample may take far longer on full data.

  • Low risk to production: A staging copy keeps long-running scans away from clinical workloads.
  • Fast iteration with DBeaver: The JDBC driver lets analysts write and adjust SQL-like queries in a familiar tool.
  • Disposable data: A copy can be rebuilt when the question changes, so no ongoing pipeline is needed.
  • Limits of the copy: A small sample can hide rare codes, skewed populations and missing values that appear at full scale.
  • Copy governance: Staging copies still contain patient data, so access, retention and deletion rules apply to them too.

Before relying on exploratory results, confirm with your own environment how long representative queries take and whether the staging copy matches production search behavior closely enough for your questions.",

What to confirm before relying on HFQL

HFQL is an experimental module, so its behavior depends on the exact HAPI FHIR release you run. The cards below list what to check with the vendor documentation and a proof of concept before any analytics workload depends on it.

Experimental status and release behavior

HFQL is documented as experimental and not optimized for performance, so results may take a long time. Confirm in the release notes for your exact HAPI FHIR version whether the HFQL module has changed, been renamed, or gained or lost features since the version you plan to run. Record the version you tested so later upgrades can be compared against it. Treat any behavior you have not reproduced on your own server as unconfirmed.

HFQL docs

FHIR versions and resource coverage

The JPA server documents support for DSTU2, DSTU3, R4, R4B and R5, but that does not tell you which resource types and search parameters HFQL can address in each version. Build a list of the resource types and fields your reports need, then run each query against a server holding that FHIR version. Note any resource that returns empty results or errors, since silent gaps are easy to miss in analytics output.

Supported SQL features

HFQL accepts SQL-like syntax, not the full SQL standard, so joins, aggregations, ordering, and functions may behave differently from what your BI tools expect. Write a test set covering each construct your team will use, such as filters on FHIR paths, grouping, and sorting, and compare results with a known dataset. Confirm how the module handles references between resources, because analysts often assume relational joins that FHIR storage does not provide.

Driver compatibility with the server

The JDBC driver is downloaded from the HAPI FHIR GitHub releases, and its version must match the server version it talks to. Confirm the driver release that pairs with your server release before connecting DBeaver or any other client. Pin both versions in your configuration, and retest the driver after each server upgrade. A mismatch can show up as connection errors or subtly different result sets.

HAPI FHIR releases

Partitioning and MDM interaction

If your server uses partitioning for multitenancy or MDM for patient matching, HFQL results may not reflect the same access boundaries or linked records that your FHIR API applies. Test a query run under each partition and confirm whether it returns only the data that partition's clients can see. Also check whether results include golden resources, source records, or both, since duplicate counts can change the meaning of an analytic result.

Proof of concept before adoption

Run a proof of concept on a copy of production-like data that covers your largest resource types and your slowest planned query. Measure response time, memory use on the database and server, and whether results match a reference count taken from the FHIR API. Ask the vendor or community which query shapes are known to be slow, and decide in advance whether a separate analytics pipeline is the fallback if the PoC misses your targets.

Common questions before using HFQL for analytics

Is HFQL production-ready for analytics?

No, not as a general analytics layer. HFQL is an experimental HAPI FHIR module that lets the JPA server answer SQL-like queries for analytics, and it is not optimized for performance, so queries may take a long time to produce results. Treat it as suitable for exploration, data validation and ad hoc investigation on a copy or a non-critical instance. For recurring reporting that other teams depend on, plan a separate analytics pipeline. Confirm the module's current status and any stability notes in the HAPI FHIR docs before you commit to it for any workload.

How much effort does an HFQL proof of concept take, and what drives it?

The effort depends on four things: how many FHIR resource types and search parameters the queries touch, how much data sits in the server, whether the HAPI FHIR JPA server is already running in a usable environment, and how many distinct questions the team wants answered. A proof of concept usually includes enabling the module, connecting a JDBC client, writing a set of representative queries, measuring response times and recording where results are slow or incomplete. Ask any firm for a written scope that lists the queries, the data volume, the environment and the success criteria, because those items set the quote.

Can an internal team set up HFQL without outside help?

Often yes, if the team already runs a HAPI FHIR JPA server and has engineers comfortable with Java application servers, database access and JDBC clients. Enabling HFQL and connecting a tool such as DBeaver is a configuration task, and the driver is available from the HAPI FHIR GitHub releases. The harder parts are query design around FHIR's resource model, measuring performance on realistic data and deciding when results are good enough. An internal team that lacks FHIR search experience or a test environment may move faster with a short external review.

What does the client need to provide for an HFQL engagement?

Plan to provide four things: a representative test dataset or permission to load a copy of production-like data, a running HAPI FHIR JPA instance in a non-production environment, a named business owner who can confirm which questions matter, and a technical owner who can grant access, approve configuration changes and review results. Access to the database and server logs is also useful for diagnosing slow queries. Without a named owner, scope tends to drift, and without representative data, performance results may not carry over to production.

When should we move analytics to a separate pipeline instead of HFQL?

Move to a separate pipeline when queries must run on a schedule, when several teams depend on the results, when data volumes make response times unpredictable, or when analysts need joins across large histories and other sources. Pipelines such as a warehouse or lakehouse fed by bulk export or change feeds keep analytical load away from the clinical system of record. Apache Airflow or a similar scheduler can manage those jobs, and tools such as Apache Superset or Metabase can serve the results. Compare the operating cost of that pipeline against the time your team spends working around HFQL limits.

What does a fixed-scope DeployOpen engagement cover for HFQL?

DeployOpen works on self-hosted open-source platforms inside the client's own infrastructure, so an HFQL engagement would run against a HAPI FHIR JPA server the client controls. Most engagements are fixed-scope, meaning the scope, deliverables and timeline are agreed in writing before work starts. A typical written scope would name the environment, the module configuration, the JDBC client setup, the representative queries to test, the performance observations to record and the recommendation on whether to keep HFQL or move to a pipeline. DeployOpen does not publish a price list, so a quote follows from that written scope.

Does HFQL need a separate security review before we give analysts access?

Yes. HFQL exposes FHIR data through a query interface, so access should be reviewed like any other data path. Confirm which users can connect through the JDBC driver, how those connections authenticate, and whether the server's authorization rules restrict which resource types and fields analysts can read. HAPI FHIR states that it does not provide a single security layer and offers building blocks such as the AuthorizationInterceptor and audit logging, so the operator must confirm how those apply to HFQL queries in their own setup before granting access to patient data.

How DeployOpen scopes HFQL and analytics work

DeployOpen plans and deploys self-hosted HAPI FHIR JPA servers in your own cloud accounts, data centers or air-gapped networks, and scoping an HFQL project starts with the server it will run against. Before any query is enabled, the work covers the JPA server build from the starter, the relational database (PostgreSQL or Microsoft SQL Server for production), the network path to the server, and who is allowed to reach the HFQL endpoint and the JDBC driver. Because HFQL is an experimental module that is not optimized for performance, the first scoping question is usually whether analytical reads will compete with clinical traffic on the same database, and that answer shapes the hosting plan more than the query syntax does.

The access review is where most of the testing effort sits. DeployOpen tests the authentication and authorization configuration around analytical queries, including which identities can open a JDBC connection, whether the AuthorizationInterceptor and any consent or search narrowing rules apply to HFQL results as they do to ordinary REST searches, and whether query activity reaches the audit trail that your compliance team reviews. Database tools such as DBeaver make it easy for an analyst to export large result sets, so the test plan also covers where those exports can be saved and who can read them. Confirm with your security team which of these controls they need evidence for before the work starts.

The recommendation is a decision, not a default. HFQL can suit occasional, bounded questions run by a small group against a server with spare capacity. A separate analytics pipeline, such as extracting FHIR data into a warehouse or a columnar store, usually fits recurring reporting, large scans or many concurrent users. DeployOpen's written scope states which option is recommended, the reasons, and what would change that choice. Most engagements are fixed-scope, with deliverables and timelines agreed in writing before work starts, and ongoing support, upgrades and continuous testing are available as a retainer. DeployOpen is not a partner of HAPI FHIR or Smile CDR, and a quote follows from the written scope.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.