DB Olympics

Queries · q317 · Chief Information Security Officer · week 2, night 3

Actor attribution in order_status_history, across all transition types

“Is the missing 'who changed this' actor specific to order cancellations, or does order_status_history never record an actor for any status transition?”

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 SQLOmarPragmatic: the shortest correct query, aggregates first and joins dimensions afterwards, FILTER clauses for conditional counts.

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 3, request 1 of 3

    Big reversal on #1 tonight's results refine, not confirm, last week's alarm: order_status_history shows 100% coverage of cancellations across every one of the 50 tenants printed — Tidewick DOES have a timestamped record of every cancellation, just not in generic audit_log. But matching_rows_with_actor is 0 for every tenant — the WHO is missing even though the WHEN is solid. That's a much narrower, more precise finding than "we can't reconstruct order history." Invoice voids turn out to not exist at all (0 ever), so that's a non-issue; but 85 "uncollectible" write-offs across 11 tenants have genuinely 0% audit trail and no purpose-built history table standing in — that's the real, confirmed financial-document gap. And the churn-session risk mostly evaporates: all never-revoked sessions on all 9 churned tenants are already naturally expired (0 still valid) — so the real residual churn risk is just account status (100% of users, all 9 tenants) plus the 2 tenants with a live API key/webhook found last week.

    With #1 and #3 now reasonably well-characterized, tonight I'm clearing Henrik's remaining two questions: whether the "no actor" gap in order_status_history is specific to cancellations or systemic across all transitions (quick, sharpens the #1 story), what actually stands in for shopper MFA (oauth-linked login vs raw password), and who specifically holds the oldest active API keys so we can judge concentration/sensitivity for #4.

    Actor attribution in order_status_history, across all transition types
    Question
    Is the missing 'who changed this' actor specific to order cancellations, or does order_status_history never record an actor for any status transition?
    One row per
    to_status value in order_status_history
    Columns
    • to_status
    • total transitions (all-time, 2023-01-01 to 2025-12-31)
    • transitions with a non-null changed_by_user_id
    • share with an actor recorded (%)
    • distinct tenants represented
  3. Omar 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
      to_status,
      count(*) as total_transitions,
      count(*) filter (where changed_by_user_id is not null) as transitions_with_actor,
      round(100.0 * count(*) filter (where changed_by_user_id is not null) / nullif(count(*), 0), 1) as share_with_actor_pct,
      count(distinct tenant_id) as distinct_tenants
    from order_status_history
    where created_at >= date '2023-01-01' and created_at < date '2026-01-01'
    group by to_status
    order by total_transitions desc, to_status

    “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. The report groups order_status_history by to_status across the full 2023-01-01–2025-12-31 window, showing total transitions, transitions with a non-null changed_by_user_id, the actor-recorded share (%), and distinct tenant count — which will directly show whether the actor gap is isolated to cancelled or systemic across all 7 status values.”

  4. The report comes back overnight

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

    to_status | total_transitions | transitions_with_actor | share_with_actor_pct | distinct_tenants
    ----------+-------------------+------------------------+----------------------+-----------------
    pending   |             51039 |                      0 |                  0.0 |              118
    paid      |             49906 |                      0 |                  0.0 |              118
    fulfilled |             49257 |                      0 |                  0.0 |              118
    cancelled |              1712 |                      0 |                  0.0 |               90
    refunded  |              1586 |                      0 |                  0.0 |               74
    (5 rows)
  5. What came of it

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

    It builds on:

  6. It becomes benchmark query q317

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

    • 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 (2×)
    The benchmark text, against the submitted SQL
    select  to_status,  count(*) as total_transitions,  count(*) filter (where changed_by_user_id is not null) as transitions_with_actor,  round(100.0 * count(*) filter (where changed_by_user_id is not null) / nullif(count(*), 0), 1) as share_with_actor_pct,  100.0 * count(*) filter (where changed_by_user_id is not null) / nullif(count(*), 0) as share_with_actor_pct,  count(distinct tenant_id) as distinct_tenantsfrom order_status_historywhere created_at >= date '2023-01-01' and created_at < date '2026-01-01'group by to_statusorder by total_transitions desc, to_statusorder by total_transitions desc NULLS FIRST, to_status NULLS LAST
    select
      to_status,
      count(*) as total_transitions,
      count(*) filter (where changed_by_user_id is not null) as transitions_with_actor,
      100.0 * count(*) filter (where changed_by_user_id is not null) / nullif(count(*), 0) as share_with_actor_pct,
      count(distinct tenant_id) as distinct_tenants
    from order_status_history
    where created_at >= date '2023-01-01' and created_at < date '2026-01-01'
    group by to_status
    order by total_transitions desc NULLS FIRST, to_status NULLS LAST
    10 ms100 ms1 sHyper64.4 msDuckDB59.9 msGizmoSQL92.7 msclickhouse_brew199 msFirebolt114 msDataFusion98.1 msClickHouse112 ms
  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

    Omar's whole session, 7 turns
    • The request, as Omar received it
      Report request from Noah Fischer (analyst for the Chief Information Security Officer):
      
      Title: Actor attribution in order_status_history, across all transition types
      Question: Is the missing 'who changed this' actor specific to order cancellations, or does order_status_history never record an actor for any status transition?
      Grain: one row per to_status value in order_status_history
      Columns:
      - to_status
      - total transitions (all-time, 2023-01-01 to 2025-12-31)
      - transitions with a non-null changed_by_user_id
      - share with an actor recorded (%)
      - distinct tenants represented
      Details: Source: order_status_history grouped by to_status (pending, confirmed, paid, partially_fulfilled, fulfilled, cancelled, refunded). For each, count total rows, count rows where changed_by_user_id is not null, and the percentage. This tells us whether the actor gap found for cancellations is a systemic property of the table (same for every transition type) or specific to cancellations. Order by total transitions descending, to_status as tie-break (expect 7 rows).
      
      Check your query with the warehouse's dry_run tool, then hand it in with submit.
    • Omar calls dry_run
      select
        to_status,
        count(*) as total_transitions,
        count(*) filter (where changed_by_user_id is not null) as transitions_with_actor,
        round(100.0 * count(*) filter (where changed_by_user_id is not null) / nullif(count(*), 0), 1) as share_with_actor_pct,
        count(distinct tenant_id) as distinct_tenants
      from order_status_history
      where created_at >= date '2023-01-01' and created_at < date '2026-01-01'
      group by to_status
      order by total_transitions desc, to_status
    • The warehouse answers
      STATUS: COMPILES (dry run on the empty schema, no data read)
      COLUMNS: to_status, total_transitions, transitions_with_actor, share_with_actor_pct, distinct_tenants
    • OmarQuery compiles cleanly. Submitting.
    • Omar calls submit
      select
        to_status,
        count(*) as total_transitions,
        count(*) filter (where changed_by_user_id is not null) as transitions_with_actor,
        round(100.0 * count(*) filter (where changed_by_user_id is not null) / nullif(count(*), 0), 1) as share_with_actor_pct,
        count(distinct tenant_id) as distinct_tenants
      from order_status_history
      where created_at >= date '2023-01-01' and created_at < date '2026-01-01'
      group by to_status
      order by total_transitions desc, to_status
    • The warehouse answers
      Submitted to tonight's batch. Your work on this request is done.
    • OmarSubmitted. The report groups `order_status_history` by `to_status` across the full 2023-01-01–2025-12-31 window, showing total transitions, transitions with a non-null `changed_by_user_id`, the actor-recorded share (%), and distinct tenant count — which will directly show whether the actor gap is isolated to `cancelled` or systemic across all 7 status values.