Choosing a Consistent Read Snapshot for a Report

A report with several queries may need all observations to refer to one database snapshot. Select an isolation mode or query shape that provides it.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace two counts
  2. Test a staged change
  3. Trace two report queries across an intervening write
  4. Decide which report facts must agree

Trace two counts

One query reads a total while a later query reads category counts after another transaction adds an item. The observations may disagree under statement-level snapshots. Aggregation grain give the expected relationship.

Test a staged change

Use two controlled sessions and commit the change between the report's reads. Isolation contracts let you state which editions of data the report should see.[1]

Trace two report queries across an intervening write

A report counts orders and then computes their total. Another connection inserts an order between those queries. With a fresh snapshot per statement, the count and total can correspond to different versions. A consistent snapshot can keep both based on one version. See reading committed state.

Initial orders: two totaling 30
Count query observes 2
Another transaction adds 20
Later total can be 50 with a fresh snapshot

Decide which report facts must agree

A live dashboard can tolerate some independent refreshes, while an audited export may require consistent joint results. Use the database's isolation contract and verify both queries observe the intended moment.

References

  1. PostgreSQL Global Development Group, Transaction Isolation ↩