canonicalizeMediaItemReferencesTx executed every merge step with both
$1 (source) and $2 (canonical) regardless of how many placeholders the
step's SQL actually used. The second step, "drop source provider ids"
(DELETE ... WHERE content_id = $1), references only $1, so passing two
args made pgx reject the Exec under the default QueryExecModeCacheStatement
with "mismatched param and argument count" (extended_query_builder.go:35,
which enforces len(ParamOIDs) == len(args)). That aborted the whole merge
transaction, so any item that got past the "refusing to merge" guard never
de-duplicated — observed in production logs as repeated
"provider-id drift row repair failed ... drop source provider ids:
mismatched param and argument count".
Extract the step list to a package var and derive each step's args from the
highest positional placeholder its SQL binds (maxPlaceholder), rather than a
brittle strings.Contains(sql, "$2") substring check that would also match
"$20"/"$25" and silently mis-size a future step. mergeStepArgs panics on an
unsupported placeholder count (caught by TestMergeStepPlaceholdersAreBounded,
which asserts every step binds $1 and at most $2) so a malformed step fails
loud in tests rather than at runtime. Tests cover maxPlaceholder's
full-number parsing (incl. $20 vs $2) and arg arity.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>