BigQuery error · DML and concurrency
UPDATE/MERGE must match at most one source row for each target row
Your MERGE or UPDATE ... FROM joined a target row to two or more source rows, so BigQuery can't tell which values to write. The source has duplicate keys, or the join condition is too loose. Deduplicate the source to one row per key (QUALIFY ROW_NUMBER() = 1) or aggregate it before the statement.
The error message
UPDATE/MERGE must match at most one source row for each target row
Causes
Duplicate keys in the source
A staging table that received the same record twice (a retried load, an at-least-once stream, a CDC feed with several changes per key) makes one target row match several source rows. The statement fails at run time, after the join, even if the duplicates are identical.
Join condition on a non-unique column
Matching on email or order_date instead of the real key, or on only part of a composite key (for example order_id without line_number), produces many-to-one matches.
UPDATE ... FROM with a fan-out join
In UPDATE target SET ... FROM source WHERE ..., each target row may join at most one row from the FROM clause. Joining to a table of events or order lines instead of one row per key triggers the same error.
When MERGE checks it
For MERGE, the error is raised when at least one WHEN MATCHED clause performs an UPDATE and a target row matches several source rows. Inserts of unmatched rows are not affected.
How to fix it
1. Find the duplicate keys
Group the source by the join key. Any key with more than one row breaks the statement.
SELECT customer_id, COUNT(*) AS source_rows
FROM `project.staging.customer_updates`
GROUP BY customer_id
HAVING COUNT(*) > 1
ORDER BY source_rows DESC
LIMIT 20;2. Keep the latest row per key with QUALIFY
Deduplicate inside the USING subquery. Order by a reliable change timestamp, and add a tie-breaker column if two changes can share a timestamp.
MERGE `project.crm.customers` AS t
USING (
SELECT *
FROM `project.staging.customer_updates`
WHERE TRUE
QUALIFY ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY updated_at DESC, ingest_id DESC
) = 1
) AS s
ON t.customer_id = s.customer_id
WHEN MATCHED THEN
UPDATE SET email = s.email, country = s.country, updated_at = s.updated_at
WHEN NOT MATCHED THEN
INSERT (customer_id, email, country, updated_at)
VALUES (s.customer_id, s.email, s.country, s.updated_at);3. Aggregate the source when you need totals
If several source rows should add up, aggregate them to one row per key first.
UPDATE `project.sales.orders` AS o
SET refunded_amount = r.total_refunded
FROM (
SELECT order_id, SUM(amount) AS total_refunded
FROM `project.sales.refunds`
GROUP BY order_id
) AS r
WHERE o.order_id = r.order_id;4. Match on the full key
Include every column of the business key in the ON or WHERE condition. If key columns can be NULL, remember that NULL = NULL is not true: use x = y OR (x IS NULL AND y IS NULL) or replace NULLs with a sentinel on both sides.
How to prevent it
- Assert uniqueness of the source key in the pipeline (for example a dbt or Dataform uniqueness test) before the MERGE runs.
- Load staging tables with
WRITE_TRUNCATEor a run ID, so retries don't append the same batch twice. - The failed statement has already scanned source and target. On-demand it isn't billed, but under capacity pricing the slot time counts, and a scheduled retry fails again until the data is fixed.
FAQ
Why does Snowflake or PostgreSQL behave differently?
PostgreSQL's UPDATE ... FROM silently uses one of the matching rows. Snowflake fails a nondeterministic MERGE by default but lets a nondeterministic UPDATE through unless ERROR_ON_NONDETERMINISTIC_UPDATE is set. BigQuery fails both, which protects you from writing an arbitrary value.
Does the error happen if the duplicate source rows are identical?
Yes. BigQuery checks the number of matches, not whether the values differ. Remove exact duplicates with SELECT DISTINCT or QUALIFY before the statement.
Am I billed for the failed MERGE?
On-demand, no: failed queries aren't billed. With capacity pricing, the slots the statement used before failing count toward your reservation.
Related
SQL reference
All BigQuery error messages · What 50M daily events say about BigQuery failures
