Skip to content

fix: qualify column names in PerfMetric upsert to avoid PG ambiguity#4684

Open
SAY-5 wants to merge 1 commit intoQuantumNous:mainfrom
SAY-5:fix/perf-metric-ambiguous-column
Open

fix: qualify column names in PerfMetric upsert to avoid PG ambiguity#4684
SAY-5 wants to merge 1 commit intoQuantumNous:mainfrom
SAY-5:fix/perf-metric-ambiguous-column

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented May 7, 2026

📝 变更描述 / Description

PostgreSQL 在 ON CONFLICT DO UPDATE SET 中将未限定的列名(如 generation_ms)视为既可指目标表列,也可指 EXCLUDED.generation_ms,触发 column reference is ambiguous (SQLSTATE 42702),导致 failed to flush perf metric bucket 日志刷屏,性能指标无法写入。

修复方法:在 UpsertPerfMetricgorm.Expr 累加表达式中显式使用 perf_metrics.<col> 限定符,明确引用目标表的现有值。MySQL 与 SQLite 同样接受 tablename.column 形式,无兼容性影响。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认
  • 非重复提交
  • Bug fix 说明
  • 变更理解
  • 范围聚焦
  • 本地验证: go build ./model/go vet ./model/ 通过
  • 安全合规

📸 运行证明 / Proof of Work

$ go vet ./model/
$ go build ./model/
(no output)

生成的 SQL 片段(PG)由

ON CONFLICT (...) DO UPDATE SET generation_ms = generation_ms + 123

变为

ON CONFLICT (...) DO UPDATE SET generation_ms = perf_metrics.generation_ms + 123

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of performance metric aggregation in database operations by ensuring proper column reference handling during conflict resolution.

PostgreSQL raises 'column reference is ambiguous' (SQLSTATE 42702) on
ON CONFLICT DO UPDATE because unqualified column names match both the
target row and EXCLUDED. Prefix with the table name so the existing
value is referenced unambiguously. Compatible with MySQL and SQLite.

Closes QuantumNous#4683

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9a7f61c7-9695-4753-bb6f-2a9feb2e9f61

📥 Commits

Reviewing files that changed from the base of the PR and between a7475a1 and faa0f14.

📒 Files selected for processing (1)
  • model/perf_metric.go

Walkthrough

The UpsertPerfMetric function now qualifies all seven accumulated metric column references with the perf_metrics. table prefix in the ON CONFLICT UPDATE clauses. This resolves SQL ambiguity errors when the database cannot determine which table owns an unqualified column name in the conflict resolution expression.

Changes

SQL Column Reference Qualification

Layer / File(s) Summary
Qualified Column References
model/perf_metric.go
All conflict update clauses now reference columns as perf_metrics.request_count, perf_metrics.success_count, perf_metrics.total_latency_ms, perf_metrics.ttft_sum_ms, perf_metrics.ttft_count, perf_metrics.output_tokens, and perf_metrics.generation_ms to eliminate column ambiguity in the upsert operation.

Estimated Code Review Effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 A column stood confused and lost,
Not sure which table paid the cost,
But with a prefix clear and bright,
perf_metrics. made queries right! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: qualifying column names in PerfMetric upsert to resolve PostgreSQL ambiguity errors.
Linked Issues check ✅ Passed The PR directly addresses issue #4683 by qualifying column names with perf_metrics prefix to resolve SQLSTATE 42702 ambiguity errors in ON CONFLICT DO UPDATE clauses.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the ambiguous column reference issue in UpsertPerfMetric; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

column reference "generation_ms" is ambiguous (SQLSTATE 42702)

1 participant