# 🔧 Field Note 03: Implementation & Querying ### The `UPDATE` statement is the silent killer of data integrity. **By Lund Consulting | Der Maschinenraum** In **[[FN 02 - The Two Clocks]]**, we established the theory: regulated data requires two independent timelines to survive an audit. - **Reality (`event_date`):** When the fact actually occurred in the world. - **Knowledge (`knowledge_date`):** When your database actually learned about it. When these clocks diverge, they create **Delays**, **Corrections**, or **Projections**. If you only have one clock, your "history of knowledge" easily gets deleted, making reports irreproducible. Now, we move into the **Engine Room** to build the solution. The solution is conceptually simple: we build the two columns `event_date` and `knowledge_date` into the database. But moving from concept to implementation creates friction. In this episode, we first dismantle two common concerns regarding this architecture: - "Why can't I just use native Time Travel?" - "I'd rather save storage" Then, we tackle the only concern that actually matters: **How do I query this "mess"?** We will provide the exact SQL to solve it. --- ## 1. Concern A: "Why can't I just use native Time Travel?" The first concern goes as follows: _"**Why build a schema for this? My database (Snowflake, BigQuery, Databricks) already has 'Time Travel' built-in. Isn't that enough?**"_ **The Reality:** Native Time Travel is a **Disaster Recovery** tool, not a business feature. - **It expires:** Vendor retention usually caps at 7–90 days. If an auditor asks for data from two years ago, you are met with an error message. - **It makes tracking "Evolution of Knowledge" difficult:** Auditors often ask how the system's knowledge about an event evolved from State A to State B, including intermediate stages A.1, A.2, etc. - Because native time travel only shows you one snapshot at a time, you cannot query the _pathway_ between them. - Comparing these states would require dozens of manual, effortful time-travels. **The Rule:** Use the native time travel features to fix an accidental `DROP TABLE`. Use Bitemporal Architecture to defend your ledger. --- ## 2. Concern B: "I'd rather save storage" The second concern goes as follows: _"**Looking at an append-only pattern, I'm concerned my database will expand too fast because every correction generates a new entry. I'd rather save storage.**"_ Yes, the database will expand. Recall our example of a backdated correction from [[FN 02 - The Two Clocks]], where a correction requires a new row in the database. | **ID** | **Amount** | **event_date** | **knowledge_date** | **Note** | | ------- | ----------- | -------------- | ------------------ | ------------------------ | | INV-101 | $50,000 | 2024-03-31 | 2024-04-05 | The Original Entry | | INV-101 | **$55,000** | **2024-03-31** | **2024-04-10** | The Backdated Correction | By adding the second row instead of overwriting the first, we have effectively doubled the storage for this invoice. To a developer trained in efficiency, this feels like "waste." **The Reality:** In the 1990s, hard drives were the bottleneck. Today, storage is the cheapest resource in your stack—**Compliance Risks** are the expensive ones. The "cost" of the extra rows is negligible compared to the risk of having no record of what your balance sheet looked like on April 6th. In the _Maschinenraum_, we trade cheap disk space for an immutable, reproducible audit trail. We accept the expansion to eliminate the risk. --- ## 3. The Real Concern: "How do I query this mess?" We will now tackle a real concern: _"**Okay, I’ll take the storage hit. But if I have 10 versions of every invoice in one table, how do I actually work with this?**"_ In the _Maschinenraum_, we solve this by designing a single SQL function that allows you to query both the present and the past. - Querying **the present** means answering to your stakeholders: How much revenue/cost do we have at this moment? - Querying **the past** means answering the audit question: What did the system know on a certain day in the past? Here is the single SQL function useful for both cases: ``` CREATE FUNCTION get_ledger(as_of_date TIMESTAMP) RETURNS SETOF raw_invoice_history AS $ SELECT DISTINCT ON (invoice_id) * FROM raw_invoice_history WHERE knowledge_date <= as_of_date ORDER BY invoice_id, knowledge_date DESC; $ LANGUAGE sql; ``` With this SQL function, querying the present is as simple as: ``` SELECT * FROM get_ledger(now()); ``` And querying for the past simply requires you to state the past date auditors are interested in. ``` SELECT * FROM get_ledger('2025-12-31 23:59:59'); ``` --- ### Summary: Architecture Over Tooling Implementation is about choosing an architecture that prioritizes the **Integrity of History**. By using an Append-Only ledger and smart querying logic, you ensure that your "Two Clocks" never stop ticking. In the future, we will continue to optimize our bitemporal implementation, utilizing a `valid_to` logic (in the sense of SCD Type 2 technique). We will discuss some existing tools, such as the `postgres-periods` extension and the `django-pghistory` plugins. --- ### _Der Maschinenraum_ _Field notes from our disciplined implementation projects._ For inquiries or to harden your firm's data architecture: [email protected]