DB Olympics

Queries · q312 · Chief Information Security Officer · week 2, night 1

Audit traceability of order cancellations and invoice voids

“For the two 'deletion-like' events on our financial documents that don't use soft-delete — a cancelled order, a voided invoice — is there any audit_log record at all tying back to that specific row?”

Manager · asked the questionHenrik AaltoChief Information Security Officer. Joined from a cloud provider's security team. Several enterprise deals depend on passing security reviews.
Analyst · planned the reportNoah FischerSystems thinker: maps how one process feeds the next before measuring anything.
Engineer · wrote the SQLViktorOld-school warehouse engineer: explicit JOIN chains straight to the base tables, derived tables, GROUP BY with CASE pivots, few CTEs.

Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told

  1. Henrik Aalto writes to Noah Fischer

    The manager, at the start of week 2

    Subject: Re: Account security snapshot — good start, three gaps worry me more now

    Noah,

    This is a strong first week — thank you. The sign-in and access-governance findings give me confidence I can defend in a questionnaire. Three things genuinely surprised me and I want to understand them better before Friday's memo, plus one follow-up.

    1. The audit trail gap is bigger than I initially feared. If we can't show who deleted something and when for one of our most common objects, I have to assume the same is true for the things a security reviewer or a merchant's own auditor would actually ask about — customer records, orders, discounts, financial documents. Can you check whether this is a product-wide blind spot or specific to a few areas? I need to know how wide the hole is before I can tell leadership whether this is "fix one thing" or "fix how deletion logging works everywhere."
    2. Shopper MFA at zero is either a non-issue or a real liability — I need to know which. Before this goes anywhere near a customer-facing security questionnaire, I want to understand what actually protects a shopper's account today if MFA genuinely isn't offered to them — is there anything else standing in for it (unusual-device checks, step-up verification at checkout, that sort of thing)? I don't want to either overstate a gap that doesn't matter or understate one that does.
    3. The churn-deactivation problem — how far does it go? You found it through privileged roles, but I suspect it's broader: if accounts aren't deprovisioned when a tenant churns, that likely applies to their sessions, their API access, everything tied to that tenant, not just role holders. I'd like a sense of the overall scale of "access that should have been turned off but wasn't" so I can size the fix properly.
    4. On the old API keys — are any of them doing something sensitive? Age alone isn't the risk; age combined with broad access is. Before I recommend a rotation mandate, I'd like to know whether those oldest keys are concentrated among a few tenants or integrations, and whether any of them look like they carry elevated access rather than routine, narrow use.

    If we can only chase a couple of these deeply this week, I'd prioritize the deletion-audit scope (#1) and the churn-deactivation scope (#3) — those are the two a due-diligence team is most likely to ask us to prove, one way or the other.

    Thanks, Henrik

  2. Noah Fischer plans the night

    The analyst · week 2, night 1, request 2 of 3

    Henrik asked us to prioritize #1 (how wide is the deletion-audit gap) and #3 (how far does the churn-deactivation problem go) this week, so all 3 slots go there tonight. For #1, last week's 6-table sample (products at 0% coverage, users at 69%, three others with zero soft-deletes ever recorded) isn't enough to tell him whether this is "fix one thing" or "fix deletion logging everywhere" — so tonight I'm running a full census across every table that actually supports soft-deletion, not a hand-picked sample. I'm also addressing a gap in how I framed this last week: orders, invoices and other financial documents don't use deleted_at at all — they're "deleted" via a status change (cancelled/void), so testing audit coverage for the specific things Henrik named (orders, financial documents) needs a different query than soft-delete matching, which is report 2. For #3, last week I only checked role holders; tonight's report sizes the exposure across every access surface on churned tenants (user accounts, sessions, API keys, webhooks) so Henrik can judge whether this is a one-line fix (deactivate Owner/Admin) or a broader deprovisioning gap. I'm deliberately not touching #2 (shopper MFA compensating controls) or #4 (old-key sensitivity) tonight per his stated priority, and will pick those up once #1 and #3 are sized.

    Audit traceability of order cancellations and invoice voids
    Question
    For the two 'deletion-like' events on our financial documents that don't use soft-delete — a cancelled order, a voided invoice — is there any audit_log record at all tying back to that specific row?
    One row per
    entity type: 'order cancellations' and 'invoice voids'
    Columns
    • entity type
    • total events (all-time, 2023-01-01 to 2025-12-31)
    • events with at least one matching audit_log row (any action, entity_table + entity_id match)
    • coverage rate (%)
    • distinct tenants represented
  3. Viktor writes the SQL

    The engineer · 1 dry run on the empty schema, then the submission

    Dry run 1 · STATUS: COMPILES (dry run on the empty schema, no data read)
    SELECT
      'order cancellations' AS entity_type,
      COUNT(*) AS total_events,
      COUNT(*) FILTER (
        WHERE EXISTS (
          SELECT 1 FROM audit_log al
          WHERE al.entity_table = 'orders'
            AND al.entity_id = o.id
            AND al.tenant_id = o.tenant_id
        )
      ) AS events_with_match,
      ROUND(
        100.0 * COUNT(*) FILTER (
          WHERE EXISTS (
            SELECT 1 FROM audit_log al
            WHERE al.entity_table = 'orders'
              AND al.entity_id = o.id
              AND al.tenant_id = o.tenant_id
          )
        ) / NULLIF(COUNT(*), 0), 1
      ) AS coverage_rate_pct,
      COUNT(DISTINCT o.tenant_id) AS distinct_tenants
    FROM orders o
    WHERE o.status = 'cancelled'
      AND o.cancelled_at >= DATE '2023-01-01'
      AND o.cancelled_at < DATE '2026-01-01'
    
    UNION ALL
    
    SELECT
      'invoice voids' AS entity_type,
      COUNT(*) AS total_events,
      COUNT(*) FILTER (
        WHERE EXISTS (
          SELECT 1 FROM audit_log al
          WHERE al.entity_table = 'invoices'
            AND al.entity_id = i.id
            AND al.tenant_id = i.tenant_id
        )
      ) AS events_with_match,
      ROUND(
        100.0 * COUNT(*) FILTER (
          WHERE EXISTS (
            SELECT 1 FROM audit_log al
            WHERE al.entity_table = 'invoices'
              AND al.entity_id = i.id
              AND al.tenant_id = i.tenant_id
          )
        ) / NULLIF(COUNT(*), 0), 1
      ) AS coverage_rate_pct,
      COUNT(DISTINCT i.tenant_id) AS distinct_tenants
    FROM invoices i
    WHERE i.status = 'void'
      AND i.voided_at >= DATE '2023-01-01'
      AND i.voided_at < DATE '2026-01-01'
    
    ORDER BY entity_type

    “Query compiles cleanly. Submitting.”

    Submitted · the SQL of the last dry run, unchanged · Submitted to tonight's batch. Your work on this request is done.

    “Submitted. Summary for the record: - Grain: one row per entity type (order cancellations, invoice voids). - Logic: for each cancelled order / voided invoice, an EXISTS check against audit_log on (entity_table, entity_id, tenant_id), any action — matching the "looser than soft-delete" test requested. - Date scoping: used orders.cancelled_at and invoices.voided_at bounded to 2023-01-01–2025-12-31, since those are the natural event timestamps for each deletion-like state. - Caveat flagged in the note: a match only proves some audit trail exists for that row — not that the cancellation/void action itself was captured.”

  4. The report comes back overnight

    As Noah Fischer saw it, from the small dataset the agents worked on

    entity_type         | total_events | events_with_match | coverage_rate_pct | distinct_tenants
    --------------------+--------------+-------------------+-------------------+-----------------
    invoice voids       |            0 |                 0 |              NULL |                0
    order cancellations |         1712 |                 3 |               0.2 |               90
    (2 rows)
  5. What came of it

    The memo and the queries that followed; links found by title, so inferred

    Later queries that build on this report:

    It builds on:

  6. It becomes benchmark query q312

    Changed only where the benchmark needs the same result on every system

    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • numericsdocs/NUMERICS.md: rounding removed; results keep full precision
    • determinismthe standard leaves the place of NULLs in a sort to the implementation: NULLS LAST ascending, NULLS FIRST descending, as in the reference results
    The benchmark text, against the submitted SQL
    SELECT  'order cancellations' AS entity_type,  COUNT(*) AS total_events,  COUNT(*) FILTER (    WHERE EXISTS (      SELECT 1 FROM audit_log al      WHERE al.entity_table = 'orders'        AND al.entity_id = o.id        AND al.tenant_id = o.tenant_id    )  ) AS events_with_match,  ROUND(    100.0 * COUNT(*) FILTER (      WHERE EXISTS (        SELECT 1 FROM audit_log al        WHERE al.entity_table = 'orders'          AND al.entity_id = o.id          AND al.tenant_id = o.tenant_id      )    ) / NULLIF(COUNT(*), 0), 1  ) AS coverage_rate_pct,  100.0 * COUNT(*) FILTER (    WHERE EXISTS (      SELECT 1 FROM audit_log al      WHERE al.entity_table = 'orders'        AND al.entity_id = o.id        AND al.tenant_id = o.tenant_id    )  ) / NULLIF(COUNT(*), 0) AS coverage_rate_pct,  COUNT(DISTINCT o.tenant_id) AS distinct_tenantsFROM orders oWHERE o.status = 'cancelled'  AND o.cancelled_at >= DATE '2023-01-01'  AND o.cancelled_at < DATE '2026-01-01'UNION ALLSELECT  'invoice voids' AS entity_type,  COUNT(*) AS total_events,  COUNT(*) FILTER (    WHERE EXISTS (      SELECT 1 FROM audit_log al      WHERE al.entity_table = 'invoices'        AND al.entity_id = i.id        AND al.tenant_id = i.tenant_id    )  ) AS events_with_match,  ROUND(    100.0 * COUNT(*) FILTER (      WHERE EXISTS (        SELECT 1 FROM audit_log al        WHERE al.entity_table = 'invoices'          AND al.entity_id = i.id          AND al.tenant_id = i.tenant_id      )    ) / NULLIF(COUNT(*), 0), 1  ) AS coverage_rate_pct,  100.0 * COUNT(*) FILTER (    WHERE EXISTS (      SELECT 1 FROM audit_log al      WHERE al.entity_table = 'invoices'        AND al.entity_id = i.id        AND al.tenant_id = i.tenant_id    )  ) / NULLIF(COUNT(*), 0) AS coverage_rate_pct,  COUNT(DISTINCT i.tenant_id) AS distinct_tenantsFROM invoices iWHERE i.status = 'void'  AND i.voided_at >= DATE '2023-01-01'  AND i.voided_at < DATE '2026-01-01'ORDER BY entity_typeORDER BY entity_type NULLS LAST
    SELECT
      'order cancellations' AS entity_type,
      COUNT(*) AS total_events,
      COUNT(*) FILTER (
        WHERE EXISTS (
          SELECT 1 FROM audit_log al
          WHERE al.entity_table = 'orders'
            AND al.entity_id = o.id
            AND al.tenant_id = o.tenant_id
        )
      ) AS events_with_match,
      100.0 * COUNT(*) FILTER (
        WHERE EXISTS (
          SELECT 1 FROM audit_log al
          WHERE al.entity_table = 'orders'
            AND al.entity_id = o.id
            AND al.tenant_id = o.tenant_id
        )
      ) / NULLIF(COUNT(*), 0) AS coverage_rate_pct,
      COUNT(DISTINCT o.tenant_id) AS distinct_tenants
    FROM orders o
    WHERE o.status = 'cancelled'
      AND o.cancelled_at >= DATE '2023-01-01'
      AND o.cancelled_at < DATE '2026-01-01'
    
    UNION ALL
    
    SELECT
      'invoice voids' AS entity_type,
      COUNT(*) AS total_events,
      COUNT(*) FILTER (
        WHERE EXISTS (
          SELECT 1 FROM audit_log al
          WHERE al.entity_table = 'invoices'
            AND al.entity_id = i.id
            AND al.tenant_id = i.tenant_id
        )
      ) AS events_with_match,
      100.0 * COUNT(*) FILTER (
        WHERE EXISTS (
          SELECT 1 FROM audit_log al
          WHERE al.entity_table = 'invoices'
            AND al.entity_id = i.id
            AND al.tenant_id = i.tenant_id
        )
      ) / NULLIF(COUNT(*), 0) AS coverage_rate_pct,
      COUNT(DISTINCT i.tenant_id) AS distinct_tenants
    FROM invoices i
    WHERE i.status = 'void'
      AND i.voided_at >= DATE '2023-01-01'
      AND i.voided_at < DATE '2026-01-01'
    
    ORDER BY entity_type NULLS LAST
    10 ms100 ms1 sHyper20.8 msDuckDB48.8 msGizmoSQL50.5 msclickhouse_brew199 msFirebolt79.5 msDataFusionerrorClickHouseerror
    • DataFusionThis feature is not implemented: Physical plan does not support logical expression Exists(Exists { subquery: <subquery>, negated: false })
    • ClickHouseCode: 48. DB::Exception: Correlated subqueries in aggregate function argument are not supported. (NOT_IMPLEMENTED)
  7. Everything, as recorded

    The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted

    Viktor's whole session, 7 turns
    • The request, as Viktor received it
      Report request from Noah Fischer (analyst for the Chief Information Security Officer):
      
      Title: Audit traceability of order cancellations and invoice voids
      Question: For the two 'deletion-like' events on our financial documents that don't use soft-delete — a cancelled order, a voided invoice — is there any audit_log record at all tying back to that specific row?
      Grain: one row per entity type: 'order cancellations' and 'invoice voids'
      Columns:
      - entity type
      - total events (all-time, 2023-01-01 to 2025-12-31)
      - events with at least one matching audit_log row (any action, entity_table + entity_id match)
      - coverage rate (%)
      - distinct tenants represented
      Details: Row 1: orders where status = 'cancelled'. For each such order, check whether any audit_log row exists with entity_table = 'orders' and entity_id = orders.id (any action, not just 'delete', since the logged action type for a cancellation isn't known in advance). Row 2: invoices where status = 'void'. Same existence check against audit_log with entity_table = 'invoices' and entity_id = invoices.id. coverage_rate = events with a match / total events, rounded to 1 decimal percent. This is a looser test than the soft-delete report (any audit row at all for that record, not specifically a 'delete' action) — flag in the report notes that even a 100% match here wouldn't confirm the cancellation/void itself was logged, only that the record has some audit trail presence.
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Viktor calls dry_run
      SELECT
        'order cancellations' AS entity_type,
        COUNT(*) AS total_events,
        COUNT(*) FILTER (
          WHERE EXISTS (
            SELECT 1 FROM audit_log al
            WHERE al.entity_table = 'orders'
              AND al.entity_id = o.id
              AND al.tenant_id = o.tenant_id
          )
        ) AS events_with_match,
        ROUND(
          100.0 * COUNT(*) FILTER (
            WHERE EXISTS (
              SELECT 1 FROM audit_log al
              WHERE al.entity_table = 'orders'
                AND al.entity_id = o.id
                AND al.tenant_id = o.tenant_id
            )
          ) / NULLIF(COUNT(*), 0), 1
        ) AS coverage_rate_pct,
        COUNT(DISTINCT o.tenant_id) AS distinct_tenants
      FROM orders o
      WHERE o.status = 'cancelled'
        AND o.cancelled_at >= DATE '2023-01-01'
        AND o.cancelled_at < DATE '2026-01-01'
      
      UNION ALL
      
      SELECT
        'invoice voids' AS entity_type,
        COUNT(*) AS total_events,
        COUNT(*) FILTER (
          WHERE EXISTS (
            SELECT 1 FROM audit_log al
            WHERE al.entity_table = 'invoices'
              AND al.entity_id = i.id
              AND al.tenant_id = i.tenant_id
          )
        ) AS events_with_match,
        ROUND(
          100.0 * COUNT(*) FILTER (
            WHERE EXISTS (
              SELECT 1 FROM audit_log al
              WHERE al.entity_table = 'invoices'
                AND al.entity_id = i.id
                AND al.tenant_id = i.tenant_id
            )
          ) / NULLIF(COUNT(*), 0), 1
        ) AS coverage_rate_pct,
        COUNT(DISTINCT i.tenant_id) AS distinct_tenants
      FROM invoices i
      WHERE i.status = 'void'
        AND i.voided_at >= DATE '2023-01-01'
        AND i.voided_at < DATE '2026-01-01'
      
      ORDER BY entity_type
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: entity_type, total_events, events_with_match, coverage_rate_pct, distinct_tenants
    • ViktorQuery compiles cleanly. Submitting.
    • Viktor calls submit
      SELECT
        'order cancellations' AS entity_type,
        COUNT(*) AS total_events,
        COUNT(*) FILTER (
          WHERE EXISTS (
            SELECT 1 FROM audit_log al
            WHERE al.entity_table = 'orders'
              AND al.entity_id = o.id
              AND al.tenant_id = o.tenant_id
          )
        ) AS events_with_match,
        ROUND(
          100.0 * COUNT(*) FILTER (
            WHERE EXISTS (
              SELECT 1 FROM audit_log al
              WHERE al.entity_table = 'orders'
                AND al.entity_id = o.id
                AND al.tenant_id = o.tenant_id
            )
          ) / NULLIF(COUNT(*), 0), 1
        ) AS coverage_rate_pct,
        COUNT(DISTINCT o.tenant_id) AS distinct_tenants
      FROM orders o
      WHERE o.status = 'cancelled'
        AND o.cancelled_at >= DATE '2023-01-01'
        AND o.cancelled_at < DATE '2026-01-01'
      
      UNION ALL
      
      SELECT
        'invoice voids' AS entity_type,
        COUNT(*) AS total_events,
        COUNT(*) FILTER (
          WHERE EXISTS (
            SELECT 1 FROM audit_log al
            WHERE al.entity_table = 'invoices'
              AND al.entity_id = i.id
              AND al.tenant_id = i.tenant_id
          )
        ) AS events_with_match,
        ROUND(
          100.0 * COUNT(*) FILTER (
            WHERE EXISTS (
              SELECT 1 FROM audit_log al
              WHERE al.entity_table = 'invoices'
                AND al.entity_id = i.id
                AND al.tenant_id = i.tenant_id
            )
          ) / NULLIF(COUNT(*), 0), 1
        ) AS coverage_rate_pct,
        COUNT(DISTINCT i.tenant_id) AS distinct_tenants
      FROM invoices i
      WHERE i.status = 'void'
        AND i.voided_at >= DATE '2023-01-01'
        AND i.voided_at < DATE '2026-01-01'
      
      ORDER BY entity_type
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • ViktorSubmitted. Summary for the record: - **Grain:** one row per entity type (`order cancellations`, `invoice voids`). - **Logic:** for each cancelled order / voided invoice, an `EXISTS` check against `audit_log` on `(entity_table, entity_id, tenant_id)`, any `action` — matching the "looser than soft-delete" test requested. - **Date scoping:** used `orders.cancelled_at` and `invoices.voided_at` bounded to 2023-01-01–2025-12-31, since those are the natural event timestamps for each deletion-like state. - **Caveat flagged in the note:** a match only proves *some* audit trail exists for that row — not that the cancellation/void action itself was captured.