Skip to content

Health Software Common Practices

HSCP-R-002 · recipe · Security

Write the log entry before the read, in the same transaction

Written entry, version 0.2, checked on 2026-11-15.

At a glance

ImplementsHSCP-G-002 Log access to health data
CaseInstantiates: applies an existing practice to a context.
Contextsoftware used during care (patient record, teleconsultation, care coordination) whose data are read through several paths
Verificationopen test · review

Implements HSCP-G-002, Log access to health data. Case: instantiates a generic practice for the health context.

When

The product logs changes and lets reads through, or logs in the user interface and forgets the other paths: API called by a third-party tool, MCP server, agent, overnight batch, administration account. A read made without reason changes nothing: without a read entry, it leaves no trace.

Six paths lead to the data. All go through the same layer, except direct database access:

The six access paths to health data
The practitioner, the AI agent, the MCP server, the third-party tool via API, the administrator and the external service provider go through a server-side access layer, which writes the access log and then reads the database. The administrator and the service provider can also access the database directly, without leaving an entry in the log.

How

  1. Route every read through a single server-side function, the only path to the table being read.
  2. Require an access context at the entry of this function: the person accountable for the access, the path taken, the basis of the access. Without a context, the read is refused, including for internal processing.
  3. In a single transaction: check the right, write the log entry, read the data, commit. If the log refuses the entry, the data is not returned.
  4. Have the database itself enforce the two rules on the basis: an access on the basis of care designates which care episode (appointment, stay, care team); any other access carries a written reason.
  5. Give the application account the right to add entries to the log, without the right to read, modify or delete them. Restrict reading to the people in charge of the review.
  6. Set the retention period of the log and implement it.
  7. Run the review queries at a fixed interval (accesses outside a care episode, accesses where the user bears the patient's name). A designated person reads the result and signs it.

A read, step by step:

A read, step by step
The requester sends its request with its context to the access layer. In a single transaction, the layer checks the right, writes the entry in the access log, reads the data in the database, then commits. It then returns the data to the requester.

Variants:

  • Refused attempts: log them in a separate transaction, since the failure of the read rolls back the main transaction and its entry.
  • Export: write the entry at the time of export, as a file taken out of the database no longer leaves any trace afterwards.
  • Direct database access (administration, maintenance): named accounts, database engine log for these accounts, access opened for a set duration and reason.

Data model

One entry per access. The fields answer four questions.

Question Field Content
Who acteur_id the person accountable for the access
Who chemin user interface, third-party API, MCP, agent, administration, service provider
Who agent_id identity of the agent or MCP client, see HSCP-R-003
What patient_id the record concerned
What ressource, ressource_id report, result, prescription
What action read, list, export, create, modify, delete
When horodatage set by the database
On what basis titre care episode, break-glass, support
On what basis prise_en_charge_id the appointment or the stay
On what basis motif written by the user
Grouping requete_id shared by the reads of a same request

Examples (PostgreSQL 16)

The two rules, enforced by the database:

CONSTRAINT titre_justifie CHECK (
  (titre = 'prise_en_charge' AND prise_en_charge_id IS NOT NULL)
  OR (titre <> 'prise_en_charge' AND motif IS NOT NULL AND length(trim(motif)) >= 10)
),
CONSTRAINT agent_nomme CHECK (
  chemin NOT IN ('agent', 'mcp') OR agent_id IS NOT NULL
)

The log as append-only:

CREATE FUNCTION acces_journal_fige() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
  RAISE EXCEPTION 'acces_journal : ajout seul, % refusé', TG_OP;
END $$;

CREATE TRIGGER acces_journal_ajout_seul
  BEFORE UPDATE OR DELETE OR TRUNCATE ON acces_journal
  FOR EACH STATEMENT EXECUTE FUNCTION acces_journal_fige();

REVOKE ALL ON acces_journal FROM PUBLIC;
GRANT INSERT ON acces_journal TO appli;
GRANT SELECT ON acces_journal, utilisateur, patient TO revue;

A review query, accesses outside a care episode over the last seven days:

SELECT j.horodatage::date AS jour, u.nom AS acteur, j.chemin,
       p.nom AS patient, j.ressource, j.titre, j.motif
FROM acces_journal j
JOIN utilisateur u ON u.id = j.acteur_id
JOIN patient p     ON p.id = j.patient_id
WHERE j.horodatage >= now() - interval '7 days'
  AND j.titre <> 'prise_en_charge'
ORDER BY j.horodatage;

Writing before reading, in Go (excerpt from code that compiles, not run against the database):

tx, err := d.db.BeginTx(ctx, nil)
if err != nil {
    return "", err
}
defer tx.Rollback()

_, err = tx.ExecContext(ctx, `
    INSERT INTO acces_journal
      (acteur_id, chemin, agent_id, requete_id, patient_id,
       ressource, ressource_id, action, titre, prise_en_charge_id, motif)
    VALUES ($1, $2, $3, $4, $5, 'compte_rendu', $6, 'lire', $7, $8, $9)`,
    a.ActeurID, a.Chemin, vide(a.AgentID), a.RequeteID, patientID,
    id, a.Titre, vide(a.PriseEnChargeID), vide(a.Motif))
if err != nil {
    return "", fmt.Errorf("journal des accès : %w", err) // entry refused: nothing is read
}

Verification

  • CI test: a read without an access context is refused; an access outside a care episode without a reason is refused by the database; after a successful read, the entry exists with all its fields.
  • Permissions test: the application account can neither read, modify nor delete an entry of the log.
  • Pitfall to test: a database constraint whose expression evaluates to NULL is accepted. The reason rule therefore first checks that the reason is filled in, and the test covers the missing reason.
  • Audit agent: identify in the code any query that reads a health data table outside the single function.

Evidence produced

Dated test report, schema of the log table with its constraints, list of the accounts' permissions on the log, signed review results.

Evidence items

Each item attests the recipe at a given date. Frequency given for a product in service.

  • Test report Dated test report: read without context refused, missing reason refused, row written after a readFrequency: at each release
  • Configuration or schema Schema of the log table with its constraintsFrequency: at each release
  • Survey List of account privileges on the logFrequency: at each release
  • Written record Signed results of the log reviewFrequency: monthly

Own mappings

In addition to those of the practice:

Identifier What the recipe contributes
pgssi_s_imputabilite_v1_0/6.2.2-4 one entry per access makes it possible to reconstruct the audit trail by person, by action, by date and by patient
pgssi_s_imputabilite_v1_0/6.2.1.4-1 logs user activity, reads included
ehds_2025_327/annexeII-p13 records at each access the person, the data category and the date, and provides review queries (published text, phased application)
reglement_exec_2024_2690/annexe1-p35 covers access to applications and accesses by administration accounts (text not applicable in France at the date of the entry)

Versions

Versions of the entry (French reference), most recent first
VersionStatusDate
0.2Draft2026-11-15
0.1Draft2026-11-15

How to cite: “Health Software Common Practices”, HSCP-R-002, version 0.2, https://healthtechpractices.org/en/recettes/hscp-r-002/, under the CC BY-NC-SA 4.0 licence. Licence