BigQuery error · DML and concurrency
UPDATE or DELETE statement would affect rows in the streaming buffer
Your UPDATE, DELETE or MERGE targets rows that were streamed in recently with the legacy insertAll API (now called Storage Write API REST) and are still in the streaming buffer. Exclude recent rows with a filter, wait for the buffer to flush, or stream with the Storage Write API (gRPC), whose rows DML can modify.
The error message
UPDATE or DELETE statement over table <project>.<dataset>.<table> would affect rows in the streaming buffer, which is not supported
Causes
Rows streamed with tabledata.insertAll in the last 30 minutes
Rows written through the legacy streaming API, which Google now calls the Storage Write API (REST), can't be modified by UPDATE, DELETE, MERGE or TRUNCATE for about 30 minutes after they are written. In rare cases rows stay in the buffer for up to 90 minutes.
A pipeline that streams, then corrects
Streaming events in and immediately running a MERGE or DELETE to deduplicate or correct them is the classic trigger. Code that writes with the client libraries' insertAll-based methods (such as insert_rows in Python) hits it on the first correction.
Storage Write API data inside a transaction
Rows streamed with the Storage Write API (gRPC) can be modified by DML right away, except inside a multi-statement transaction. Data written by some connectors' direct-write modes can also be briefly unavailable to DML after commit.
How to fix it
1. Exclude rows still in the buffer
Filter the statement to rows older than the buffer window, using an ingestion timestamp your pipeline writes. Run bq show --format=prettyjson project:dataset.table and check streamingBuffer.oldestEntryTime; if the section is absent, all rows are available to DML.
DELETE FROM `project.events.page_views`
WHERE is_bot = TRUE
AND ingested_at < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 90 MINUTE);2. Exclude buffered rows of an ingestion-time partitioned table
Recently streamed rows have a NULL _PARTITIONTIME until they leave the buffer, so filtering on real partitions skips them.
UPDATE `project.events.clicks`
SET country = 'unknown'
WHERE country IS NULL
AND _PARTITIONTIME >= TIMESTAMP_SUB(TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(), DAY), INTERVAL 7 DAY);3. Switch to the Storage Write API (gRPC)
Rows written with the Storage Write API (gRPC) can be modified by UPDATE, DELETE and MERGE immediately. Google recommends it for new projects and positions it as the lower-priced option; compare ingestion costs on the pricing page.
4. Deduplicate at read time instead of mutating
Keep the streamed table append-only and expose a view that keeps the latest row per key. Run the corrective MERGE into a downstream table on a schedule, after the buffer window.
CREATE OR REPLACE VIEW `project.events.orders_latest` AS
SELECT *
FROM `project.events.orders_stream`
WHERE TRUE
QUALIFY ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY event_ts DESC) = 1;How to prevent it
- Design streamed tables as append-only and apply corrections downstream on a schedule.
- Write an
ingested_atcolumn with every streamed row so DML can always exclude the last 90 minutes. - A view that deduplicates on read is re-computed by every query, so on-demand you pay for scanning the stream table each time. Materialize the deduplicated result if many dashboards read it.
FAQ
How long do I need to wait before DML works?
For rows streamed with the legacy API, recent writes are those from the last 30 minutes; in rare cases rows stay in the streaming buffer for up to 90 minutes. Check streamingBuffer in the table metadata to know for sure.
Does INSERT work on a table with a streaming buffer?
Yes. The restriction applies to statements that modify or delete existing rows: UPDATE, DELETE, MERGE and TRUNCATE.
Is the failed statement billed?
On-demand, no: failed queries aren't billed. Under capacity pricing, slot time used before the failure counts toward the reservation.
Related
SQL reference
All BigQuery error messages · What 50M daily events say about BigQuery failures
