
Forward-Filling Machine Logs with SQL Window Functions
SMT machines log a generic error just before they log the component that caused it, and a forward-fill CTE using window functions links the two.
- Published
- April 13, 2026
- Author
- Hrushiekesh Reddy Kanjula
- Read time
- ~4 min
- Category
- engineering

Our SMT machines log the error before they log the component that caused it. The log says "Vacuum error on Head 3", not "Component C47 failed vacuum pick". The component ID (C47) shows up in the next event, 50 milliseconds later, when the machine records what it was trying to pick.
A standard JOIN cannot link the two rows. The error row has a NULL component ID. The component row has no error code. A naive query attributes zero errors to any component, and the analytics layer is useless for root cause analysis.
The fix is a CTE with window functions that does a forward-fill. It carries the component identifier back to the error row that precedes it. Here is the full implementation.
The log structure
The machine error log table looks like this:
timestamp | event_type | component_ref | head_id | error_code
2026-04-13 14:32:01 | VACUUM_ERROR | NULL | 3 | E_VAC_LOW
2026-04-13 14:32:01 | COMPONENT_PICK | C47 | 3 | NULL
2026-04-13 14:32:04 | VACUUM_ERROR | NULL | 3 | E_VAC_LOW
2026-04-13 14:32:04 | COMPONENT_PICK | R12 | 3 | NULL
The VACUUM_ERROR rows have the error code and no component ref. The COMPONENT_PICK rows have the component ref and no error code. They share a timestamp and head ID. The error happened while the machine was trying to pick the component logged 50ms later.

The forward-fill CTE
The first version uses LEAD(), a window function that looks at the next row in a partition:
WITH enriched_log AS (
SELECT
timestamp,
event_type,
component_ref,
head_id,
error_code,
-- Look ahead to the next row in this head's timeline
LEAD(component_ref) OVER (
PARTITION BY head_id
ORDER BY timestamp
) AS next_component_ref
FROM machine_error_log
),
attributed_errors AS (
SELECT
timestamp,
head_id,
error_code,
-- Use the component ref from this row if available,
-- otherwise use the one from the next row
COALESCE(component_ref, next_component_ref) AS attributed_component
FROM enriched_log
WHERE error_code IS NOT NULL
)
SELECT * FROM attributed_errors;LEAD() is partitioned by head_id and ordered by timestamp. It returns component_ref from the next row in the same head's event stream. COALESCE takes the current row's component ref if it has one. Otherwise it takes the next row's.

The result is a clean table of errors, each tied to the component being picked when it happened:
timestamp | head_id | error_code | attributed_component
2026-04-13 14:32:01 | 3 | E_VAC_LOW | C47
2026-04-13 14:32:04 | 3 | E_VAC_LOW | R12
Gaps of more than one row
The default LEAD(value, 1) works when the component ID is exactly one row after the error. Some machine firmware versions write extra entries in between: diagnostic codes, head position updates, retry attempts. A single LEAD misses those.
The general solution is a recursive CTE that carries the fill forward across several NULL rows:
WITH RECURSIVE
ordered AS (
-- Number each head's events in time order
SELECT
timestamp, head_id, event_type, component_ref, error_code,
ROW_NUMBER() OVER (
PARTITION BY head_id
ORDER BY timestamp
) AS seq
FROM machine_error_log
),
look_ahead AS (
-- Base case: every error row starts a search at its own position
SELECT
timestamp, head_id, error_code,
seq AS at_seq,
component_ref AS found_ref
FROM ordered
WHERE error_code IS NOT NULL
UNION ALL
-- Recursive case: step to the next event on the same head
-- until a row with a component_ref turns up
SELECT
la.timestamp, la.head_id, la.error_code,
o.seq,
o.component_ref
FROM look_ahead la
JOIN ordered o ON o.head_id = la.head_id
AND o.seq = la.at_seq + 1
WHERE la.found_ref IS NULL
)
SELECT timestamp, head_id, error_code, found_ref AS attributed_component
FROM look_ahead
WHERE found_ref IS NOT NULL
ORDER BY head_id, timestamp;Each error row starts a search at its own position. The recursion steps to the next event on the same head until it finds a component_ref. This handles gaps of any length. Two errors in a row both get the next component. An error with no component after it drops out, so check for those separately.
The events are numbered per head with ROW_NUMBER(), not by rowid. On a line with several heads, the next row in the table is often another head's event.

Performance
Window functions and recursive CTEs cost real time at scale. The error log on a busy line grows by about 50,000 rows per shift. Attribution over 30 days of history means scanning several million rows. Three changes keep it manageable.
Index on (head_id, timestamp). The composite index supports PARTITION BY head_id ORDER BY timestamp directly. A full table scan becomes an index range scan.
Materialize intermediate results. For the recursive version, put the base case in a temporary table before the recursion runs. This cuts repeated work a lot.
Attribute incrementally. Do not recompute all history on every query. A nightly background job runs the CTE over the previous day's logs only. It writes the results to a separate attributed_errors table. Dashboard queries read that table, which is fast and simple.
What it unlocks
With reliable attribution, questions that were impossible become routine. Which component reference has the most vacuum errors this week? Which head and component pair fails most often? Do errors cluster around specific pick sequences, which would point to a pick-order optimization?
All of them need errors tied to components. The forward-fill CTE makes that link. It is one of the less visible parts of the hub's analytics layer, and one of the most basic.